-
Notifications
You must be signed in to change notification settings - Fork 44
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
Attach comments #81
Comments
Could you look at the code and try to figure out where it goes wrong? What's the differences vs Esprima as you mentioned? @3cp do you have any idea what's going wrong here? |
I'm not sure I fully understand the logic, but I can attach the tokens I get from Esprima and what I get in Meriyah. BTW, it also get the wrong Punctuator values (I used a workaround to fixed that, but it actually broken too).
|
I see the range for ident is out of range? |
It is just different than it should be and that's why escodegen can't generate comments. to { we'll successfully get the first comment. |
I also had to convert "start" and "end" properties into range:[start,end], for each body objects, to be able to generate the code (even without comments) |
Both start, end, and range[] are not estree standard. There was some argument in estree spec repo but no action was taken so far. As for skipped comments, I think I saw the behavior too but it doesn't affect my use cases. I will see if I can find anything. |
Thank you! Looking forward for that. |
Use of range[] kills performance, but the loc tracking itself does it too. It's better to use an optional linear incrementing id to store 4 * 4 bytes (32bit for start, stop, col, line) per |
I guess that's the effect of an array. @KFlash I am not sure do you want to add range[] or not? Maybe another option to add this compatible range[] thing? |
A 'range[]' property is fine, and makes Meriyah more compatible with other parsers. |
The comments are not missing, but the token after comment has wrong start. const meriyah = require('meriyah');
const sourceCode = `
// Comment1
const a = 1;
// Comment2
const b = 2;
`;
let tokens = [];
let comments = [];
let tree = meriyah.parse(sourceCode, {
ranges: true,
globalReturn: true,
onToken: function (type, start, end) {
console.log('onToken', arguments);
let value = this.tokenValue ? this.tokenValue.toString() : '';
tokens.push({type: type, value: value, range: [start, end]});
},
onComment: function (type, value, start, end) {
console.log('onComment', arguments);
type = type === 'SingleLine' ? 'Line' : type;
type = type === 'MultiLine' ? 'Block' : type;
comments.push({type: type, value: value, range: [start, end]});
}
});
console.log(tree); Output:
|
The two PRs hopefully can fix your use case. |
The #83 removed leading white space before token, so there are gaps between tokens. If this affects your reconstruction of the code, you might need to study the behaviour of other parsers. I never tried escodegen. For now, you might need to fill up the gap with synthesised token. |
Released v. 1.9.14. |
Works now. |
I'm trying to use attachComments functionality of escodegen, which gets tree,comments,tokens as arguments.
Unfortunately, it doesn't preserve the comments using Meriayh and it looks like the tokens types and range are different than what escodegen expect to have.
I tried the following scenario:
`
`
sourceCode:
//Comment1
const test1 = 1;
//Comment2
const test2 = 2;
res:
const test1 = 1;
As you can see, the comments are missing.
I use
"meriyah": "1.9.12"
"escodegen": "1.14.1"
BTW, it works with Esprima.
Thanks,
Niv
The text was updated successfully, but these errors were encountered: