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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with parsing jsx #1

Closed
adambrgmn opened this issue Feb 24, 2022 · 4 comments 路 Fixed by #2
Closed

Issues with parsing jsx #1

adambrgmn opened this issue Feb 24, 2022 · 4 comments 路 Fixed by #2

Comments

@adambrgmn
Copy link
Contributor

This was exactly the kind of tool I was looking for, thanks for that! 馃憦
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:

  1. Create new project (npm init -y) and install equivalent-exchange (npm install equivalent-exchange)
  2. 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);
}
  1. 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.

@adambrgmn
Copy link
Contributor Author

The following code works fine, when I do self-closing tags:

export const Foo: React.FC<{ foo: string }> = ({foo}) => {
  return <div />
}

@adambrgmn
Copy link
Contributor Author

And of course, now I saw that my test used self-closing tag as well. This test fails:

test('with jsx syntax', async () => {
  const code = `export const Component: React.FC = () => {
    console.log("hello!");
    return <div>Hello</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();
});

@adambrgmn
Copy link
Contributor Author

I found the solution and submitted a PR #2. Hope you're okey with that 馃憤 And once again, thanks for the repo!

@suchipi
Copy link
Owner

suchipi commented Feb 25, 2022

Fixed in 1.2.1 馃憤

Thank you again for reporting and fixing this!

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 a pull request may close this issue.

2 participants