tree-sitter-javascript
tree-sitter-javascript copied to clipboard
Invalid variable names are not marked as errors
The following piece of code is invalid but it is parsed correctly:
let let = 5;
let const = 5;
const const = 2;
const let = 4;
const class = 3;
const if = 3;
const true = 2;
var const = 3;
var class = 2;
var in = 1;
let in = 2;
const in = 3;
The output of tree-sitter
playground is the following:
program [1, 0] - [13, 0]
lexical_declaration [1, 0] - [1, 12]
variable_declarator [1, 4] - [1, 11]
name: identifier [1, 4] - [1, 7]
value: number [1, 10] - [1, 11]
lexical_declaration [2, 0] - [2, 14]
variable_declarator [2, 4] - [2, 13]
name: identifier [2, 4] - [2, 9]
value: number [2, 12] - [2, 13]
lexical_declaration [3, 0] - [3, 16]
variable_declarator [3, 6] - [3, 15]
name: identifier [3, 6] - [3, 11]
value: number [3, 14] - [3, 15]
lexical_declaration [4, 0] - [4, 14]
variable_declarator [4, 6] - [4, 13]
name: identifier [4, 6] - [4, 9]
value: number [4, 12] - [4, 13]
lexical_declaration [6, 0] - [6, 16]
variable_declarator [6, 6] - [6, 15]
name: identifier [6, 6] - [6, 11]
value: number [6, 14] - [6, 15]
lexical_declaration [7, 0] - [7, 13]
variable_declarator [7, 6] - [7, 12]
name: identifier [7, 6] - [7, 8]
value: number [7, 11] - [7, 12]
lexical_declaration [8, 0] - [8, 15]
variable_declarator [8, 6] - [8, 14]
name: identifier [8, 6] - [8, 10]
value: number [8, 13] - [8, 14]
variable_declaration [10, 0] - [10, 14]
variable_declarator [10, 4] - [10, 13]
name: identifier [10, 4] - [10, 9]
value: number [10, 12] - [10, 13]
variable_declaration [11, 0] - [11, 14]
variable_declarator [11, 4] - [11, 13]
name: identifier [11, 4] - [11, 9]
value: number [11, 12] - [11, 13]
Each of these lines should be a syntax error.
A more special case is let
: var let = 2
is valid, but let let = 2
or const let = 2
is not.
Also let in
is a syntax error because it's an incomplete binary_expression
unlike const in
or let in
which are errors because in
is an invalid identifier.
The other identifiers I've listed are invalid in var
/let
/const
.
I believe it will be possible to fix this issue only once https://github.com/tree-sitter/tree-sitter/pull/246 is merged. Right now there is no way to say "an identifier but not a keyword" in the grammar.