i18next-scanner
i18next-scanner copied to clipboard
Parser parses JSX before customTransform function execution
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: https://github.com/i18next/i18next-scanner/blob/b19aa7b1c8fb5587f65d9a568145676687a43f7f/src/index.js#L35
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();
},