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

Parser parses JSX before customTransform function execution #173

Open
gornyyvladimir opened this issue Jan 31, 2020 · 0 comments
Open

Parser parses JSX before customTransform function execution #173

gornyyvladimir opened this issue Jan 31, 2020 · 0 comments

Comments

@gornyyvladimir
Copy link

gornyyvladimir commented Jan 31, 2020

Hello! I have a project that uses React + Flow. Because I have Flow in project scanner shows Unable to parse Trans component from error.
I have written this customTransform function to remove Flow types:

transform: function customTransform(file, enc, done) {
    const parser = this.parser;
    const content = fs.readFileSync(file.path, enc);
    const code = flowRemoveTypes(content)
    parser.parseTransFromString(code.toString());
    done();
  },

However, if you have extensions array in trans options, this code will run and crash your app:

if (includes(get(options, 'trans.extensions'), extname)) {

    trans: {
      component: 'Trans',
      i18nKey: 'i18nKey',
      defaultsKey: 'defaults',
      extensions: ['.js', '.jsx'], // <--- this line crash my app, if extensions not null
      fallbackKey: function(ns, value) {
        return value;
      },
      acorn: {
        ecmaVersion: 10, // defaults to 10
        sourceType: 'module', // defaults to 'module'
      },
    },

Because it tries to parse code before the transformation, and Flow types crash it.

If I change extensions to null and modify customTransform function it will run first, and parser in function (parser.parseTransFromString) will work with transformed code and all is ok.
My final customTransform:

  transform: function customTransform(file, enc, done) {
    const parser = this.parser;
    const extname = path.extname(file.path);
    console.log(file.path, extname);
    const content = fs.readFileSync(file.path, enc);
    if (['.js', '.jsx'].includes(extname)) {
      const code = flowRemoveTypes(content)
      parser.parseTransFromString(code.toString());
    }
    done();
  },
@gornyyvladimir gornyyvladimir changed the title Parser parse JSX before customTransform function execution Parser parses JSX before customTransform function execution Jan 31, 2020
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

No branches or pull requests

1 participant