Skip to content

Commit bf9ae3b

Browse files
committed
refactor(ast/estree): remove custom serializer for TSImportTypeQualifier (#12961)
Follow-on after #12801. Remove the custom serializer for `TSImportTypeQualifier`. All we need is to rename `TSImportTypeQualifiedName` to `TSQualifiedName`, so we can use `#[estree(rename = "TSQualifiedName")]`, and let the codegen do the rest.
1 parent e5e2496 commit bf9ae3b

File tree

6 files changed

+9
-221
lines changed

6 files changed

+9
-221
lines changed

crates/oxc_ast/src/ast/ts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,6 @@ pub struct TSImportType<'a> {
13591359
pub span: Span,
13601360
pub argument: TSType<'a>,
13611361
pub options: Option<Box<'a, ObjectExpression<'a>>>,
1362-
#[estree(via = TSImportTypeQualifierConverter)]
13631362
pub qualifier: Option<TSImportTypeQualifier<'a>>,
13641363
pub type_arguments: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
13651364
}
@@ -1386,6 +1385,7 @@ pub enum TSImportTypeQualifier<'a> {
13861385
#[ast(visit)]
13871386
#[derive(Debug)]
13881387
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
1388+
#[estree(rename = "TSQualifiedName")]
13891389
pub struct TSImportTypeQualifiedName<'a> {
13901390
pub span: Span,
13911391
pub left: TSImportTypeQualifier<'a>,

crates/oxc_ast/src/generated/derive_estree.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,10 +2977,7 @@ impl ESTree for TSImportType<'_> {
29772977
state.serialize_field("type", &JsonSafeString("TSImportType"));
29782978
state.serialize_field("argument", &self.argument);
29792979
state.serialize_field("options", &self.options);
2980-
state.serialize_field(
2981-
"qualifier",
2982-
&crate::serialize::ts::TSImportTypeQualifierConverter(self),
2983-
);
2980+
state.serialize_field("qualifier", &self.qualifier);
29842981
state.serialize_field("typeArguments", &self.type_arguments);
29852982
state.serialize_span(self.span);
29862983
state.end();
@@ -2999,7 +2996,7 @@ impl ESTree for TSImportTypeQualifier<'_> {
29992996
impl ESTree for TSImportTypeQualifiedName<'_> {
30002997
fn serialize<S: Serializer>(&self, serializer: S) {
30012998
let mut state = serializer.serialize_struct();
3002-
state.serialize_field("type", &JsonSafeString("TSImportTypeQualifiedName"));
2999+
state.serialize_field("type", &JsonSafeString("TSQualifiedName"));
30033000
state.serialize_field("left", &self.left);
30043001
state.serialize_field("right", &self.right);
30053002
state.serialize_span(self.span);

crates/oxc_ast/src/serialize/ts.rs

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -401,118 +401,3 @@ impl ESTree for TSFunctionTypeParams<'_, '_> {
401401
Concat2(&fn_type.this_param, fn_type.params.as_ref()).serialize(serializer);
402402
}
403403
}
404-
405-
/// Serializer for `qualifier` field of `TSImportType`.
406-
///
407-
/// Our AST represents the qualifier as `TSImportTypeQualifier` with `IdentifierName` nodes.
408-
/// TS-ESTree represents it as `TSQualifiedName` with `Identifier` nodes.
409-
#[ast_meta]
410-
#[estree(
411-
ts_type = "TSQualifiedName | IdentifierName | null",
412-
raw_deser = "
413-
let qualifier = DESER[Option<TSImportTypeQualifier>](POS_OFFSET.qualifier);
414-
if (qualifier !== null) {
415-
if (qualifier.type === 'IdentifierName') {
416-
qualifier = {
417-
type: 'Identifier',
418-
decorators: [],
419-
name: qualifier.name,
420-
optional: false,
421-
typeAnnotation: null,
422-
start: qualifier.start,
423-
end: qualifier.end,
424-
};
425-
} else if (qualifier.type === 'TSImportTypeQualifiedName') {
426-
// Convert TSImportTypeQualifiedName to TSQualifiedName
427-
const convertQualifier = (q) => {
428-
if (q.type === 'IdentifierName') {
429-
return {
430-
type: 'Identifier',
431-
decorators: [],
432-
name: q.name,
433-
optional: false,
434-
typeAnnotation: null,
435-
start: q.start,
436-
end: q.end,
437-
};
438-
} else if (q.type === 'TSImportTypeQualifiedName') {
439-
return {
440-
type: 'TSQualifiedName',
441-
left: convertQualifier(q.left),
442-
right: {
443-
type: 'Identifier',
444-
decorators: [],
445-
name: q.right.name,
446-
optional: false,
447-
typeAnnotation: null,
448-
start: q.right.start,
449-
end: q.right.end,
450-
},
451-
start: q.start,
452-
end: q.end,
453-
};
454-
}
455-
return q;
456-
};
457-
qualifier = convertQualifier(qualifier);
458-
}
459-
}
460-
qualifier
461-
"
462-
)]
463-
pub struct TSImportTypeQualifierConverter<'a, 'b>(pub &'b TSImportType<'a>);
464-
465-
impl ESTree for TSImportTypeQualifierConverter<'_, '_> {
466-
fn serialize<S: Serializer>(&self, serializer: S) {
467-
match &self.0.qualifier {
468-
None => None::<()>.serialize(serializer),
469-
Some(qualifier) => {
470-
TSImportTypeQualifierAsQualifiedName(qualifier).serialize(serializer);
471-
}
472-
}
473-
}
474-
}
475-
476-
struct TSImportTypeQualifierAsQualifiedName<'a, 'b>(&'b TSImportTypeQualifier<'a>);
477-
478-
impl ESTree for TSImportTypeQualifierAsQualifiedName<'_, '_> {
479-
fn serialize<S: Serializer>(&self, serializer: S) {
480-
match self.0 {
481-
TSImportTypeQualifier::Identifier(ident) => {
482-
// Convert IdentifierName to Identifier
483-
let mut state = serializer.serialize_struct();
484-
state.serialize_field("type", &JsonSafeString("Identifier"));
485-
state.serialize_field("decorators", &Vec::<Decorator>::new().as_slice());
486-
state.serialize_field("name", &ident.name);
487-
state.serialize_field("optional", &false);
488-
state.serialize_field("typeAnnotation", &None::<()>);
489-
state.serialize_span(ident.span);
490-
state.end();
491-
}
492-
TSImportTypeQualifier::QualifiedName(name) => {
493-
// Convert TSImportTypeQualifiedName to TSQualifiedName
494-
let mut state = serializer.serialize_struct();
495-
state.serialize_field("type", &JsonSafeString("TSQualifiedName"));
496-
state.serialize_field("left", &TSImportTypeQualifierAsQualifiedName(&name.left));
497-
state.serialize_field("right", &IdentifierAsIdentifier(&name.right));
498-
state.serialize_span(name.span);
499-
state.end();
500-
}
501-
}
502-
}
503-
}
504-
505-
struct IdentifierAsIdentifier<'a, 'b>(&'b IdentifierName<'a>);
506-
507-
impl ESTree for IdentifierAsIdentifier<'_, '_> {
508-
fn serialize<S: Serializer>(&self, serializer: S) {
509-
let mut state = serializer.serialize_struct();
510-
state.serialize_field("type", &JsonSafeString("Identifier"));
511-
state.serialize_field("decorators", &Vec::<Decorator>::new().as_slice());
512-
state.serialize_field("name", &self.0.name);
513-
state.serialize_field("optional", &false);
514-
state.serialize_field("typeAnnotation", &None::<()>);
515-
state.serialize_span(self.0.span);
516-
state.end();
517-
}
518-
}

