Skip to content
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

Merged
merged 1 commit into from
Jun 20, 2018

Conversation

eventualbuddha
Copy link
Collaborator

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

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.

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"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

for (let plugin of ALL_PLUGINS) {
if (plugin !== 'flow' || !opts.filename.endsWith('.ts')) {
let usePlugin =

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?

Copy link

@cab cab Jun 20, 2018

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']

Copy link
Collaborator Author

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.

Copy link

@cab cab left a 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.
Copy link

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"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed.

];

export default function(babel: typeof Babel) {
return {
manipulateOptions(opts: BabelOptions, parserOpts: ParseOptions) {
let isTypeScript = TypeScriptExtensions.has(extname(opts.filename));
Copy link

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

for (let plugin of ALL_PLUGINS) {
if (plugin !== 'flow' || !opts.filename.endsWith('.ts')) {
let usePlugin =
Copy link

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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ext can be const

Copy link
Collaborator Author

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>>) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌 I like this refactor

Copy link
Collaborator Author

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';
Copy link

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?

Copy link
Collaborator Author

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
@eventualbuddha eventualbuddha merged commit 00ae598 into master Jun 20, 2018
@eventualbuddha eventualbuddha deleted the transform-typescript branch June 20, 2018 17:56
@babel-codemod-bot
Copy link

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants