Skip to content

Add node fields - #43

Closed
maxbrunsfeld wants to merge 2 commits into
masterfrom
node-fields
Closed

maxbrunsfeld wants to merge 2 commits into
masterfrom
node-fields

Conversation

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Depends on tree-sitter/tree-sitter#271
See also tree-sitter/node-tree-sitter#39

This PR adds named fields to most node types in the grammar, which should make code analysis much easier:

const tree = parser.parse(`
  function foo() {
    TEST=1 ENV=2 ./run-it.sh --foo bar
  }
`)

const functionNode = tree.rootNode.firstChild;

// Nodes have fields for accessing their important childfren
const nameNode = functionNode.nameNode;
const bodyNode = functionNode.bodyNode;

// Each type of node has its own subclass
assert.equal(functionNode.constructor.name, 'FunctionDefinitionNode')
assert.equal(nameNode.constructor.name, 'WordNode')
assert.equal(bodyNode.constructor.name, 'CompoundStatementNode')

const commandNode = bodyNode.firstNamedChild;

// Getters for singular fields like `name` return single nodes
assert.equal(commandNode.nameNode.text, './run-it.sh')

// Getters for plural fields like `environment` and `argument` return arrays
assert.deepEqual(commandNode.environmentNodes.map(_ => _.leftNode.text), ['TEST', 'ENV'])
assert.deepEqual(commandNode.argumentNodes.map(_ => _.text), ['--foo', 'bar'])

@maxbrunsfeld

Copy link
Copy Markdown
Contributor Author

@mads-hartmann @skovhus This feature might simplify some of the analysis code in your language server.

It's not quite ready to merge yet; I think I want to merge tree-sitter/node-tree-sitter#39 first and get it integrated into Atom. I thought you might find it interesting tho.

@mads-hartmann

Copy link
Copy Markdown
Contributor

Definitely, this looks great @maxbrunsfeld 🙌

@skovhus

skovhus commented May 13, 2020

Copy link
Copy Markdown
Contributor

@maxbrunsfeld Anything we can do to land this?

@maxbrunsfeld

Copy link
Copy Markdown
Contributor Author

I can't remember why I did this work in two places, but this has been superseded by this commit on master: de690d8. Sorry for the confusion.

@amaanq
amaanq deleted the node-fields branch September 2, 2024 07:37
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.

3 participants