Hacker News new | past | comments | ask | show | jobs | submit login
Ts-morph – programmatically manipulate TypeScript source code with TypeScript (npmjs.com)
43 points by aabbcc1241 on May 18, 2023 | hide | past | favorite | 21 comments



Eventually I wonder if the TypeScript team will revert their stance on not allowing compiler plugins. It seems like such a waste not to have a standard package interface. All this is doing is exposing the already provided compiler tools, its not using Babel or anything, its just a nicer API over the raw TS compiler API.

Arguably, the TypeScript team should be supplying that nicer API. Its the only thing missing to me from TypeScript that it really needs.


I personally understand the TS team’s stance. Imagine every dev shop having their own TS transforms in the pipeline… I could easily envision fragmentation. TS is great because of how predictable it is.


I don't know. With the plethora of compiler flags that TSC has that can dramatically alter how TypeScript works and where and when it looks for type definitions, I don't think one can really say that cross-project TypeScript setups are predictable. At least, I know I have spent many, many hours playing whack-a-mole with this page (https://www.typescriptlang.org/docs/handbook/compiler-option...) while trying to get various projects integrated with mine.

It's also concerning just how much TypeScript has in it that is specific to just JSX. I mean, how did it work out that React got to get special treatment with the TS compiler? Can any source transforming project get that kind of treatment? That's exactly the sort of thing that should have been delegated to plugins.


React isn’t the only library using JSX, first, and then it’s also true React is the biggest web framework and TS applies best to writing web apps, second, so it seems pretty natural to me they would bake in JSX support.

I suggest you just find one TSConfig setup that works for you and go from that… I’ve never felt these integration pains in the 4 years of heavy TS usage. That said, I authored my own component library, so maybe that’s a factor


[flagged]


[flagged]


[flagged]


Yikes, not doing yourself any favors. From oligopolists ruining everything to "fuck your open source" in just one reply.

Good luck getting thru your day - seems you might need it.


[flagged]


> Fuck Microsoft's open source, since you need things spelled out for you.

I do need that spelled out for me; TS is clearly such a humungous improvement on writing JS that it's ludicrous you would prefer them to just... keep it to themselves.

You're fully capable of opting out of using it and writing NodeJS; with all the rage in your heart for TS, I don't really understand why you didn't take that approach

> people who actually give a shit about situations getting from bad to worse

Lmao, you're so dramatic. Enjoy


That has already happened, but the libraries are being utilized at runtime with associated overheads, and in fact the hoops jumped through are larger and uglier than if TS had plugins.

There is no bloody reason why a custom JSON.parse shouldn't be able to look at compile time at what the expected type of a JSON blog is and compile time generate validation code.


Your example already exists, it’s called TSON, unless you are complaining about how they had to implement that

I also disagree that type validators like Zod/io-ts would benefit from plugins, they are just js code in the end that does defensive checks for you. Unless you have something else in mind


Maybe comment or check out https://github.com/microsoft/TypeScript/issues/54276 that proposes transformer plugins?


This can be easily solved by using https://github.com/cevek/ttypescript, which enables the use of transformers in tsc and allows them to be defined in the tsconfig.json file.


Is it possible to use ts-morph to generate new code or is it mostly aimed at modifying existing code? I want to generate some boilerplate in a project im working on but not sure what the best tool is


Yes, the demo in readme shows you to create/edit source code in type-safe manner


I’ve always wanted a tool that could search ts code using types & logic, e.g. you could search for every instance where an object of a particular type has a value assigned to one of its properties.


I’ve also always wanted more advanced search - kind of like a SQL over all the AST


This is a great tool. The api surface takes a bit to figure out, and navigating manipulating the syntax tree can require some trial and error, but I’ve used this to do large scale refactors that would have been very time consuming if done by hand.

99% of the time the tools provided by jetbrains / vscode are more than sufficient, but 1% of the time a tool like this saves you a bunch of time, both searching for what you need to change and making the changes you want.


For people who want to do similar programmatic manipulation using Racket: https://www.neilvandyke.org/racket/progedit/

(Not just manipulation of Lisp-like languages: any language can be parsed to Racket syntax objects.)


I used this to build an inhouse dev tool that generates boilerplate code once.


A better (simpler?) version to generate boilerplate is hygen in my opinion in case anyone is thinking of this use case.


Totally agree, Hygen (https://www.hygen.io/docs/quick-start) is great and the best part is that everyone in the team can easily customize the generators


Templates are brittle


Thanks!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: