Typescript support?
This library is awesome, but I can't use it since my codebase is in typescript. Any chance of TS support in the future? Or could you maybe point me in the direction of similar projects that work for TS?
In theory it is possible to support any language. Things that would need to be done:
- Create custom AST node definitions for recast/ast-types (if necessary)
- Create custom pretty printer for recast or extend recast's existing printer
- Create custom scope resolution logic for recast (if necessary, probably the most difficult part)
- Create language specific jscodeshift API (if desired).
Not using TypeScript myself, I don't know of any similar tool for it.
https://github.com/babel/babylon/pull/523 is a big step towards making this happen. Perhaps this justifies reopening the issue?
Has the landscape changed enough for this to be possible now? I know the Babylon parser has typescript support in the v7 beta, but not sure if the recast portion of the tooling has any support yet.
But be really nice to make this happen. @fkling can you reopen?
Also see https://github.com/benjamn/recast/issues/424
Echoing @fkling's comment above, I've outlined what's required on the Recast side here.
While I agree with @fkling about the steps, I'm generally pretty optimistic about this effort, and I would note that only the first two steps are critical to implementing TypeScript support in Recast (adding the types to ast-types and adding new printer cases to Recast).
Whether or not this happens ultimately depends on whether the work gets done (is that a tautology? maybe 😛 ). I'm happy to help, and I imagine @fkling is cautiously happy to provide some guidance, but I don't think either of us can commit to this project in a serious way unless we get a lot of help from the community.
I am very interested in using jscodeshift on a TypeScript code base. I might have some learning curve on the parsing and AST stuff, but I'd be willing to help with this effort if I can!
I used the flow parser on a typescript codebase and it actually worked very well. @blaster151 perhaps you could use it as a starting point?
Hmm. Thanks, @ianks! I did not consider that and I will look into it!
Same position as @blaster151 Happy to help, not so good on AST, but good on TypeScript, so if anyone can point mein the right direction, I'll make a start :)
I made a repo containing some simple codemods to transform js{x} based react code to tsx based code. IMO, it is possible to parse react code using babylon parsor
https://github.com/sjy/js2tsx
I copied an Angular(typescript) component in astexplorer.net and a simple codemod that reverses identifiers (https://gist.github.com/de28761373f95a0615a4ef9bc4155822/e73fd0cdec045078763954f6d2f58a434ab8f429).
It works fine if the component does not contain "public" or "private" keyword but if it does following error is occurring: "The keyword 'public' is reserved error"
I can use the Flow parser to parse TypeScript with the exception of the casting operator (as in,
What is the concluding note here? Should we use Flow parser or parser created by @sjy? Further, can you please provide any example on how to use Flow parser for migration?
@Phoenixmatrix has been doing some great work to add TypeScript support to both ast-types and recast: https://github.com/benjamn/recast/issues/424#issuecomment-363131130
- [x] https://github.com/benjamn/ast-types/pull/249
- [x] https://github.com/benjamn/recast/pull/461
This paves the way for jscodeshift to support TypeScript transforms. I wrote a quick example here: https://github.com/elliottsj/jscodeshift-transforms
To make this work while those pull requests are pending, that repo has an npm dependency on https://github.com/elliottsj/jscodeshift/tree/typescript, which has an npm dependency on https://github.com/elliottsj/recast/tree/typescript-initial, which has an npm dependency on https://github.com/Phoenixmatrix/ast-types/tree/typescript-initial
This is an example transform which works on TypeScript:
https://github.com/elliottsj/jscodeshift-transforms/blob/master/transforms/reverse-identifiers.js
The key is to have module.exports.parser defined in the transform module, which will parse code using babylon@7 (which can parse TypeScript AST nodes). At some point, it may be a good idea to update jscodeshift itself to use babylon@7 when the --parser babylon option is used, but I'm not sure whether that should wait until babylon@7 is out of beta.
@elliottsj btw, one thing I'm not sure how jscodeshift should handle: the difference between TS and TSX. In Babylon world, as far as I understand, the difference is just if you add the jsx plugin or not. But jscodeshift is usually used to process a lot of files, and it would make sense to be able to go through ts and tsx files in one run, thus requiring to toggle the parser based on file extension or something.
If your code never use <foo>bar style assertion then it doesn't matter, but that's probably not a good assumption to make.
@Phoenixmatrix that's a good point. The fileInfo parameter can be used to tell whether the file is tsx:
module.exports = function transform(fileInfo, api) {
const j = api.jscodeshift;
if (fileInfo.path.endsWith('.tsx')) {
j(fileInfo.source)
// ...
} else {
j(fileInfo.source)
// ...
}
}
but module.exports.parse still somehow needs to know whether to apply the 'jsx' plugin or not.
-
One option is to parse using recast in the transform itself:
module.exports = function transform(fileInfo, api) { const parse = source => babylon.parse(source, { sourceType: 'module', plugins: file.path.endsWith('.tsx') ? ['jsx', 'typescript'] : ['typescript'], }); const j = api.jscodeshift; return j(recast.parse(file.source, { parser: { parse } })) // ... } -
Another option is to modify jscodeshift and recast to pass another parameter
pathto theparsefunction:module.exports.parser = { parse: (source, path) => babylon.parse(source, { sourceType: 'module', plugins: path.endsWith('.tsx') ? ['jsx', 'typescript'] : ['typescript'], }), }; -
Or, if we update jscodeshift to use babylon@7, we could make jscodeshift smart enough to apply the 'jsx' plugin automatically when parsing a file with extension .tsx, e.g. here.
I'm not sure which of these is better.
I'm thinking option 3 is probably what we want for --parser babylon once jscodeshift is on babylon@7. But option 1 feels like a good workaround in the interim.
@fkling what do you think?
I tried Option 3 by upgrading beta version of babylon
npm install [email protected] - transformation didn't work.
Also tried option 1. Updated manual-bind-to-arrow.js file, however that did not updated a single file.
command: jscodeshift -t transforms/manual-bind-to-arrow.js 'C:\\Users\\name\\Project\\proj1\\ForUpgrade\\src'
Updated file is:
export default function transformer(file, api) {
const parse = source => babylon.parse(source, {
sourceType: 'module',
plugins: file.path.endsWith('.tsx') ? ['jsx', 'typescript'] : ['typescript'],
});
const j = api.jscodeshift;
var root = j(file.source);
.
.
.
if (hasChanged) {
//return transform.toSource();
return j(recast.parse(file.source, { parser: { parse } }))
}```
Question: Our current project runs on React 15.4.2. We identified that only following transforms are required:
- manual-bind-to-arrow
- pure-component
- pure-render-mixin
- rename-unsafe-lifecycles
- react-to-react-dom
However, as it is Typescript - codemode does not work. It is advisable to simply upgrade without transformation? If not then manual find and replace is the only option for now.
@RupenAnjaria you will also need to use the work-in-progress versions of ast-types and recast, which are not yet done nor released:
- [x] https://github.com/benjamn/ast-types/pull/249
- [x] https://github.com/benjamn/recast/pull/461
You will probably also need the latest version of babylon@7, which is currently 7.0.0-beta.40.
Those transforms you mentioned probably also won't work with TypeScript sources, since they're designed around a JS AST; TypeScript type annotations will likely not be preserved.
TypeScript support is very much in a work-in-progress state right now, so I wouldn't recommend using it for any purpose beyond helping to test the pending ast-types and recast changes.