Skip to content

Commit 974871c

Browse files
committed
fix(ast/estree): fix TSImportType
1 parent 43becb7 commit 974871c

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
@@ -505,6 +505,86 @@ impl ESTree for ExportAllDeclarationWithClause<'_, '_> {
505505
}
506506
}
507507

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

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)