This was exactly the kind of tool I was looking for, thanks for that! :clap:
But I stumbled upon something that I really can't figure out. I have issues parsing code with JSX syntax.
Here's the way I did to reproduce:
- Create new project (
npm init -y
) and install equivalent-exchange
(npm install equivalent-exchange
)
- Create a file with the following content:
import { transmute, traverse } from 'equivalent-exchange';
const code = `
export const Foo: React.FC<{ foo: string }> = ({foo}) => {
return <div>{foo}</div>
}
`;
try {
transmute(code, { fileName: 'Foo.tsx' });
} catch (error) {
console.error(error);
}
- Execute the script with node
node script.mjs
With this I get the following error thrown:
Error: Line 3: Invalid regular expression: missing /
at ErrorHandler.constructError (/project/node_modules/esprima/dist/esprima.js:5012:22)
at ErrorHandler.createError (/project/node_modules/esprima/dist/esprima.js:5028:27)
at ErrorHandler.throwError (/project/node_modules/esprima/dist/esprima.js:5035:21)
at Scanner.throwUnexpectedToken (/project/node_modules/esprima/dist/esprima.js:5164:35)
at Scanner.scanRegExpBody (/project/node_modules/esprima/dist/esprima.js:6139:23)
at Scanner.scanRegExp (/project/node_modules/esprima/dist/esprima.js:6204:29)
at Tokenizer.getNextToken (/project/node_modules/esprima/dist/esprima.js:6675:56)
at Object.tokenize (/project/node_modules/esprima/dist/esprima.js:154:36)
at Object.parse (/project/node_modules/recast/lib/parser.js:40:30)
at codeToAst (/project/node_modules/equivalent-exchange/dist/index.js:45:24) {
index: 90,
lineNumber: 3,
description: 'Invalid regular expression: missing /'
}
The strange thing is that when I clone this repo and try adding a test like this it all passes π€
test('with jsx syntax', async () => {
const code = `export const Component: React.FC = () => {
console.log("hello!");
return <div />
}`;
const transform = (ast: AST) => {
traverse(ast, {
StringLiteral(path) {
const { node } = path;
if (node.value === 'hello!') {
path.replaceWith(types.stringLiteral('goodbye!'));
}
},
});
};
expect(() => {
transmute(code, transform);
}).not.toThrow();
});
Do you have any ideas of what might be going wrong? At first I thought of different babel versions colliding. But since it didn't work in my very bare-bones repro either I'm suspecting it is something else.