You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently on a project that would greatly benefit from using field names to extract information on class/method names. I saw that you were already working on adding support to tree-sitter bindings for other languages. How are your plans regarding node? If you think it's feasible I'd also love to have a look at it.
Issue-Label Bot is automatically applying the label feature_request to this issue, with a confidence of 0.87. Please mark this comment with 👍 or 👎 to give our bot feedback!
Yeah, they are already supported in node. There are auto-generated methods like ‘getNameNode’ and ‘getDecoratorNodes’ based on the field names. See #39 for more info.
but neither of them are working. Also, I didn't find any type annotation in index.d.ts so TypeScript is giving me errors. How would I get the name field from a node (say a class_declaration node in Java)?
I was able to figure out you can access named fields like the following:
(in the case where a node has two fields: 'body' and 'condition')
node.fields // list of fields for a node (eg, ['bodyNode', 'conditionNode'])
node.bodyNode // get the named field node
IMO this is a bit of a weird API choice, wouldn't it have made more sense to have a dict of the fields accessible like: node.fields['body'] -- seems like it would be a lot more friendly to people writing tools using this lib.
Reacted by Jonas Hübotter, Anton Baumann, Parsa, Daisy Leigh Brenecki and Squidgical
I agree with you though, the current implementation is not very intuitive. Also, it lacks type hints for TypeScript. They would also be a lot easier to add if named nodes were to be accessible through a fields object.
Activity
issue-label-bot commentedon Nov 24, 2019
Issue-Label Bot is automatically applying the label
feature_requestto this issue, with a confidence of 0.87. Please mark this comment with 👍 or 👎 to give our bot feedback!Links: app homepage, dashboard and code for this bot.
maxbrunsfeld commentedon Nov 24, 2019
Yeah, they are already supported in node. There are auto-generated methods like ‘getNameNode’ and ‘getDecoratorNodes’ based on the field names. See #39 for more info.
jonhue commentedon Nov 26, 2019
@maxbrunsfeld Hmm... I tried
but neither of them are working. Also, I didn't find any type annotation in
index.d.tsso TypeScript is giving me errors. How would I get the name field from a node (say aclass_declarationnode in Java)?jonhue commentedon Dec 15, 2019
Any updates on this @maxbrunsfeld? 🙂
paked commentedon Dec 23, 2019
@jonhue
So maybe I've discovered something different than what @maxbrunsfeld is talking about here, but by looking at: https://github.com/tree-sitter/node-tree-sitter/pull/39/files#diff-168726dbe96b3ce427e7fedce31bb0bcR454-R478
I was able to figure out you can access named fields like the following:
(in the case where a node has two fields: 'body' and 'condition')
IMO this is a bit of a weird API choice, wouldn't it have made more sense to have a dict of the fields accessible like:
node.fields['body']-- seems like it would be a lot more friendly to people writing tools using this lib.jonhue commentedon Jan 3, 2020
@paked thanks! your solution works.
I agree with you though, the current implementation is not very intuitive. Also, it lacks type hints for TypeScript. They would also be a lot easier to add if named nodes were to be accessible through a
fieldsobject.jmbockhorst commentedon Sep 5, 2020
Here is a new, promising library that can generate types for a given tree-sitter syntax. Then you would be able to access fields using
node.bodyNodeor whatever, which in my opinion is better than having to use a dict which wouldn't have the correct types. https://github.com/asgerf/dts-tree-sitter