30 ms·
Ts-morph – programmatically manipulate TypeScript source code with TypeScript
- deleted 3y ago[deleted]
- no_wizard 3y agoEventually 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.
- nawgz 3y agoI 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.
- moron4hire 3y agoI 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-options.html 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.
- nawgz 3y agoReact 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
- tinideiznaimnou 3y ago[flagged]
- nawgz 3y ago[flagged]
- tinideiznaimnou 3y ago[flagged]
- nawgz 3y agoYikes, 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.
- tinideiznaimnou 3y ago[flagged]
- nawgz 3y ago> 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
- com2kid 3y agoThat 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.
- nawgz 3y agoYour 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
- re-thc 3y agoMaybe comment or check out https://github.com/microsoft/TypeScript/issues/54276 https://github.com/microsoft/TypeScript/issues/54276 that proposes transformer plugins?
- madarco 3y agoThis can be easily solved by using https://github.com/cevek/ttypescript https://github.com/cevek/ttypescript, which enables the use of transformers in tsc and allows them to be defined in the tsconfig.json file.
- raydiatian 3y agoI used this to build an inhouse dev tool that generates boilerplate code once.
- ponyous 3y agoA better (simpler?) version to generate boilerplate is hygen in my opinion in case anyone is thinking of this use case.
- madarco 3y agoTotally agree, Hygen (https://www.hygen.io/docs/quick-start https://www.hygen.io/docs/quick-start) is great and the best part is that everyone in the team can easily customize the generators
- raydiatian 3y agoThanks!
- klysm 3y agoTemplates are brittle
- neilv 3y agoFor people who want to do similar programmatic manipulation using Racket: https://www.neilvandyke.org/racket/progedit/ https://www.neilvandyke.org/racket/progedit/ (Not just manipulation of Lisp-like languages: any language can be parsed to Racket syntax objects.)
- funnymunny 3y agoI’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.
- klysm 3y agoI’ve also always wanted more advanced search - kind of like a SQL over all the AST
- nomurrcy 3y agoThis 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.
- klysm 3y agoIs 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
- aabbcc1241 3y agoYes, the demo in readme shows you to create/edit source code in type-safe manner