Skip to content

Commit f05e966

Browse files
committed
fix(ast/estree): fix TSImportType
1 parent 89b6e4c commit f05e966

File tree

6 files changed

+169
-36
lines changed

6 files changed

+169
-36
lines changed

crates/oxc_ast/src/ast/ts.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,7 @@ pub struct TSImportType<'a> {
12891289
#[ast(visit)]
12901290
#[derive(Debug)]
12911291
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
1292+
#[estree(via = TSImportAttributesConverter)]
12921293
pub struct TSImportAttributes<'a> {
12931294
pub span: Span,
12941295
pub attributes_keyword: IdentifierName<'a>, // `with` or `assert`
@@ -1300,8 +1301,14 @@ pub struct TSImportAttributes<'a> {
13001301
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
13011302
// Pluralize as `TSImportAttributeList` to avoid naming clash with `TSImportAttributes`.
13021303
#[plural(TSImportAttributeList)]
1304+
#[estree(
1305+
rename = "Property",
1306+
add_fields(method = False, shorthand = True, computed = False, kind = Init),
1307+
field_order(span, method, shorthand, computed, name, value, kind),
1308+
)]
13031309
pub struct TSImportAttribute<'a> {
13041310
pub span: Span,
1311+
#[estree(rename = "key")]
13051312
pub name: TSImportAttributeName<'a>,
13061313
pub value: Expression<'a>,
13071314
}

crates/oxc_ast/src/generated/derive_estree.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -3102,24 +3102,22 @@ impl ESTree for TSImportType<'_> {
31023102

31033103
impl ESTree for TSImportAttributes<'_> {
31043104
fn serialize<S: Serializer>(&self, serializer: S) {
3105-
let mut state = serializer.serialize_struct();
3106-
state.serialize_field("type", &JsonSafeString("TSImportAttributes"));
3107-
state.serialize_field("start", &self.span.start);
3108-
state.serialize_field("end", &self.span.end);
3109-
state.serialize_field("attributesKeyword", &self.attributes_keyword);
3110-
state.serialize_field("elements", &self.elements);
3111-
state.end();
3105+
crate::serialize::TSImportAttributesConverter(self).serialize(serializer)
31123106
}
31133107
}
31143108

31153109
impl ESTree for TSImportAttribute<'_> {
31163110
fn serialize<S: Serializer>(&self, serializer: S) {
31173111
let mut state = serializer.serialize_struct();
3118-
state.serialize_field("type", &JsonSafeString("TSImportAttribute"));
3112+
state.serialize_field("type", &JsonSafeString("Property"));
31193113
state.serialize_field("start", &self.span.start);
31203114
state.serialize_field("end", &self.span.end);
3121-
state.serialize_field("name", &self.name);
3115+
state.serialize_field("method", &crate::serialize::False(self));
3116+
state.serialize_field("shorthand", &crate::serialize::True(self));
3117+
state.serialize_field("computed", &crate::serialize::False(self));
3118+
state.serialize_field("key", &self.name);
31223119
state.serialize_field("value", &self.value);
3120+
state.serialize_field("kind", &crate::serialize::Init(self));
31233121
state.end();
31243122
}
31253123
}

crates/oxc_ast/src/serialize.rs

+80
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,86 @@ impl ESTree for ExportAllDeclarationWithClause<'_, '_> {
506506
}
507507
}
508508

