@willbooster/tree-sitter-javascript 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/grammar.js CHANGED
@@ -8,6 +8,46 @@
8
8
  /// <reference types="tree-sitter-cli/dsl" />
9
9
  // @ts-check
10
10
 
11
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words
12
+ const RESERVED_WORDS = [
13
+ 'break',
14
+ 'case',
15
+ 'catch',
16
+ 'class',
17
+ 'const',
18
+ 'continue',
19
+ 'debugger',
20
+ 'default',
21
+ 'delete',
22
+ 'do',
23
+ 'else',
24
+ 'export',
25
+ 'extends',
26
+ 'false',
27
+ 'finally',
28
+ 'for',
29
+ 'function',
30
+ 'if',
31
+ 'import',
32
+ 'in',
33
+ 'instanceof',
34
+ 'new',
35
+ 'null',
36
+ 'return',
37
+ 'super',
38
+ 'switch',
39
+ 'this',
40
+ 'throw',
41
+ 'true',
42
+ 'try',
43
+ 'typeof',
44
+ 'var',
45
+ 'void',
46
+ 'while',
47
+ 'with',
48
+ ];
49
+ const NAMED_RESERVED_WORDS = new Set(['this', 'super', 'true', 'false', 'null']);
50
+
11
51
  // oxlint-disable-next-line unicorn/prefer-module -- This package is CommonJS, so tree-sitter loads grammar.js as CommonJS.
