Skip to content

Commit 91eb3f2

Browse files
committed
fix(ast/estree): convert TSImportType argument field to Literal (#16109)
Fixes #16074. Convert `argument` field of `TSImportType` to `Literal` in TS-ESTree AST, and rename it to `source`. This follows the change made recently in TS-ESLint (typescript-eslint/typescript-eslint#11591).
1 parent 1d70bab commit 91eb3f2

File tree

16 files changed

+113
-125
lines changed

16 files changed

+113
-125
lines changed

apps/oxlint/src-js/generated/deserialize.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5213,16 +5213,21 @@ function deserializeTSImportType(pos) {
52135213
node = (parent = {
52145214
__proto__: NodeProto,
52155215
type: "TSImportType",
5216-
argument: null,
5216+
source: null,
52175217
options: null,
52185218
qualifier: null,
52195219
typeArguments: null,
52205220
start,
52215221
end,
52225222
range: [start, end],
52235223
parent,
5224-
});
5225-
node.argument = deserializeTSType(pos + 8);
5224+
}),
5225+
source = deserializeTSType(pos + 8);
5226+
if (source.type === "TSLiteralType") {
5227+
source = source.literal;
5228+
source.parent = parent;
5229+
}
5230+
node.source = source;
52265231
node.options = deserializeOptionBoxObjectExpression(pos + 24);
52275232
node.qualifier = deserializeOptionTSImportTypeQualifier(pos + 32);
52285233
node.typeArguments = deserializeOptionBoxTSTypeParameterInstantiation(pos + 48);

apps/oxlint/src-js/generated/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ export type TSTypeQueryExprName = TSImportType | TSTypeName;
15441544

15451545
export interface TSImportType extends Span {
15461546
type: "TSImportType";
1547-
argument: TSType;
1547+
source: StringLiteral;
15481548
options: ObjectExpression | null;
15491549
qualifier: TSImportTypeQualifier | null;
15501550
typeArguments: TSTypeParameterInstantiation | null;

crates/oxc_ast/src/ast/ts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,7 @@ pub enum TSTypeQueryExprName<'a> {
13811381
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree, UnstableAddress)]
13821382
pub struct TSImportType<'a> {
13831383
pub span: Span,
1384+
#[estree(rename = "source", via = TSImportTypeSource)]
13841385
pub argument: TSType<'a>,
13851386
pub options: Option<Box<'a, ObjectExpression<'a>>>,
13861387
pub qualifier: Option<TSImportTypeQualifier<'a>>,

crates/oxc_ast/src/generated/derive_estree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2982,7 +2982,7 @@ impl ESTree for TSImportType<'_> {
29822982
fn serialize<S: Serializer>(&self, serializer: S) {
29832983
let mut state = serializer.serialize_struct();
29842984
state.serialize_field("type", &JsonSafeString("TSImportType"));
2985-
state.serialize_field("argument", &self.argument);
2985+
state.serialize_field("source", &crate::serialize::ts::TSImportTypeSource(self));
29862986
state.serialize_field("options", &self.options);
29872987
state.serialize_field("qualifier", &self.qualifier);
29882988
state.serialize_field("typeArguments", &self.type_arguments);

crates/oxc_ast/src/serialize/ts.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,40 @@ impl ESTree for TSFunctionTypeParams<'_, '_> {
503503
}
504504
}
505505

506+
/// Serializer for `source` field of `TSImportType`.
507+
///
508+
/// * Field is named `argument` in Oxc AST.
509+
/// * Serialized as a `StringLiteral` - all other values are illegal syntax.
510+
#[ast_meta]
511+
#[estree(
512+
ts_type = "StringLiteral",
513+
raw_deser = "
514+
let source = DESER[TSType](POS_OFFSET.argument);
515+
if (source.type === 'TSLiteralType') {
516+
source = source.literal;
517+
if (PARENT) source.parent = parent;
518+
} else {
519+
// Should be unreachable - illegal syntax
520+
}
521+
source
522+
"
523+
)]
524+
pub struct TSImportTypeSource<'a, 'b>(pub &'b TSImportType<'a>);
525+
526+
impl ESTree for TSImportTypeSource<'_, '_> {
527+
fn serialize<S: Serializer>(&self, serializer: S) {
528+
let source = &self.0.argument;
529+
if let TSType::TSLiteralType(ts_lit_type) = source
530+
&& let TSLiteral::StringLiteral(str_lit) = &ts_lit_type.literal
531+
{
532+
str_lit.serialize(serializer);
533+
return;
534+
}
535+
// Should be unreachable - illegal syntax
536+
source.serialize(serializer);
537+
}
538+
}
539+
506540
/// Converter for [`TSParenthesizedType`].
507541
///
508542
/// In raw transfer, do not produce a `TSParenthesizedType` node in AST if `PRESERVE_PARENS` is false.

