Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/generated/ast_changes_watch_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ src:
- 'crates/oxc_traverse/src/generated/scopes_collector.rs'
- 'napi/parser/generated/constants.js'
- 'napi/parser/generated/deserialize/js.js'
- 'napi/parser/generated/deserialize/js_range.js'
- 'napi/parser/generated/deserialize/ts.js'
- 'napi/parser/generated/deserialize/ts_range.js'
- 'napi/parser/generated/lazy/constructors.js'
- 'napi/parser/generated/lazy/types.js'
- 'napi/parser/generated/lazy/walk.js'
Expand Down
19 changes: 13 additions & 6 deletions crates/oxc_ast/src/serialize/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl ESTree for CatchParameterConverter<'_, '_> {
const params = DESER[Vec<FormalParameter>](POS_OFFSET.items);
if (uint32[(POS_OFFSET.rest) >> 2] !== 0 && uint32[(POS_OFFSET.rest + 4) >> 2] !== 0) {
pos = uint32[(POS_OFFSET.rest) >> 2];
let start, end;
params.push({
type: 'RestElement',
...(IS_TS && { decorators: [] }),
Expand All @@ -130,8 +131,9 @@ impl ESTree for CatchParameterConverter<'_, '_> {
),
value: null,
}),
start: DESER[u32]( POS_OFFSET<BindingRestElement>.span.start ),
end: DESER[u32]( POS_OFFSET<BindingRestElement>.span.end ),
start: start = DESER[u32]( POS_OFFSET<BindingRestElement>.span.start ),
end: end = DESER[u32]( POS_OFFSET<BindingRestElement>.span.end ),
...(RANGE && { range: [start, end] }),
});
}
params
Expand Down Expand Up @@ -192,6 +194,7 @@ impl ESTree for FormalParametersRest<'_, '_> {
param.optional = DESER[bool](POS_OFFSET.pattern.optional);
param.typeAnnotation = DESER[Option<Box<TSTypeAnnotation>>](POS_OFFSET.pattern.type_annotation);
} else {
let start, end;
param = {
type: 'TSParameterProperty',
accessibility,
Expand All @@ -200,8 +203,9 @@ impl ESTree for FormalParametersRest<'_, '_> {
parameter: DESER[BindingPattern](POS_OFFSET.pattern),
readonly,
static: false,
start: DESER[u32]( POS_OFFSET<BindingRestElement>.span.start ),
end: DESER[u32]( POS_OFFSET<BindingRestElement>.span.end ),
start: start = DESER[u32]( POS_OFFSET<BindingRestElement>.span.start ),
end: end = DESER[u32]( POS_OFFSET<BindingRestElement>.span.end ),
...(RANGE && { range: [start, end] }),
};
}
} else {
Expand Down Expand Up @@ -416,6 +420,7 @@ impl ESTree for ArrowFunctionExpressionBody<'_> {
}),
start: THIS.start,
end: THIS.end,
...(RANGE && { range: [THIS.start, THIS.end] }),
};
value
"
Expand Down Expand Up @@ -455,11 +460,13 @@ impl ESTree for AssignmentTargetPropertyIdentifierInit<'_> {
#[estree(raw_deser = "
let node = DESER[Expression](POS_OFFSET.expression);
if (preserveParens) {
let start, end;
node = {
type: 'ParenthesizedExpression',
expression: node,
start: DESER[u32]( POS_OFFSET.span.start ),
end: DESER[u32]( POS_OFFSET.span.end ),
start: start = DESER[u32]( POS_OFFSET.span.start ),
end: end = DESER[u32]( POS_OFFSET.span.end ),
...(RANGE && { range: [start, end] }),
};
}
node
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_ast/src/serialize/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl ESTree for JSXOpeningElementSelfClosing<'_, '_> {
ts_type = "JSXIdentifier",
raw_deser = "
const ident = DESER[Box<IdentifierReference>](POS);
{ type: 'JSXIdentifier', name: ident.name, start: ident.start, end: ident.end }
{ type: 'JSXIdentifier', name: ident.name, start: ident.start, end: ident.end, ...(RANGE && { range: ident.range }) }
"
)]
pub struct JSXElementIdentifierReference<'a, 'b>(pub &'b IdentifierReference<'a>);
Expand All @@ -75,7 +75,7 @@ impl ESTree for JSXElementIdentifierReference<'_, '_> {
ts_type = "JSXIdentifier",
raw_deser = "
const thisExpr = DESER[Box<ThisExpression>](POS);
{ type: 'JSXIdentifier', name: 'this', start: thisExpr.start, end: thisExpr.end }
{ type: 'JSXIdentifier', name: 'this', start: thisExpr.start, end: thisExpr.end, ...(RANGE && { range: thisExpr.range }) }
"
)]
pub struct JSXElementThisExpression<'b>(pub &'b ThisExpression);
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/serialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl Program<'_> {
hashbang: DESER[Option<Hashbang>](POS_OFFSET.hashbang),
start,
end,
...(RANGE && { range: [start, end] }),
};
program
")]
Expand Down
51 changes: 38 additions & 13 deletions crates/oxc_ast/src/serialize/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,36 @@ impl ESTree for ExpressionStatementDirective<'_, '_> {
if (body !== null && body.type === 'TSModuleDeclaration') {
let innerId = body.id;
if (innerId.type === 'Identifier') {
let start, end;
id = {
type: 'TSQualifiedName',
left: id,
right: innerId,
start: id.start,
end: innerId.end,
start: start = id.start,
end: end = innerId.end,
...(RANGE && { range: [start, end] }),
};
} else {
// Replace `left` of innermost `TSQualifiedName` with a nested `TSQualifiedName` with `id` of
// this module on left, and previous `left` of innermost `TSQualifiedName` on right
while (true) {
innerId.start = id.start;
if (RANGE) {
innerId.start = innerId.range[0] = id.start;
} else {
innerId.start = id.start;
}
if (innerId.left.type === 'Identifier') break;
innerId = innerId.left;
}

let start, end;
innerId.left = {
type: 'TSQualifiedName',
left: id,
right: innerId.left,
start: id.start,
end: innerId.left.end,
start: start = id.start,
end: end = innerId.left.end,
...(RANGE && { range: [start, end] }),
};
id = body.id;
}
Expand All @@ -89,8 +98,18 @@ impl ESTree for ExpressionStatementDirective<'_, '_> {

// Skip `body` field if `null`
const node = body === null
? { type: 'TSModuleDeclaration', id, kind, declare, global, start, end }
: { type: 'TSModuleDeclaration', id, body, kind, declare, global, start, end };
? {
type: 'TSModuleDeclaration',
id, kind, declare, global,
start, end,
...(RANGE && { range: [start, end] })
}
: {
type: 'TSModuleDeclaration',
id, body, kind, declare, global,
start, end,
...(RANGE && { range: [start, end] })
};
node
")]
pub struct TSModuleDeclarationConverter<'a, 'b>(pub &'b TSModuleDeclaration<'a>);
Expand Down Expand Up @@ -273,26 +292,30 @@ impl ESTree for TSMappedTypeConstraint<'_, '_> {
let expression = DESER[TSTypeName](POS_OFFSET.expression);
if (expression.type === 'TSQualifiedName') {
let object = expression.left;
let start, end;
let parent = expression = {
type: 'MemberExpression',
object,
property: expression.right,
optional: false,
computed: false,
start: expression.start,
end: expression.end,
start: start = expression.start,
end: end = expression.end,
...(RANGE && { range: [start, end] }),
};

while (object.type === 'TSQualifiedName') {
const { left } = object;
let start, end;
parent = parent.object = {
type: 'MemberExpression',
object: left,
property: object.right,
optional: false,
computed: false,
start: object.start,
end: object.end,
start: start = object.start,
end: end = object.end,
...(RANGE && { range: [start, end] }),
};
object = left;
}
Expand Down Expand Up @@ -415,11 +438,13 @@ impl ESTree for TSFunctionTypeParams<'_, '_> {
#[estree(raw_deser = "
let node = DESER[TSType](POS_OFFSET.type_annotation);
if (preserveParens) {
let start, end;
node = {
type: 'TSParenthesizedType',
typeAnnotation: node,
start: DESER[u32]( POS_OFFSET.span.start ),
end: DESER[u32]( POS_OFFSET.span.end ),
start: start = DESER[u32]( POS_OFFSET.span.start ),
end: end = DESER[u32]( POS_OFFSET.span.end ),
...(RANGE && { range: [start, end] }),
};
}
node
Expand Down
22 changes: 14 additions & 8 deletions crates/oxc_syntax/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ macro_rules! dummy_estree_impl {
ts_type = "Dummy",
raw_deser = "
var nameSpan = DESER[NameSpan](POS);
{kind: 'Name', name: nameSpan.value, start: nameSpan.start, end: nameSpan.end}
{ kind: 'Name', name: nameSpan.value, start: nameSpan.start, end: nameSpan.end, ...(RANGE && { range: nameSpan.range }) }
"
)]
pub struct ImportOrExportNameName<'a, 'b>(#[expect(dead_code)] pub &'b NameSpan<'a>);
Expand All @@ -38,7 +38,7 @@ dummy_estree_impl!(ImportOrExportNameName<'_, '_>);
ts_type = "Dummy",
raw_deser = "
var nameSpan = DESER[NameSpan](POS);
{kind: 'Default', name: nameSpan.value, start: nameSpan.start, end: nameSpan.end}
{ kind: 'Default', name: nameSpan.value, start: nameSpan.start, end: nameSpan.end, ...(RANGE && { range: nameSpan.range }) }
"
)]
pub struct ExportLocalNameDefault<'a, 'b>(#[expect(dead_code)] pub &'b NameSpan<'a>);
Expand All @@ -47,7 +47,10 @@ dummy_estree_impl!(ExportLocalNameDefault<'_, '_>);

/// Serializer for `Null` variant of `ExportLocalName`, `ExportExportName`, and `ExportImportName`.
#[ast_meta]
#[estree(ts_type = "Dummy", raw_deser = "{kind: 'None', name: null, start: null, end: null}")]
#[estree(
ts_type = "Dummy",
raw_deser = "{ kind: 'None', name: null, start: null, end: null, ...(RANGE && { range: [null, null] }) }"
)]
pub struct ExportNameNull(pub ());

dummy_estree_impl!(ExportNameNull);
Expand All @@ -57,8 +60,8 @@ dummy_estree_impl!(ExportNameNull);
#[estree(
ts_type = "Dummy",
raw_deser = "
var span = DESER[Span](POS);
{kind: 'Default', name: null, start: span.start, end: span.end}
var { start, end } = DESER[Span](POS);
{ kind: 'Default', name: null, start, end, ...(RANGE && { range: [start, end] }) }
"
)]
pub struct ImportOrExportNameDefault<'b>(#[expect(dead_code)] pub &'b Span);
Expand All @@ -67,7 +70,10 @@ dummy_estree_impl!(ImportOrExportNameDefault<'_>);

/// Serializer for `All` variant of `ExportImportName`.
#[ast_meta]
#[estree(ts_type = "Dummy", raw_deser = "{kind: 'All', name: null, start: null, end: null}")]
#[estree(
ts_type = "Dummy",
raw_deser = "{ kind: 'All', name: null, start: null, end: null, ...(RANGE && { range: [null, null] }) }"
)]
pub struct ExportImportNameAll(pub ());

dummy_estree_impl!(ExportImportNameAll);
Expand All @@ -76,7 +82,7 @@ dummy_estree_impl!(ExportImportNameAll);
#[ast_meta]
#[estree(
ts_type = "Dummy",
raw_deser = "{kind: 'AllButDefault', name: null, start: null, end: null}"
raw_deser = "{ kind: 'AllButDefault', name: null, start: null, end: null, ...(RANGE && { range: [null, null] }) }"
)]
pub struct ExportImportNameAllButDefault(pub ());

Expand All @@ -86,7 +92,7 @@ dummy_estree_impl!(ExportImportNameAllButDefault);
#[ast_meta]
#[estree(
ts_type = "Dummy",
raw_deser = "{kind: 'NamespaceObject', name: null, start: null, end: null}"
raw_deser = "{ kind: 'NamespaceObject', name: null, start: null, end: null, ...(RANGE && { range: [null, null] }) }"
)]
pub struct ImportImportNameNamespaceObject(pub ());

Expand Down
Loading
Loading