Skip to content

Commit caf45c5

Browse files
committed
Support string aliases for imports/exports
1 parent 936d976 commit caf45c5

File tree

5 files changed

+36343
-34498
lines changed

5 files changed

+36343
-34498
lines changed

grammar.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ module.exports = grammar({
103103
seq(
104104
'export',
105105
choice(
106-
seq('*', $._from_clause, $._semicolon),
107-
seq(alias($.namespace_import_export, $.namespace_export), $._from_clause, $._semicolon),
108-
seq($.export_clause, $._from_clause, $._semicolon),
109-
seq($.export_clause, $._semicolon)
110-
)
106+
seq('*', $._from_clause),
107+
seq($.namespace_export, $._from_clause),
108+
seq($.export_clause, $._from_clause),
109+
$.export_clause,
110+
),
111+
$._semicolon,
111112
),
112113
seq(
113114
repeat(field('decorator', $.decorator)),
@@ -128,21 +129,30 @@ module.exports = grammar({
128129
)
129130
),
130131

132+
namespace_export: $ => seq(
133+
'*', 'as', $._module_export_name
134+
),
135+
131136
export_clause: $ => seq(
132137
'{',
133-
commaSep(alias($._import_export_specifier, $.export_specifier)),
138+
commaSep($.export_specifier),
134139
optional(','),
135140
'}'
136141
),
137142

138-
_import_export_specifier: $ => seq(
139-
field('name', $.identifier),
143+
export_specifier: $ => seq(
144+
field('name', $._module_export_name),
140145
optional(seq(
141146
'as',
142-
field('alias', $.identifier)
147+
field('alias', $._module_export_name)
143148
))
144149
),
145150

151+
_module_export_name: $ => choice(
152+
$.identifier,
153+
$.string,
154+
),
155+
146156
declaration: $ => choice(
147157
$.function_declaration,
148158
$.generator_function_declaration,
@@ -167,14 +177,14 @@ module.exports = grammar({
167177
),
168178

169179
import_clause: $ => choice(
170-
alias($.namespace_import_export, $.namespace_import),
180+
$.namespace_import,
171181
$.named_imports,
172182
seq(
173183
$.identifier,
174184
optional(seq(
175185
',',
176186
choice(
177-
alias($.namespace_import_export, $.namespace_import),
187+
$.namespace_import,
178188
$.named_imports
179189
)
180190
))
@@ -185,17 +195,26 @@ module.exports = grammar({
185195
"from", field('source', $.string)
186196
),
187197

188-
namespace_import_export: $ => seq(
198+
namespace_import: $ => seq(
189199
"*", "as", $.identifier
190200
),
191201

192202
named_imports: $ => seq(
193203
'{',
194-
commaSep(alias($._import_export_specifier, $.import_specifier)),
204+
commaSep($.import_specifier),
195205
optional(','),
196206
'}'
197207
),
198208

209+
import_specifier: $ => choice(
210+
field('name', $.identifier),
211+
seq(
212+
field('name', $._module_export_name),
213+
'as',
214+
field('alias', $.identifier)
215+
),
216+
),
217+
199218
//
200219
// Statements
201220
//

src/grammar.json

Lines changed: 92 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,19 @@
5353
{
5454
"type": "SYMBOL",
5555
"name": "_from_clause"
56-
},
57-
{
58-
"type": "SYMBOL",
59-
"name": "_semicolon"
6056
}
6157
]
6258
},
6359
{
6460
"type": "SEQ",
6561
"members": [
66-
{
67-
"type": "ALIAS",
68-
"content": {
69-
"type": "SYMBOL",
70-
"name": "namespace_import_export"
71-
},
72-
"named": true,
73-
"value": "namespace_export"
74-
},
7562
{
7663
"type": "SYMBOL",
77-
"name": "_from_clause"
64+
"name": "namespace_export"
7865
},
7966
{
8067
"type": "SYMBOL",
81-
"name": "_semicolon"
68+
"name": "_from_clause"
8269
}
8370
]
8471
},
@@ -92,27 +79,18 @@
9279
{
9380
"type": "SYMBOL",
9481
"name": "_from_clause"
95-
},
96-
{
97-
"type": "SYMBOL",
98-
"name": "_semicolon"
9982
}
10083
]
10184
},
10285
{
103-
"type": "SEQ",
104-
"members": [
105-
{
106-
"type": "SYMBOL",
107-
"name": "export_clause"
108-
},
109-
{
110-
"type": "SYMBOL",
111-
"name": "_semicolon"
112-
}
113-
]
86+
"type": "SYMBOL",
87+
"name": "export_clause"
11488
}
11589
]
90+
},
91+
{
92+
"type": "SYMBOL",
93+
"name": "_semicolon"
11694
}
11795
]
11896
},
@@ -190,6 +168,23 @@
190168
}
191169
]
192170
},
171+
"namespace_export": {
172+
"type": "SEQ",
173+
"members": [
174+
{
175+
"type": "STRING",
176+
"value": "*"
177+
},
178+
{
179+
"type": "STRING",
180+
"value": "as"
181+
},
182+
{
183+
"type": "SYMBOL",
184+
"name": "_module_export_name"
185+
}
186+
]
187+
},
193188
"export_clause": {
194189
"type": "SEQ",
195190
"members": [
@@ -204,13 +199,8 @@
204199
"type": "SEQ",
205200
"members": [
206201
{
207-
"type": "ALIAS",
208-
"content": {
209-
"type": "SYMBOL",
210-
"name": "_import_export_specifier"
211-
},
212-
"named": true,
213-
"value": "export_specifier"
202+
"type": "SYMBOL",
203+
"name": "export_specifier"
214204
},
215205
{
216206
"type": "REPEAT",
@@ -222,13 +212,8 @@
222212
"value": ","
223213
},
224214
{
225-
"type": "ALIAS",
226-
"content": {
227-
"type": "SYMBOL",
228-
"name": "_import_export_specifier"
229-
},
230-
"named": true,
231-
"value": "export_specifier"
215+
"type": "SYMBOL",
216+
"name": "export_specifier"
232217
}
233218
]
234219
}
@@ -258,15 +243,15 @@
258243
}
259244
]
260245
},
261-
"_import_export_specifier": {
246+
"export_specifier": {
262247
"type": "SEQ",
263248
"members": [
264249
{
265250
"type": "FIELD",
266251
"name": "name",
267252
"content": {
268253
"type": "SYMBOL",
269-
"name": "identifier"
254+
"name": "_module_export_name"
270255
}
271256
},
272257
{
@@ -284,7 +269,7 @@
284269
"name": "alias",
285270
"content": {
286271
"type": "SYMBOL",
287-
"name": "identifier"
272+
"name": "_module_export_name"
288273
}
289274
}
290275
]
@@ -296,6 +281,19 @@
296281
}
297282
]
298283
},
284+
"_module_export_name": {
285+
"type": "CHOICE",
286+
"members": [
287+
{
288+
"type": "SYMBOL",
289+
"name": "identifier"
290+
},
291+
{
292+
"type": "SYMBOL",
293+
"name": "string"
294+
}
295+
]
296+
},
299297
"declaration": {
300298
"type": "CHOICE",
301299
"members": [
@@ -371,13 +369,8 @@
371369
"type": "CHOICE",
372370
"members": [
373371
{
374-
"type": "ALIAS",
375-
"content": {
376-
"type": "SYMBOL",
377-
"name": "namespace_import_export"
378-
},
379-
"named": true,
380-
"value": "namespace_import"
372+
"type": "SYMBOL",
373+
"name": "namespace_import"
381374
},
382375
{
383376
"type": "SYMBOL",
@@ -404,13 +397,8 @@
404397
"type": "CHOICE",
405398
"members": [
406399
{
407-
"type": "ALIAS",
408-
"content": {
409-
"type": "SYMBOL",
410-
"name": "namespace_import_export"
411-
},
412-
"named": true,
413-
"value": "namespace_import"
400+
"type": "SYMBOL",
401+
"name": "namespace_import"
414402
},
415403
{
416404
"type": "SYMBOL",
@@ -446,7 +434,7 @@
446434
}
447435
]
448436
},
449-
"namespace_import_export": {
437+
"namespace_import": {
450438
"type": "SEQ",
451439
"members": [
452440
{
@@ -477,13 +465,8 @@
477465
"type": "SEQ",
478466
"members": [
479467
{
480-
"type": "ALIAS",
481-
"content": {
482-
"type": "SYMBOL",
483-
"name": "_import_export_specifier"
484-
},
485-
"named": true,
486-
"value": "import_specifier"
468+
"type": "SYMBOL",
469+
"name": "import_specifier"
487470
},
488471
{
489472
"type": "REPEAT",
@@ -495,13 +478,8 @@
495478
"value": ","
496479
},
497480
{
498-
"type": "ALIAS",
499-
"content": {
500-
"type": "SYMBOL",
501-
"name": "_import_export_specifier"
502-
},
503-
"named": true,
504-
"value": "import_specifier"
481+
"type": "SYMBOL",
482+
"name": "import_specifier"
505483
}
506484
]
507485
}
@@ -531,6 +509,44 @@
531509
}
532510
]
533511
},
512+
"import_specifier": {
513+
"type": "CHOICE",
514+
"members": [
515+
{
516+
"type": "FIELD",
517+
"name": "name",
518+
"content": {
519+
"type": "SYMBOL",
520+
"name": "identifier"
521+
}
522+
},
523+
{
524+
"type": "SEQ",
525+
"members": [
526+
{
527+
"type": "FIELD",
528+
"name": "name",
529+
"content": {
530+
"type": "SYMBOL",
531+
"name": "_module_export_name"
532+
}
533+
},
534+
{
535+
"type": "STRING",
536+
"value": "as"
537+
},
538+
{
539+
"type": "FIELD",
540+
"name": "alias",
541+
"content": {
542+
"type": "SYMBOL",
543+
"name": "identifier"
544+
}
545+
}
546+
]
547+
}
548+
]
549+
},
534550
"statement": {
535551
"type": "CHOICE",
536552
"members": [

0 commit comments

Comments
 (0)