509+
#[ast_meta]
510+
#[estree(
511+
ts_type = "ObjectExpression",
512+
raw_deser = "
513+
const start = DESER[u32](POS_OFFSET.span.start);
514+
const end = DESER[u32](POS_OFFSET.span.end);
515+
const attributesKeyword = DESER[IdentifierName](POS_OFFSET.attributes_keyword);
516+
const properties = DESER[Vec<TSImportAttribute>](POS_OFFSET.elements);
517+
const result = {
518+
type: 'ObjectExpression',
519+
start,
520+
end,
521+
properties: [
522+
{
523+
type: 'Property',
524+
start,
525+
end,
526+
method: false,
527+
shorthand: false,
528+
computed: false,
529+
key: attributesKeyword,
530+
value: {
531+
type: 'ObjectExpression',
532+
start,
533+
end,
534+
properties,
535+
},
536+
kind: 'init',
537+
},
538+
],
539+
};
540+
result
541+
"
542+
)]
543+
pub struct TSImportAttributesConverter<'a, 'b>(pub &'b TSImportAttributes<'a>);
544+
545+
impl ESTree for TSImportAttributesConverter<'_, '_> {
546+
fn serialize<S: Serializer>(&self, serializer: S) {
547+
let mut state = serializer.serialize_struct();
548+
state.serialize_field("type", &JsonSafeString("ObjectExpression"));
549+
state.serialize_field("start", &self.0.span.start);
550+
state.serialize_field("end", &self.0.span.end);
551+
state.serialize_field("properties", &[TSImportAttributesWithClauseConverter(self.0)]);
552+
state.end();
553+
}
554+
}
555+
556+
struct TSImportAttributesWithClauseConverter<'a, 'b>(&'b TSImportAttributes<'a>);
557+
558+
impl ESTree for TSImportAttributesWithClauseConverter<'_, '_> {
559+
fn serialize<S: Serializer>(&self, serializer: S) {
560+
let mut state = serializer.serialize_struct();
561+
state.serialize_field("type", &JsonSafeString("Property"));
562+
// TODO: span
563+
state.serialize_field("start", &self.0.span.end);
564+
state.serialize_field("end", &self.0.span.end);
565+
state.serialize_field("method", &false);
566+
state.serialize_field("shorthand", &false);
567+
state.serialize_field("computed", &false);
568+
state.serialize_field("key", &self.0.attributes_keyword);
569+
state.serialize_field("value", &TSImportAttributesElementsConverter(self.0));
570+
state.serialize_field("kind", &"init");
571+
state.end();
572+
}
573+
}
574+
575+
struct TSImportAttributesElementsConverter<'a, 'b>(&'b TSImportAttributes<'a>);
576+
577+
impl ESTree for TSImportAttributesElementsConverter<'_, '_> {
578+
fn serialize<S: Serializer>(&self, serializer: S) {
579+
let mut state = serializer.serialize_struct();
580+
state.serialize_field("type", &JsonSafeString("ObjectExpression"));
581+
// TODO: span
582+
state.serialize_field("start", &self.0.span.start);
583+
state.serialize_field("end", &self.0.span.end);
584+
state.serialize_field("properties", &self.0.elements);
585+
state.end();
586+
}
587+
}
588+
509589
// --------------------
510590
// JSX
511591
// --------------------

napi/parser/deserialize-js.js

+34-9
Original file line numberDiff line numberDiff line change
@@ -1780,22 +1780,47 @@ function deserializeTSImportType(pos) {
17801780
}
17811781

17821782
function deserializeTSImportAttributes(pos) {
1783-
return {
1784-
type: 'TSImportAttributes',
1785-
start: deserializeU32(pos),
1786-
end: deserializeU32(pos + 4),
1787-
attributesKeyword: deserializeIdentifierName(pos + 8),
1788-
elements: deserializeVecTSImportAttribute(pos + 32),
1789-
};
1783+
const start = deserializeU32(pos);
1784+
const end = deserializeU32(pos + 4);
1785+
const attributesKeyword = deserializeIdentifierName(pos + 8);
1786+
const properties = deserializeVecTSImportAttribute(pos + 32);
1787+
const result = {
1788+
type: 'ObjectExpression',
1789+
start,
1790+
end,
1791+
properties: [
1792+
{
1793+
type: 'Property',
1794+
start,
1795+
end,
1796+
method: false,
1797+
shorthand: false,
1798+
computed: false,
1799+
key: attributesKeyword,
1800+
value: {
1801+
type: 'ObjectExpression',
1802+
start,
1803+
end,
1804+
properties,
1805+
},
1806+
kind: 'init',
1807+
},
1808+
],
1809+
};
1810+
return result;
17901811
}
17911812

