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

line number information is not serialized #451

Open
nashid opened this issue Feb 15, 2022 · 3 comments
Open

line number information is not serialized #451

nashid opened this issue Feb 15, 2022 · 3 comments

Comments

@nashid
Copy link

nashid commented Feb 15, 2022

Should show line number information.

Test case:

let {parseScriptWithLocation, parseModuleWithLocation} = require("shift-parser");
let {tree, locations, comments} = parseScriptWithLocation("2 + 3 /* = 5 */");
console.log(JSON.stringify(locations))

Output = {}

Expected: Can show locations
Actual: Shows empty array {}

@bakkot
Copy link
Member

bakkot commented Feb 15, 2022

That's because it's a WeakMap. That's how WeakMaps work. What are you expecting?

@nashid
Copy link
Author

nashid commented Feb 15, 2022

I need to serialize the location information. For each AST node, I need to determine the start and end line number.

@bakkot
Copy link
Member

bakkot commented Feb 15, 2022

You can just stick the location information on the nodes, if you want:

let { tree, locations } = parseScriptWithLocation(`whatever`);
function addLocationInformation(node) {
  node.loc = locations.get(node);
  for (let field of Object.values(node)) {
    if (Array.isArray(field)) {
      field.forEach(addLocationInformation);
    } else if (typeof field === 'object' && field !== null) {
      addLocationInformation(field);
    }
  }
}
addLocationInformation(tree);

console.log(JSON.stringify(tree));

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

No branches or pull requests

2 participants