Commit 74cf572
committed
- [x] Change `source` field type in `TSImportType` from `TSType<'a>` to `StringLiteral<'a>` in `crates/oxc_ast/src/ast/ts.rs`
- [x] Remove `#[estree(via = TSImportTypeSource)]` attribute from the field
- [x] Run `just ast` to regenerate all derived code
- [x] Update the parser in `crates/oxc_parser/src/ts/types.rs` to parse a `StringLiteral` directly and emit errors for non-string literals
- [x] Delete the dead code `TSImportTypeSource` serializer from `crates/oxc_ast/src/serialize/ts.rs`
- [x] Add `ts_string_literal_expected` diagnostic (TS1141)
- [x] Update semantic snapshot for `import-type-with-type-params.snap`
- [x] Add test case for invalid import type source
- [x] Verify all tests pass
- [x] Simplify error recovery - just report error and create dummy string literal (per review feedback)
- [x] Fix spurious "Expected ')'" error by skipping invalid tokens in error recovery
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>AST: Make `source` field of `TSImportType` a `StringLiteral`</issue_title>
> <issue_description>Continuation of #16074.
>
> The expression inside `import(...)` in TS context (`TSImportType`) cannot legally be anything other than a string literal.
>
> Our parser is too liberal. We currently accept all kind of crazy stuff like:
>
> ```ts
> type A = typeof import(`react`);
> type B = typeof import(A);
> type C = typeof import(`${A} ${B}`);
> type D = typeof import(typeof import('react'));
> ```
>
> [Playground](https://playground.oxc.rs/#eNptU9tOGzEQ/ZXIQqJIqUSRKqFUfQjQSJVooaUqL31g1jsbDF7bHc/molX+nfEmdkDhyfaZ+5njXmk1UbwOOJqOvo7SxTcj0wZP/OGBEDQ/nHz55waPiwOPabFdHkYf9dPN6Ki/2OwzXB14vX0dDxWPT1KAGiuvJr2izqXDGsdq0oCNOFaNpxaYkQrCBC4muCAmeguM9RVqCwRsvIvFuHw0jDGAxgK14OZ2/9S+DYRxHxK1D2Jm6tJj3Vbe5pdu5ju3zVgFoJga6xWuGF2UuoniKPOAtX75G7kjd9NxNDXOOqdTZzlTKom0wFsgCc3oEPf3/LtjMpJQF0NE6ZqN/kbkaYdKC4mroQW5v6KqV13EP1DtZ2Ko7k3Nj2pyNlbo6pvm2jgZUtlG2g1SjXf281OpZhJDvzrPe5qe4uruHfh/et2SD1JLQfzoEGusJafsyUh780vftpBs1goqc5gyrMyyzPOLfQnrxF1FoJ+R72RnEp6dMwotbjvf1ffVE2q+JwiSI5OaCg29TlmIrDrpEOlNGK4CkmnRMdgbuQJ7uvXRbFeUGFJCadHallQGmqNoU2E8O/30WaoIzVfYSOKZp0sLMc4M2rps7XUVUacfyuytreGC/kCGGliY2q32fVX3KspEQQSC5EB0mcWofY1zHD6QS0Iokn2KtZevv6PcOc9DroxYnGffIUv+C0lR+aPI5D5c4wJL0mfE8FNWkQU2hIpovZ2JgFPEAqnyMROe5nFpUclUbhuB64G+BJfbJuGJZoisNi/WFa5W)
>
> TypeScript says these are all errors: [TS Playground](https://www.typescriptlang.org/play/?#code/C4TwDgpgBAglC8VSQPYDMoEsC2YUCdgAKAA3wgEMBjYEgSgG4AoZaAIQSXAnS1wOIxGLblADCnVrxx5CpACQBvGAF8oStivrNWUACKTu0-nKkYZAogHJy1YFbrCgA)
>
> ### AST change
>
> We should alter the type of the `source` field from `TSType<'a>` to `StringLiteral<'a>`:
>
> https://github.com/oxc-project/oxc/blob/35d0ee491cbcd271ebceaac7e25cff0490ded5c4/crates/oxc_ast/src/ast/ts.rs#L1382-L1389
>
> ```diff
> pub struct TSImportType<'a> {
> pub span: Span,
> - #[estree(via = TSImportTypeSource)]
> - pub source: TSType<'a>,
> + pub source: StringLiteral<'a>,
> pub options: Option<Box<'a, ObjectExpression<'a>>>,
> pub qualifier: Option<TSImportTypeQualifier<'a>>,
> pub type_arguments: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
> }
> ```
>
> ### Parser changes
>
> If we change the AST first and run `just ast` to regenerate traits code, then all the places which rely on the type of `source` field will become apparent. We'll then need to alter the parser to throw an error if it encounters something that isn't a string literal.
>
> ### `ESTree` serialization
>
> Currently there's a custom deserializer to alter the AST on JS side from `TSType` to `StringLiteral`:
>
> https://github.com/oxc-project/oxc/blob/91eb3f2be509cef11cffeff51fac61a3a4fbd12c/crates/oxc_ast/src/serialize/ts.rs#L506-L538
>
> Once we've changed the field type in AST, this code can be deleted - it's dead code once `#[estree(via = TSImportTypeSource)]` attribute is removed from the `source` field in AST struct.</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
- Fixes #16111
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey).
1 parent c05db06 commit 74cf572
File tree
31 files changed
+235
-181
lines changed- apps/oxlint/src-js/generated
- crates
- oxc_ast_visit/src/generated
- oxc_ast/src
- ast
- generated
- serialize
- oxc_codegen/src
- oxc_formatter/src/ast_nodes/generated
- oxc_parser/src
- ts
- oxc_semantic/tests/fixtures/typescript-eslint/type-declaration
- oxc_traverse/src/generated
- napi/parser/generated
- deserialize
- lazy
- tasks/coverage
- misc/fail
- snapshots
31 files changed
+235
-181
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5221 | 5221 | | |
5222 | 5222 | | |
5223 | 5223 | | |
5224 | | - | |
5225 | | - | |
5226 | | - | |
5227 | | - | |
5228 | | - | |
5229 | | - | |
5230 | | - | |
5231 | | - | |
5232 | | - | |
5233 | | - | |
| 5224 | + | |
| 5225 | + | |
| 5226 | + | |
| 5227 | + | |
| 5228 | + | |
5234 | 5229 | | |
5235 | 5230 | | |
5236 | 5231 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1381 | 1381 | | |
1382 | 1382 | | |
1383 | 1383 | | |
1384 | | - | |
1385 | | - | |
| 1384 | + | |
1386 | 1385 | | |
1387 | 1386 | | |
1388 | 1387 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1449 | 1449 | | |
1450 | 1450 | | |
1451 | 1451 | | |
1452 | | - | |
| 1452 | + | |
1453 | 1453 | | |
1454 | 1454 | | |
1455 | 1455 | | |
1456 | | - | |
1457 | | - | |
1458 | | - | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
1459 | 1459 | | |
1460 | 1460 | | |
1461 | 1461 | | |
| |||
3065 | 3065 | | |
3066 | 3066 | | |
3067 | 3067 | | |
3068 | | - | |
| 3068 | + | |
3069 | 3069 | | |
3070 | 3070 | | |
3071 | 3071 | | |
3072 | | - | |
3073 | | - | |
3074 | | - | |
| 3072 | + | |
| 3073 | + | |
| 3074 | + | |
3075 | 3075 | | |
3076 | 3076 | | |
3077 | 3077 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10629 | 10629 | | |
10630 | 10630 | | |
10631 | 10631 | | |
10632 | | - | |
| 10632 | + | |
10633 | 10633 | | |
10634 | 10634 | | |
10635 | 10635 | | |
| |||
14024 | 14024 | | |
14025 | 14025 | | |
14026 | 14026 | | |
14027 | | - | |
| 14027 | + | |
14028 | 14028 | | |
14029 | 14029 | | |
14030 | 14030 | | |
| |||
14057 | 14057 | | |
14058 | 14058 | | |
14059 | 14059 | | |
14060 | | - | |
| 14060 | + | |
14061 | 14061 | | |
14062 | 14062 | | |
14063 | 14063 | | |
| |||
14090 | 14090 | | |
14091 | 14091 | | |
14092 | 14092 | | |
14093 | | - | |
| 14093 | + | |
14094 | 14094 | | |
14095 | 14095 | | |
14096 | 14096 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2638 | 2638 | | |
2639 | 2639 | | |
2640 | 2640 | | |
2641 | | - | |
| 2641 | + | |
2642 | 2642 | | |
2643 | 2643 | | |
2644 | 2644 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2982 | 2982 | | |
2983 | 2983 | | |
2984 | 2984 | | |
2985 | | - | |
| 2985 | + | |
2986 | 2986 | | |
2987 | 2987 | | |
2988 | 2988 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
503 | 503 | | |
504 | 504 | | |
505 | 505 | | |
506 | | - | |
507 | | - | |
508 | | - | |
509 | | - | |
510 | | - | |
511 | | - | |
512 | | - | |
513 | | - | |
514 | | - | |
515 | | - | |
516 | | - | |
517 | | - | |
518 | | - | |
519 | | - | |
520 | | - | |
521 | | - | |
522 | | - | |
523 | | - | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | | - | |
539 | 506 | | |
540 | 507 | | |
541 | 508 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3955 | 3955 | | |
3956 | 3956 | | |
3957 | 3957 | | |
3958 | | - | |
| 3958 | + | |
3959 | 3959 | | |
3960 | 3960 | | |
3961 | 3961 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4164 | 4164 | | |
4165 | 4165 | | |
4166 | 4166 | | |
4167 | | - | |
| 4167 | + | |
4168 | 4168 | | |
4169 | 4169 | | |
4170 | 4170 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3515 | 3515 | | |
3516 | 3516 | | |
3517 | 3517 | | |
3518 | | - | |
| 3518 | + | |
3519 | 3519 | | |
3520 | 3520 | | |
3521 | 3521 | | |
| |||
0 commit comments