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

Auto-generate Node subclasses with getters for node fields #39

Merged
merged 7 commits into from
May 6, 2019

Conversation

maxbrunsfeld
Copy link
Contributor

@maxbrunsfeld maxbrunsfeld commented Mar 29, 2019

This PR exposes the new node fields APIs introduced in tree-sitter/tree-sitter#271.

When using a language that includes fields (like these branches of tree-sitter-javascript, tree-sitter-python and tree-sitter-bash), syntax tree nodes will have different subclasses. Each node will have getters for its different fields.

Example:

const tree = parser.parse(`
  class A {
    @autobind
    @something
    b(c, d) {
      return c + d;
    }
  }
`);

const classNode = tree.rootNode.firstChild;
const methodNode = classNode.bodyNode.firstNamedChild;

// Subclasses named after the node types
assert.equal(classNode.constructor.name, 'ClassDeclarationNode');
assert.equal(methodNode.constructor.name, 'MethodDefinitionNode');

// Plural getters that return arrays
const decoratorNodes = methodNode.decoratorNodes;
assert.deepEqual(decoratorNodes.map(_ => _.text), ['@autobind', '@something'])

// Singular getters that return single nodes
const paramsNode = methodNode.parametersNode;
const bodyNode = methodNode.bodyNode;
assert.equal(paramsNode.constructor.name, 'FormalParametersNode');
assert.equal(bodyNode.constructor.name, 'StatementBlockNode');

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 this pull request may close these issues.

None yet

1 participant