Skip to content

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

Merged
maxbrunsfeld merged 7 commits into
masterfrom
node-fields
May 6, 2019
Merged

maxbrunsfeld merged 7 commits into
masterfrom
node-fields

Conversation

@maxbrunsfeld

@maxbrunsfeld maxbrunsfeld commented Mar 29, 2019 •

Copy link
Copy Markdown
Contributor

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.

1 participant