napi/parser/generated/deserialize/js.js

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,58 +1874,11 @@ function deserializeTSTypeQuery(pos) {
18741874
}
18751875

18761876
function deserializeTSImportType(pos) {
1877-
let qualifier = deserializeOptionTSImportTypeQualifier(pos + 32);
1878-
if (qualifier !== null) {
1879-
if (qualifier.type === 'IdentifierName') {
1880-
qualifier = {
1881-
type: 'Identifier',
1882-
decorators: [],
1883-
name: qualifier.name,
1884-
optional: false,
1885-
typeAnnotation: null,
1886-
start: qualifier.start,
1887-
end: qualifier.end,
1888-
};
1889-
} else if (qualifier.type === 'TSImportTypeQualifiedName') {
1890-
// Convert TSImportTypeQualifiedName to TSQualifiedName
1891-
const convertQualifier = (q) => {
1892-
if (q.type === 'IdentifierName') {
1893-
return {
1894-
type: 'Identifier',
1895-
decorators: [],
1896-
name: q.name,
1897-
optional: false,
1898-
typeAnnotation: null,
1899-
start: q.start,
1900-
end: q.end,
1901-
};
1902-
} else if (q.type === 'TSImportTypeQualifiedName') {
1903-
return {
1904-
type: 'TSQualifiedName',
1905-
left: convertQualifier(q.left),
1906-
right: {
1907-
type: 'Identifier',
1908-
decorators: [],
1909-
name: q.right.name,
1910-
optional: false,
1911-
typeAnnotation: null,
1912-
start: q.right.start,
1913-
end: q.right.end,
1914-
},
1915-
start: q.start,
1916-
end: q.end,
1917-
};
1918-
}
1919-
return q;
1920-
};
1921-
qualifier = convertQualifier(qualifier);
1922-
}
1923-
}
19241877
return {
19251878
type: 'TSImportType',
19261879
argument: deserializeTSType(pos + 8),
19271880
options: deserializeOptionBoxObjectExpression(pos + 24),
1928-
qualifier,
1881+
qualifier: deserializeOptionTSImportTypeQualifier(pos + 32),
19291882
typeArguments: deserializeOptionBoxTSTypeParameterInstantiation(pos + 48),
19301883
start: deserializeU32(pos),
19311884
end: deserializeU32(pos + 4),
@@ -1934,7 +1887,7 @@ function deserializeTSImportType(pos) {
19341887

19351888
function deserializeTSImportTypeQualifiedName(pos) {
19361889
return {
1937-
type: 'TSImportTypeQualifiedName',
1890+
type: 'TSQualifiedName',
19381891
left: deserializeTSImportTypeQualifier(pos + 8),
19391892
right: deserializeIdentifierName(pos + 24),
19401893
start: deserializeU32(pos),

napi/parser/generated/deserialize/ts.js

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,58 +2005,11 @@ function deserializeTSTypeQuery(pos) {
20052005
}
20062006

20072007
function deserializeTSImportType(pos) {
2008-
let qualifier = deserializeOptionTSImportTypeQualifier(pos + 32);
2009-
if (qualifier !== null) {
2010-
if (qualifier.type === 'IdentifierName') {
2011-
qualifier = {
2012-
type: 'Identifier',
2013-
decorators: [],
2014-
name: qualifier.name,
2015-
optional: false,
2016-
typeAnnotation: null,
2017-
start: qualifier.start,
2018-
end: qualifier.end,
2019-
};
2020-
} else if (qualifier.type === 'TSImportTypeQualifiedName') {
2021-
// Convert TSImportTypeQualifiedName to TSQualifiedName
2022-
const convertQualifier = (q) => {
2023-
if (q.type === 'IdentifierName') {
2024-
return {
2025-
type: 'Identifier',
2026-
decorators: [],
2027-
name: q.name,
2028-
optional: false,
2029-
typeAnnotation: null,
2030-
start: q.start,
2031-
end: q.end,
2032-
};
2033-
} else if (q.type === 'TSImportTypeQualifiedName') {
2034-
return {
2035-
type: 'TSQualifiedName',
2036-
left: convertQualifier(q.left),
2037-
right: {
2038-
type: 'Identifier',
2039-
decorators: [],
2040-
name: q.right.name,
2041-
optional: false,
2042-
typeAnnotation: null,
2043-
start: q.right.start,
2044-
end: q.right.end,
2045-
},
2046-
start: q.start,
2047-
end: q.end,
2048-
};
2049-
}
2050-
return q;
2051-
};
2052-
qualifier = convertQualifier(qualifier);
2053-
}
2054-
}
20552008
return {
20562009
type: 'TSImportType',
20572010
argument: deserializeTSType(pos + 8),
20582011
options: deserializeOptionBoxObjectExpression(pos + 24),
2059-
qualifier,
2012+
qualifier: deserializeOptionTSImportTypeQualifier(pos + 32),
20602013
typeArguments: deserializeOptionBoxTSTypeParameterInstantiation(pos + 48),
20612014
start: deserializeU32(pos),
20622015
end: deserializeU32(pos + 4),
@@ -2065,7 +2018,7 @@ function deserializeTSImportType(pos) {
20652018

20662019
function deserializeTSImportTypeQualifiedName(pos) {
20672020
return {
2068-
type: 'TSImportTypeQualifiedName',
2021+
type: 'TSQualifiedName',
20692022
left: deserializeTSImportTypeQualifier(pos + 8),
20702023
right: deserializeIdentifierName(pos + 24),
20712024
start: deserializeU32(pos),

npm/oxc-types/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,14 +1351,14 @@ export interface TSImportType extends Span {
13511351
type: 'TSImportType';
13521352
argument: TSType;
13531353
options: ObjectExpression | null;
1354-
qualifier: TSQualifiedName | IdentifierName | null;
1354+
qualifier: TSImportTypeQualifier | null;
13551355
typeArguments: TSTypeParameterInstantiation | null;
13561356
}
13571357

13581358
export type TSImportTypeQualifier = IdentifierName | TSImportTypeQualifiedName;
13591359

13601360
export interface TSImportTypeQualifiedName extends Span {
1361-
type: 'TSImportTypeQualifiedName';
1361+
type: 'TSQualifiedName';
13621362
left: TSImportTypeQualifier;
13631363
right: IdentifierName;
13641364
}

0 commit comments

Comments
 (0)