12
52
  module.exports = grammar({
13
53
  name: 'javascript',
@@ -34,44 +74,7 @@ module.exports = grammar({
34
74
  extras: ($) => [$.comment, $.html_comment, /[\s\p{Zs}\uFEFF\u2028\u2029\u2060\u200B]/u],
35
75
 
36
76
  reserved: {
37
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words
38
- global: () => [
39
- 'break',
40
- 'case',
41
- 'catch',
42
- 'class',
43
- 'const',
44
- 'continue',
45
- 'debugger',
46
- 'default',
47
- 'delete',
48
- 'do',
49
- 'else',
50
- 'export',
51
- 'extends',
52
- 'false',
53
- 'finally',
54
- 'for',
55
- 'function',
56
- 'if',
57
- 'import',
58
- 'in',
59
- 'instanceof',
60
- 'new',
61
- 'null',
62
- 'return',
63
- 'super',
64
- 'switch',
65
- 'this',
66
- 'throw',
67
- 'true',
68
- 'try',
69
- 'typeof',
70
- 'var',
71
- 'void',
72
- 'while',
73
- 'with',
74
- ],
77
+ global: () => RESERVED_WORDS,
75
78
  properties: () => [],
76
79
  },
77
80
 
@@ -129,6 +132,7 @@ module.exports = grammar({
129
132
  ],
130
133
 
131
134
  conflicts: ($) => [
135
+ [$._local_export_specifier, $._module_export_name],
132
136
  [$.primary_expression, $._property_name],
133
137
  [$.primary_expression, $.await_expression],
134
138
  [$.primary_expression, $.await_expression, $._property_name],
@@ -167,7 +171,7 @@ module.exports = grammar({
167
171
  seq('*', $._from_clause),
168
172
  seq($.namespace_export, $._from_clause),
169
173
  seq($.export_clause, $._from_clause),
170
- $.export_clause
174
+ alias($._local_export_clause, $.export_clause)
171
175
  ),
172
176
  $._semicolon
173
177
  ),
@@ -188,7 +192,22 @@ module.exports = grammar({
188
192
  export_specifier: ($) =>
189
193
  seq(field('name', $._module_export_name), optional(seq('as', field('alias', $._module_export_name)))),
190
194
 
191
- _module_export_name: ($) => choice($.identifier, $.string, 'default'),
195
+ // Without a `from` clause, each name refers to a local binding, which cannot be a reserved word.
196
+ _local_export_clause: ($) =>
197
+ seq('{', commaSep(alias($._local_export_specifier, $.export_specifier)), optional(','), '}'),
198
+
199
+ _local_export_specifier: ($) =>
200
+ seq(field('name', choice($.identifier, $.string)), optional(seq('as', field('alias', $._module_export_name)))),
201
+
202
+ // Reserved words are listed as keywords rather than allowed through the property word set, because the word set
203
+ // would also apply to the local bindings that share these positions until `as` or `from` (e.g. `import { if }`).
204
+ _module_export_name: ($) =>
205
+ choice(
206
+ $.identifier,
207
+ $.string,
208
+ // `this`, `super`, `true`, `false`, and `null` are named rules, and a bare string would make them non-terminals.
209
+ alias(choice(...RESERVED_WORDS.map((word) => (NAMED_RESERVED_WORDS.has(word) ? $[word] : word))), $.identifier)
210
+ ),
192
211
 
193
212
  declaration: ($) =>
194
213
  choice(
@@ -547,15 +566,18 @@ module.exports = grammar({
547
566
 
548
567
  jsx_identifier: () => /[a-zA-Z_$][a-zA-Z\d_$]*-[a-zA-Z\d_$-]*/,
549
568
 
550
- _jsx_identifier: ($) => choice(alias($.jsx_identifier, $.identifier), $.identifier),
569
+ _jsx_identifier: ($) => choice(alias($.jsx_identifier, $.identifier), reserved('properties', $.identifier)),
551
570
 
552
571
  nested_identifier: ($) =>
553
572
  prec(
554
573
  'member',
555
574
  seq(
556
- field('object', choice($.identifier, alias($.nested_identifier, $.member_expression))),
575
+ field(
576
+ 'object',
577
+ choice(reserved('properties', $.identifier), alias($.nested_identifier, $.member_expression))
578
+ ),
557
579
  '.',
558
- field('property', alias($.identifier, $.property_identifier))
580
+ field('property', alias(reserved('properties', $.identifier), $.property_identifier))
559
581
  )
560
582
  ),
561
583
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-javascript",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "JavaScript grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
@@ -202,3 +202,17 @@
202
202
  "with"
203
203
  "yield"
204
204
  ] @keyword
205
+
206
+ ; `default` as a module export name is an identifier node.
207
+ (export_specifier
208
+ name: (identifier) @keyword
209
+ (#eq? @keyword "default"))
210
+ (export_specifier
211
+ alias: (identifier) @keyword
212
+ (#eq? @keyword "default"))
213
+ (import_specifier
214
+ name: (identifier) @keyword
215
+ (#eq? @keyword "default"))
216
+ (namespace_export
217
+ (identifier) @keyword
218
+ (#eq? @keyword "default"))
@@ -1094,10 +1094,6 @@
1094
1094
  "multiple": false,
1095
1095
  "required": false,
1096
1096
  "types": [
1097
- {
1098
- "type": "default",
1099
- "named": false
1100
- },
1101
1097
  {
1102
1098
  "type": "identifier",
1103
1099
  "named": true
@@ -1112,10 +1108,6 @@
1112
1108
  "multiple": false,
1113
1109
  "required": true,
1114
1110
  "types": [
1115
- {
1116
- "type": "default",
1117
- "named": false
1118
- },
1119
1111
  {
1120
1112
  "type": "identifier",
1121
1113
  "named": true
@@ -1727,10 +1719,6 @@
1727
1719
  "multiple": false,
1728
1720
  "required": true,
1729
1721
  "types": [
1730
- {
1731
- "type": "default",
1732
- "named": false
1733
- },
1734
1722
  {
1735
1723
  "type": "identifier",
1736
1724
  "named": true
@@ -2186,7 +2174,7 @@
2186
2174
  "fields": {},
2187
2175
  "children": {
2188
2176
  "multiple": false,
2189
- "required": false,
2177
+ "required": true,
2190
2178
  "types": [
2191
2179
  {
2192
2180
  "type": "identifier",
Binary file
package/tree-sitter.json CHANGED
@@ -16,7 +16,7 @@
16
16
  }
17
17
  ],
18
18
  "metadata": {
19
- "version": "1.0.7",
19
+ "version": "1.0.8",
20
20
  "license": "MIT",
21
21
  "description": "JavaScript grammar for tree-sitter",
22
22
  "authors": [