DataDog / datadog-static-analyzer

Datadog Static Analyzer
https://docs.datadoghq.com/static_analysis/
Apache License 2.0
100 stars 12 forks source link

Update op to only return named children #417

Closed jasonforal closed 3 months ago

jasonforal commented 3 months ago

What problem are you trying to solve?

Currently, when a rule requests the children of a tree-sitter node, they get both anonymous and named nodes, which is unintuitive and does not follow the semantics of what we currently do with the stella library.

function echo(a, b, c) {}

For the query:

((function_declaration
    (formal_parameters) @paramList))

The (anonymous + named) children of @paramList are: (, a, ,, b, ,, c, )

The named children are: a, b, c

What is your solution?

Use tree-sitter's named_children API instead of children.

Alternatives considered

What the reviewer should know

juli1 commented 3 months ago

Not opposed to the change but how will we get the unnamed nodes? It's generally not useful for the analysis but it's often required for calculating the fixes (like the place of a comma, etc). We need a way to get the unnamed nodes. What about we name a node.getChildren() and node.getNamedChildren() ?

jasonforal commented 3 months ago

Yes, I should've been more clear in the initial description. I just edited it, however, in sum:

This PR only matches the ddsa behavior to what we currently have with stella.

https://github.com/DataDog/datadog-static-analyzer/blob/e0f4f7685a69ac825a80f44055ff505dcf9c931f/crates/static-analysis-kernel/src/analysis/tree_sitter.rs#L317-L318