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

Bump recast and babylon to support JSX fragments #239

Merged
merged 1 commit into from
Mar 7, 2018

Conversation

xixixao
Copy link
Contributor

@xixixao xixixao commented Feb 22, 2018

What is says on the can.

@xsburg
Copy link

xsburg commented Feb 22, 2018

Just wanted to point out that this PR basically adds support for TypeScript transformations as well, thanks to the effort of @benjamn and @Phoenixmatrix who supported it in the ast-types and recast packages.
All we need now is to create a babylon parser with enabled typescript option:

module.exports = function(fileInfo, api) {
  // your codemod here
}
const babylon = require('babylon');
const parserOptions = {
    sourceType: 'module',
    allowImportExportEverywhere: true,
    allowReturnOutsideFunction: true,
    plugins: [
        'jsx',
        'typescript',
        'asyncFunctions',
        'classConstructorCall',
        'doExpressions',
        'trailingFunctionCommas',
        'objectRestSpread',
        'decorators',
        'classProperties',
        'exportExtensions',
        'exponentiationOperator',
        'asyncGenerators',
        'functionBind',
        'functionSent',
        'dynamicImport'
    ]
};
module.exports.parser = {
    parse(code) {
        return babylon.parse(code, parserOptions);
    }
};

@benjamn
Copy link

benjamn commented Feb 22, 2018

With recast@0.14.2 (just published), @xsburg's example becomes even easier:

module.exports = function(fileInfo, api) {
  // your codemod here
}
// Use a parser (Babylon 7) preconfigured for TypeScript.
// Note that you still need to run `npm install babylon@next` yourself,
// since Recast does not depend directly on Babylon.
module.exports.parser = require("recast/parsers/typescript");

More information about passing a different parser to recast.parse. The preconfigured parsers are still a work in progress, so please let me know if they fail to meet your needs.

@Phoenixmatrix
Copy link

I believe there's still gonna be the issue of jsx vs type assertions (not using the "as" syntax) being mutually exclusive though?

@benjamn
Copy link

benjamn commented Feb 22, 2018

Yeah, recast/parsers/typescript currently doesn't include the jsx Babylon plugin.

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

Successfully merging this pull request may close these issues.

6 participants