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

recast with espree failed on new.target #352

Open
pionxzh opened this issue Nov 26, 2023 · 17 comments
Open

recast with espree failed on new.target #352

pionxzh opened this issue Nov 26, 2023 · 17 comments

Comments

@pionxzh
Copy link
Contributor

pionxzh commented Nov 26, 2023

I know this might look like I'm reporting in the wrong place. Let me explain.

  1. espree can parse the code without error
  2. recast with espree will have this error
  3. recast with default parser (esprima) will not have this error
  4. recast does not officially support espree as parser, that why I report it here

I don't know which project should be responsible for this.

I have tried various espree options to make it work. But failed.

Input

const { transform } = require('lebab');
const code = `function foo() { new.target }`;

transform(
    code, // code to transform
    ['let'] // transforms to apply
);

Output

TypeError: Cannot read properties of undefined (reading 'line')
    at Object.fixFaultyLocations (\node_modules\recast\lib\util.js:153:23)
    at TCp.copy (\node_modules\recast\lib\parser.js:138:10)
    at TCp.copy (\node_modules\recast\lib\parser.js:190:30)
    at TCp.copy (\node_modules\recast\lib\parser.js:190:30)
    at TreeCopier.<anonymous> (\node_modules\recast\lib\parser.js:131:30)
    at Array.forEach (<anonymous>)
    at TCp.copy (\node_modules\recast\lib\parser.js:130:14)
    at TCp.copy (\node_modules\recast\lib\parser.js:190:30)
    at TCp.copy (\node_modules\recast\lib\parser.js:190:30)
    at TreeCopier.<anonymous> (\node_modules\recast\lib\parser.js:131:30)

The wrong node is the new.target (MetaProperty)

{
  "type": "Identifier",
  "start": 20,
  "end": 23,
  "loc": {
    "end": {
      "line": 2,
      "column": 5
    }
  },
  "name": "new"
}
@nene
Copy link
Collaborator

nene commented Nov 27, 2023

Perhaps you could give a small heads up what this new.target code is supposed to do. I'm currently away from computer and not really keen to dig into ecmascript spec.

@pionxzh
Copy link
Contributor Author

pionxzh commented Nov 28, 2023

new.target is MetaProperty in the AST node. The expectation is not to throw an error when lebab processes this code snippet. lebab won't transform this code, but the tool (recast + espree) combination used by lebab will fail on this.

Or maybe we can have an option to customize the parser used by recast? I tried the default parser(esprima) can works normally.

@nene
Copy link
Collaborator

nene commented Nov 28, 2023

Well... I'll need to take a closer look when I get back.

Possibly Lebab could switch to a different parser. I don't really remember why exactly it uses espree. There likely were some issues with other parsers. But it's been a while and sure these parsers have advanced a bit. Lebab isn't really in active development. It's not really even in active maintenance. But when I feel like it, I might fix a bug or two. Though switching a parser is probably a fairly involved task.

@pionxzh
Copy link
Contributor Author

pionxzh commented Nov 28, 2023

I can understand the status of this project. Don't worry 🙏
I can send a PR for the parser part.

@nene
Copy link
Collaborator

nene commented Nov 28, 2023

Sure. I'm open for a PR.

@pionxzh
Copy link
Contributor Author

pionxzh commented Jan 11, 2024

This issue seems fixed for me... I'm not sure why

Still can reproduce

@nene
Copy link
Collaborator

nene commented Jan 13, 2024

So, I finally started looking closer at this issue, and I'm unable to reproduce it.

Testing with this snippet:

function foo() { new.target }

It works just fine on my machine, and when I try it in the demo site, it also works without problems.

I see you patched the problem in Recast. I suggest you send that patch to Recast repository. I'm sure Recast also wants to support the Espree parser (although it doesn't explicitly list a configuration for it in its parsers/ dir).

But I guess the important bit is making this bug first reproducible. Currently for me it's not.

@pionxzh
Copy link
Contributor Author

pionxzh commented Jan 13, 2024

Run this in Node.js. Not sure why web is fine.

const { transform } = require('lebab')

const code = `function foo() { new.target }`;

transform(
  code, // code to transform
  ['let'] // transforms to apply
);

@pionxzh
Copy link
Contributor Author

pionxzh commented Jan 13, 2024

var espree = require('espree')
var recast = require('recast')

const code = `
function foo() { new.target }
`

const ESPREE_OPTS = {
  ecmaVersion: 2022,
  ecmaFeatures: {jsx: true},
  comment: true,
  tokens: true,
  range: true,
  loc: true,
};

var parser = {
  parse: function parse(js, opts) {
    return espree.parse(js, ESPREE_OPTS);
  }
};

const ast = recast.parse(code, { parser })

const result = recast.print(ast, {
  lineTerminator: '\n',
  objectCurlySpacing: false,
}).code;

console.log(result);

@nene
Copy link
Collaborator

nene commented Jan 13, 2024

Run this in Node.js. Not sure why web is fine.

Tried. Works without issues.

@nene
Copy link
Collaborator

nene commented Jan 13, 2024

This other example also works without issue for me.

@nene
Copy link
Collaborator

nene commented Jan 13, 2024

Also running in codesandbox.io

@pionxzh
Copy link
Contributor Author

pionxzh commented Jan 13, 2024

Hmm. What OS did you use?

Mine:
OS: Windows 11
Node.js 20.10.0

@pionxzh
Copy link
Contributor Author

pionxzh commented Jan 13, 2024

It works fine on my Mac. WTF.

@nene
Copy link
Collaborator

nene commented Jan 13, 2024

Yeah, I'm using Mac. Node 18.

@nene
Copy link
Collaborator

nene commented Jan 13, 2024

Perhaps there's something off with your windows environment. Maybe you can ask somebody to try it on Windows. I currently don't have a Windows at hand.

@nene
Copy link
Collaborator

nene commented Jan 13, 2024

I'd suspect it's a difference in Node environment, not a difference in OS.

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