napi/parser/generated/deserialize/js.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3891,15 +3891,17 @@ function deserializeTSTypeQueryExprName(pos) {
38913891

38923892
function deserializeTSImportType(pos) {
38933893
let node = {
3894-
type: "TSImportType",
3895-
argument: null,
3896-
options: null,
3897-
qualifier: null,
3898-
typeArguments: null,
3899-
start: deserializeU32(pos),
3900-
end: deserializeU32(pos + 4),
3901-
};
3902-
node.argument = deserializeTSType(pos + 8);
3894+
type: "TSImportType",
3895+
source: null,
3896+
options: null,
3897+
qualifier: null,
3898+
typeArguments: null,
3899+
start: deserializeU32(pos),
3900+
end: deserializeU32(pos + 4),
3901+
},
3902+
source = deserializeTSType(pos + 8);
3903+
source.type === "TSLiteralType" && (source = source.literal);
3904+
node.source = source;
39033905
node.options = deserializeOptionBoxObjectExpression(pos + 24);
39043906
node.qualifier = deserializeOptionTSImportTypeQualifier(pos + 32);
39053907
node.typeArguments = deserializeOptionBoxTSTypeParameterInstantiation(pos + 48);

napi/parser/generated/deserialize/js_parent.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4545,15 +4545,20 @@ function deserializeTSImportType(pos) {
45454545
previousParent = parent,
45464546
node = (parent = {
45474547
type: "TSImportType",
4548-
argument: null,
4548+
source: null,
45494549
options: null,
45504550
qualifier: null,
45514551
typeArguments: null,
45524552
start,
45534553
end,
45544554
parent,
4555-
});
4556-
node.argument = deserializeTSType(pos + 8);
4555+
}),
4556+
source = deserializeTSType(pos + 8);
4557+
if (source.type === "TSLiteralType") {
4558+
source = source.literal;
4559+
source.parent = parent;
4560+
}
4561+
node.source = source;
45574562
node.options = deserializeOptionBoxObjectExpression(pos + 24);
45584563
node.qualifier = deserializeOptionTSImportTypeQualifier(pos + 32);
45594564
node.typeArguments = deserializeOptionBoxTSTypeParameterInstantiation(pos + 48);

napi/parser/generated/deserialize/js_range.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4293,15 +4293,17 @@ function deserializeTSImportType(pos) {
42934293
end = deserializeU32(pos + 4),
42944294
node = {
42954295
type: "TSImportType",
4296-
argument: null,
4296+
source: null,
42974297
options: null,
42984298
qualifier: null,
42994299
typeArguments: null,
43004300
start,
43014301
end,
43024302
range: [start, end],
4303-
};
4304-
node.argument = deserializeTSType(pos + 8);
4303+
},
4304+
source = deserializeTSType(pos + 8);
4305+
source.type === "TSLiteralType" && (source = source.literal);
4306+
node.source = source;
43054307
node.options = deserializeOptionBoxObjectExpression(pos + 24);
43064308
node.qualifier = deserializeOptionTSImportTypeQualifier(pos + 32);
43074309
node.typeArguments = deserializeOptionBoxTSTypeParameterInstantiation(pos + 48);

napi/parser/generated/deserialize/js_range_parent.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4770,16 +4770,21 @@ function deserializeTSImportType(pos) {
47704770
previousParent = parent,
47714771
node = (parent = {
47724772
type: "TSImportType",
4773-
argument: null,
4773+
source: null,
47744774
options: null,
47754775
qualifier: null,
47764776
typeArguments: null,
47774777
start,
47784778
end,
47794779
range: [start, end],
47804780
parent,
4781-
});
4782-
node.argument = deserializeTSType(pos + 8);
4781+
}),
4782+
source = deserializeTSType(pos + 8);
4783+
if (source.type === "TSLiteralType") {
4784+
source = source.literal;
4785+
source.parent = parent;
4786+
}
4787+
node.source = source;
47834788
node.options = deserializeOptionBoxObjectExpression(pos + 24);
47844789
node.qualifier = deserializeOptionTSImportTypeQualifier(pos + 32);
47854790
node.typeArguments = deserializeOptionBoxTSTypeParameterInstantiation(pos + 48);

napi/parser/generated/deserialize/ts.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4144,15 +4144,17 @@ function deserializeTSTypeQueryExprName(pos) {
41444144

41454145
function deserializeTSImportType(pos) {
41464146
let node = {
4147-
type: "TSImportType",
4148-
argument: null,
4149-
options: null,
4150-
qualifier: null,
4151-
typeArguments: null,
4152-
start: deserializeU32(pos),
4153-
end: deserializeU32(pos + 4),
4154-
};
4155-
node.argument = deserializeTSType(pos + 8);
4147+
type: "TSImportType",
4148+
source: null,
4149+
options: null,
4150+
qualifier: null,
4151+
typeArguments: null,
4152+
start: deserializeU32(pos),
4153+
end: deserializeU32(pos + 4),
4154+
},
4155+
source = deserializeTSType(pos + 8);
4156+
source.type === "TSLiteralType" && (source = source.literal);
4157+
node.source = source;
41564158
node.options = deserializeOptionBoxObjectExpression(pos + 24);
41574159
node.qualifier = deserializeOptionTSImportTypeQualifier(pos + 32);
41584160
node.typeArguments = deserializeOptionBoxTSTypeParameterInstantiation(pos + 48);

0 commit comments

Comments
 (0)