17921813
function deserializeTSImportAttribute(pos) {
17931814
return {
1794-
type: 'TSImportAttribute',
1815+
type: 'Property',
17951816
start: deserializeU32(pos),
17961817
end: deserializeU32(pos + 4),
1797-
name: deserializeTSImportAttributeName(pos + 8),
1818+
method: false,
1819+
shorthand: true,
1820+
computed: false,
1821+
key: deserializeTSImportAttributeName(pos + 8),
17981822
value: deserializeExpression(pos + 56),
1823+
kind: 'init',
17991824
};
18001825
}
18011826

napi/parser/deserialize-ts.js

+34-9
Original file line numberDiff line numberDiff line change
@@ -1833,22 +1833,47 @@ function deserializeTSImportType(pos) {
18331833
}
18341834

18351835
function deserializeTSImportAttributes(pos) {
1836-
return {
1837-
type: 'TSImportAttributes',
1838-
start: deserializeU32(pos),
1839-
end: deserializeU32(pos + 4),
1840-
attributesKeyword: deserializeIdentifierName(pos + 8),
1841-
elements: deserializeVecTSImportAttribute(pos + 32),
1842-
};
1836+
const start = deserializeU32(pos);
1837+
const end = deserializeU32(pos + 4);
1838+
const attributesKeyword = deserializeIdentifierName(pos + 8);
1839+
const properties = deserializeVecTSImportAttribute(pos + 32);
1840+
const result = {
1841+
type: 'ObjectExpression',
1842+
start,
1843+
end,
1844+
properties: [
1845+
{
1846+
type: 'Property',
1847+
start,
1848+
end,
1849+
method: false,
1850+
shorthand: false,
1851+
computed: false,
1852+
key: attributesKeyword,
1853+
value: {
1854+
type: 'ObjectExpression',
1855+
start,
1856+
end,
1857+
properties,
1858+
},
1859+
kind: 'init',
1860+
},
1861+
],
1862+
};
1863+
return result;
18431864
}
18441865

18451866
function deserializeTSImportAttribute(pos) {
18461867
return {
1847-
type: 'TSImportAttribute',
1868+
type: 'Property',
18481869
start: deserializeU32(pos),
18491870
end: deserializeU32(pos + 4),
1850-
name: deserializeTSImportAttributeName(pos + 8),
1871+
method: false,
1872+
shorthand: true,
1873+
computed: false,
1874+
key: deserializeTSImportAttributeName(pos + 8),
18511875
value: deserializeExpression(pos + 56),
1876+
kind: 'init',
18521877
};
18531878
}
18541879

npm/oxc-types/types.d.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1282,22 +1282,20 @@ export type TSTypeQueryExprName = TSImportType | TSTypeName;
12821282
export interface TSImportType extends Span {
12831283
type: 'TSImportType';
12841284
argument: TSType;
1285-
options: TSImportAttributes | null;
1285+
options: ObjectExpression | null;
12861286
qualifier: TSTypeName | null;
12871287
typeArguments: TSTypeParameterInstantiation | null;
12881288
isTypeOf: boolean;
12891289
}
12901290

1291-
export interface TSImportAttributes extends Span {
1292-
type: 'TSImportAttributes';
1293-
attributesKeyword: IdentifierName;
1294-
elements: Array<TSImportAttribute>;
1295-
}
1296-
12971291
export interface TSImportAttribute extends Span {
1298-
type: 'TSImportAttribute';
1299-
name: TSImportAttributeName;
1292+
type: 'Property';
1293+
method: false;
1294+
shorthand: true;
1295+
computed: false;
1296+
key: TSImportAttributeName;
13001297
value: Expression;
1298+
kind: 'init';
13011299
}
13021300

13031301
export type TSImportAttributeName = IdentifierName | StringLiteral;

0 commit comments

Comments
 (0)