-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(typescript): process TypeScript files #139
Conversation
README.md
Outdated
``` | ||
|
||
This will re-write the files `path/to/file.js` and `another/file.js` by transforming them with the babel plugin `transform-module-name`. Multiple plugins may be specified, and multiple files or directories may be re-written at once. | ||
This will re-write the files `path/to/file.js` and `another/file.js` any supported files found in `a/directory` by transforming them with the babel plugin `transform-module-name`. Multiple plugins may be specified, and multiple files or directories may be re-written at once. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: "any supported files" -> "and any ..." or switch to oxford comma: "path/to/file.js
, another/file.js
, and supported files found in a/directory
"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
src/AllSyntaxPlugin.ts
Outdated
for (let plugin of ALL_PLUGINS) { | ||
if (plugin !== 'flow' || !opts.filename.endsWith('.ts')) { | ||
let usePlugin = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bit nasty to read. what about:
const plugins = ALL_PLUGINS.filter(plugin => {
if (plugin === 'flow' && isTypeScript) return false;
if (plugin === 'typescript' && !isTypeScript) return false;
return true;
});
parserOpts.plugins.concat(...plugins);
or something similar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that -- another way that might be cleaner is having BASE_PLUGINS
and then something like
const plugins = isTypescript ? [...BASE_PLUGINS, 'typescript'] : [...BASE_PLUGINS, 'flow']
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestions! I've gone with a hybrid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Generally LGTM, mostly just nits.
README.md
Outdated
``` | ||
|
||
This will re-write the files `path/to/file.js` and `another/file.js` by transforming them with the babel plugin `transform-module-name`. Multiple plugins may be specified, and multiple files or directories may be re-written at once. | ||
This will re-write the files `path/to/file.js` and `another/file.js` any supported files found in `a/directory` by transforming them with the babel plugin `transform-module-name`. Multiple plugins may be specified, and multiple files or directories may be re-written at once. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo nit: "another/file.js
any supported" should be "another/file.js
and any supported"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed.
src/AllSyntaxPlugin.ts
Outdated
]; | ||
|
||
export default function(babel: typeof Babel) { | ||
return { | ||
manipulateOptions(opts: BabelOptions, parserOpts: ParseOptions) { | ||
let isTypeScript = TypeScriptExtensions.has(extname(opts.filename)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this can be a const
src/AllSyntaxPlugin.ts
Outdated
for (let plugin of ALL_PLUGINS) { | ||
if (plugin !== 'flow' || !opts.filename.endsWith('.ts')) { | ||
let usePlugin = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, could be const
@@ -157,7 +157,7 @@ function getRequirableModulePath(modulePath: string): string { | |||
return resolve(modulePath); | |||
} | |||
|
|||
for (let ext of SUPPORTED_EXTENSIONS) { | |||
for (let ext of RequireableExtensions) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ext
can be const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general I've mostly followed Ember's conventions for let
and const
, but lately I've been tempted to just use const
everywhere since it is sometimes needed for TypeScript to make certain guarantees when type checking. For now I'll leave these as let
and maybe change them all in a later refactor.
@@ -0,0 +1,24 @@ | |||
function union<T>(...sets: Array<Set<T>>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙌 I like this refactor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 😄
@@ -6,7 +6,11 @@ export interface BabelOptions { | |||
filename: string; | |||
} | |||
export interface ParseOptions { | |||
plugins: Array<string>; | |||
sourceType?: 'module' | 'script'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like 'script'
isn't used as a value anywhere? Or is that user-configurable / future-proofing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally all these things would be provided by .d.ts
files that come with e.g. @babel/parser
, but we don't currently live in that world and I'm not sure how to pull them from DefinitelyTyped when the packages are namespaced, so I just manually write out a subset here. These are the two allowed values for sourceType
, so I just encoded that here.
To support TypeScript, mostly we just needed to use the `typescript` parser plugin when the file being processed is TypeScript. BREAKING CHANGE: This expands the set of file extensions that babel-codemod will look for to include `.ts`, `.tsx`, `.es`, `.es6`, and `.mjs` in addition to `.js` and `.jsx`. Closes #137
882cd0c
to
07a4930
Compare
🎉 This PR is included in version 2.0.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
To support TypeScript, mostly we just needed to use the
typescript
parser plugin when the file being processed is TypeScript.BREAKING CHANGE: This expands the set of file extensions that babel-codemod will look for to include
.ts
,.tsx
,.es
,.es6
, and.mjs
in addition to.js
and.jsx
.Closes #137