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

add start and end range of a node #17

Open
gre opened this issue Jun 14, 2017 · 1 comment
Open

add start and end range of a node #17

gre opened this issue Jun 14, 2017 · 1 comment

Comments

@gre
Copy link
Member

gre commented Jun 14, 2017

i was only able to locate the "start" via node.token.position, but figuring out the "end" is not trivial as far as I know

@gre
Copy link
Member Author

gre commented Jun 14, 2017

actually here is a workaround

const tokens = tokenize(code);
const ast = parse(tokens);

function decoratePosition(node, end) {
  node.loc = {
    start: node.token.position || 0,
    end,
  };
  node.children.forEach((child, i) => {
    const nextSibling = node.children[i + 1];
    decoratePosition(
      child,
      nextSibling && nextSibling.token && 'position' in nextSibling.token
        ? nextSibling.token.position -
            (nextSibling.token.preceding || [])
              .reduce((s, n) => s + (n.data || '').length, 0)
        : end
    );
  });
}
decoratePosition(ast, code.length);

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

1 participant