Skip to content

Commit b66d19b

Browse files
Merge pull request #240 from rtsao/fix-type-imports
Fix edge cases with type imports
2 parents c6e56d4 + 45ea26b commit b66d19b

File tree

6 files changed

+243068
-242563
lines changed

6 files changed

+243068
-242563
lines changed

common/corpus/types.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ import {type UserID, type User} from "./User.js";
317317
import typeof {jimiguitar as GuitarT} from "./User.js";
318318
import type UserID, {addUser, removeUser} from './User.js';
319319

320+
import type from './User.js';
321+
import type, {addUser} from './User.js';
322+
import {type} from './User.js';
323+
import {addUser as type} from './User.js';
324+
import {type as addUser} from './User.js';
325+
import {type, addUser} from './User.js';
326+
import {addUser, type} from './User.js';
327+
import type, {addUser, type User} from './User.js';
328+
320329
---
321330

322331
(program
@@ -329,6 +338,29 @@ import type UserID, {addUser, removeUser} from './User.js';
329338
(import_statement
330339
(import_clause
331340
(named_imports (import_specifier (identifier) (identifier)))) (string (string_fragment)))
341+
(import_statement
342+
(import_clause (identifier)
343+
(named_imports (import_specifier (identifier)) (import_specifier (identifier)))) (string (string_fragment)))
344+
(import_statement
345+
(import_clause (identifier)) (string (string_fragment)))
346+
(import_statement
347+
(import_clause (identifier)
348+
(named_imports (import_specifier (identifier)))) (string (string_fragment)))
349+
(import_statement
350+
(import_clause
351+
(named_imports (import_specifier (identifier)))) (string (string_fragment)))
352+
(import_statement
353+
(import_clause
354+
(named_imports (import_specifier (identifier) (identifier)))) (string (string_fragment)))
355+
(import_statement
356+
(import_clause
357+
(named_imports (import_specifier (identifier) (identifier)))) (string (string_fragment)))
358+
(import_statement
359+
(import_clause
360+
(named_imports (import_specifier (identifier)) (import_specifier (identifier)))) (string (string_fragment)))
361+
(import_statement
362+
(import_clause
363+
(named_imports (import_specifier (identifier)) (import_specifier (identifier)))) (string (string_fragment)))
332364
(import_statement
333365
(import_clause (identifier)
334366
(named_imports (import_specifier (identifier)) (import_specifier (identifier)))) (string (string_fragment))))

common/define-grammar.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,32 @@ module.exports = function defineGrammar(dialect) {
262262
previous
263263
),
264264

265+
_import_identifier: $ => choice($.identifier, alias('type', $.identifier)),
266+
265267
import_specifier: ($, previous) => seq(
266268
optional(choice('type', 'typeof')),
267-
previous
269+
choice(
270+
field('name', $._import_identifier),
271+
seq(
272+
field('name', choice($._module_export_name, alias('type', $.identifier))),
273+
'as',
274+
field('alias', $._import_identifier)
275+
),
276+
)),
277+
278+
import_clause: ($, previous) => choice(
279+
$.namespace_import,
280+
$.named_imports,
281+
seq(
282+
$._import_identifier,
283+
optional(seq(
284+
',',
285+
choice(
286+
$.namespace_import,
287+
$.named_imports
288+
)
289+
))
290+
)
268291
),
269292

270293
import_statement: $ => seq(

tsx/src/grammar.json

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@
561561
"members": [
562562
{
563563
"type": "SYMBOL",
564-
"name": "identifier"
564+
"name": "_import_identifier"
565565
},
566566
{
567567
"type": "CHOICE",
@@ -721,7 +721,7 @@
721721
"name": "name",
722722
"content": {
723723
"type": "SYMBOL",
724-
"name": "identifier"
724+
"name": "_import_identifier"
725725
}
726726
},
727727
{
@@ -731,8 +731,22 @@
731731
"type": "FIELD",
732732
"name": "name",
733733
"content": {
734-
"type": "SYMBOL",
735-
"name": "_module_export_name"
734+
"type": "CHOICE",
735+
"members": [
736+
{
737+
"type": "SYMBOL",
738+
"name": "_module_export_name"
739+
},
740+
{
741+
"type": "ALIAS",
742+
"content": {
743+
"type": "STRING",
744+
"value": "type"
745+
},
746+
"named": true,
747+
"value": "identifier"
748+
}
749+
]
736750
}
737751
},
738752
{
@@ -744,7 +758,7 @@
744758
"name": "alias",
745759
"content": {
746760
"type": "SYMBOL",
747-
"name": "identifier"
761+
"name": "_import_identifier"
748762
}
749763
}
750764
]
@@ -7269,6 +7283,24 @@
72697283
}
72707284
]
72717285
},
7286+
"_import_identifier": {
7287+
"type": "CHOICE",
7288+
"members": [
7289+
{
7290+
"type": "SYMBOL",
7291+
"name": "identifier"
7292+
},
7293+
{
7294+
"type": "ALIAS",
7295+
"content": {
7296+
"type": "STRING",
7297+
"value": "type"
7298+
},
7299+
"named": true,
7300+
"value": "identifier"
7301+
}
7302+
]
7303+
},
72727304
"non_null_expression": {
72737305
"type": "PREC_LEFT",
72747306
"value": "unary",

0 commit comments

Comments
 (0)