tree-sitter-java icon indicating copy to clipboard operation
tree-sitter-java copied to clipboard

field_access takes precedence over class_literal

Open danzat opened this issue 3 years ago • 5 comments

Problem description

The class literal pattern, e.g. String.class gets interpreted as a field access.

For the following snippet:

class C { Class x = String.class; }

Expected parse tree output (optional)

(program (class_declaration name: (identifier) body: (class_body (field_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) 
value: (class_literal (_unannotated_type)))))))

Actual parse tree output (optional)

(program (class_declaration name: (identifier) body: (class_body (field_declaration type: (type_identifier) declarator (variable_declarator name: (identifier) 
value: (field_access object: (identifier) field: (identifier)))))))

danzat avatar Sep 01 '22 11:09 danzat

Same problem here: https://github.com/returntocorp/semgrep/issues/6002

aryx avatar Sep 05 '22 15:09 aryx

I don't know if this used to work. @danzat can you bisect to a point where it stopped working? Or it never worked?

aryx avatar Sep 05 '22 15:09 aryx

Looks like it has never worked.

I have a fix that sets dynamic precedence to field_access and class_literal, how do you prefer to apply it? PR? Should I post the diff here?

Also, are there any formatting guidelines to the grammar.js file?

danzat avatar Sep 06 '22 06:09 danzat

PR sounds great to me. I think if there's some formatting issues, the commit hooks would complain.

aryx avatar Sep 06 '22 07:09 aryx

https://github.com/tree-sitter/tree-sitter-java/pull/125

danzat avatar Sep 13 '22 14:09 danzat