mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
fix(estree): handle empty spans serializing ImportMeta and NewTarget (#24775)
Follow-on after #24557. Handle when `ImportMeta` / `NewTarget` has an empty span (`SPAN`). This code path is not exercised at present, but it seems a good idea to be prepared for when we may serialize AST after transformation.
This commit is contained in:
1 parent
4f45b48818
commit
e80574fdf0
10 files changed
+65
-61
No files matched your search
@@ -763,7 +763,7 @@ function deserializeImportMeta(pos) {
|
||||
parent,
|
||||
}),
|
||||
importStart = deserializeI32(pos),
|
||||
importEnd = importStart + 6,
|
||||
importEnd = deserializeI32(pos + 4) === 0 ? 0 : importStart + 6,
|
||||
importIdent = {
|
||||
__proto__: NodeProto,
|
||||
type: "Identifier",
|
||||
@@ -777,7 +777,7 @@ function deserializeImportMeta(pos) {
|
||||
parent,
|
||||
},
|
||||
metaEnd = deserializeI32(pos + 4),
|
||||
metaStart = metaEnd - 4,
|
||||
metaStart = metaEnd === 0 ? 0 : metaEnd - 4,
|
||||
metaIdent = {
|
||||
__proto__: NodeProto,
|
||||
type: "Identifier",
|
||||
@@ -811,7 +811,7 @@ function deserializeNewTarget(pos) {
|
||||
parent,
|
||||
}),
|
||||
newStart = deserializeI32(pos),
|
||||
newEnd = newStart + 3,
|
||||
newEnd = deserializeI32(pos + 4) === 0 ? 0 : newStart + 3,
|
||||
newIdent = {
|
||||
__proto__: NodeProto,
|
||||
type: "Identifier",
|
||||
@@ -825,7 +825,7 @@ function deserializeNewTarget(pos) {
|
||||
parent,
|
||||
},
|
||||
targetEnd = deserializeI32(pos + 4),
|
||||
targetStart = targetEnd - 6,
|
||||
targetStart = targetEnd === 0 ? 0 : targetEnd - 6,
|
||||
targetIdent = {
|
||||
__proto__: NodeProto,
|
||||
type: "Identifier",
|
||||
|
||||
@@ -5,7 +5,7 @@ use oxc_estree::{
|
||||
Concat2, ConcatElement, ESTree, JsonSafeString, SequenceSerializer, Serializer,
|
||||
StructSerializer,
|
||||
};
|
||||
use oxc_span::{GetSpan, Span};
|
||||
use oxc_span::{GetSpan, SPAN, Span};
|
||||
use oxc_str::{Ident, static_ident};
|
||||
use oxc_syntax::node::NodeId;
|
||||
|
||||
@@ -26,7 +26,7 @@ fn serialize_meta_property_identifier<S: Serializer>(serializer: S, span: Span,
|
||||
ts_type = "IdentifierName",
|
||||
raw_deser = "
|
||||
const importStart = DESER[i32](POS_OFFSET.span.start);
|
||||
const importEnd = importStart + 6;
|
||||
const importEnd = DESER[i32](POS_OFFSET.span.end) === 0 ? 0 : importStart + 6;
|
||||
const importIdent = {
|
||||
type: 'Identifier',
|
||||
...(IS_TS && { decorators: [] }),
|
||||
@@ -44,11 +44,12 @@ pub struct ImportMetaMeta<'b>(pub &'b ImportMeta);
|
||||
|
||||
impl ESTree for ImportMetaMeta<'_> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) {
|
||||
serialize_meta_property_identifier(
|
||||
serializer,
|
||||
Span::sized(self.0.span.start, 6),
|
||||
static_ident!("import"),
|
||||
);
|
||||
let mut span = self.0.span;
|
||||
if span != SPAN {
|
||||
span.end = span.start + 6;
|
||||
}
|
||||
|
||||
serialize_meta_property_identifier(serializer, span, static_ident!("import"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +58,7 @@ impl ESTree for ImportMetaMeta<'_> {
|
||||
ts_type = "IdentifierName",
|
||||
raw_deser = "
|
||||
const metaEnd = DESER[i32](POS_OFFSET.span.end);
|
||||
const metaStart = metaEnd - 4;
|
||||
const metaStart = metaEnd === 0 ? 0 : metaEnd - 4;
|
||||
const metaIdent = {
|
||||
type: 'Identifier',
|
||||
...(IS_TS && { decorators: [] }),
|
||||
@@ -75,11 +76,12 @@ pub struct ImportMetaProperty<'b>(pub &'b ImportMeta);
|
||||
|
||||
impl ESTree for ImportMetaProperty<'_> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) {
|
||||
serialize_meta_property_identifier(
|
||||
serializer,
|
||||
Span::new(self.0.span.end.saturating_sub(4), self.0.span.end),
|
||||
static_ident!("meta"),
|
||||
);
|
||||
let mut span = self.0.span;
|
||||
if span != SPAN {
|
||||
span.start = span.end - 4;
|
||||
}
|
||||
|
||||
serialize_meta_property_identifier(serializer, span, static_ident!("meta"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +90,7 @@ impl ESTree for ImportMetaProperty<'_> {
|
||||
ts_type = "IdentifierName",
|
||||
raw_deser = "
|
||||
const newStart = DESER[i32](POS_OFFSET.span.start);
|
||||
const newEnd = newStart + 3;
|
||||
const newEnd = DESER[i32](POS_OFFSET.span.end) === 0 ? 0 : newStart + 3;
|
||||
const newIdent = {
|
||||
type: 'Identifier',
|
||||
...(IS_TS && { decorators: [] }),
|
||||
@@ -106,11 +108,12 @@ pub struct NewTargetMeta<'b>(pub &'b NewTarget);
|
||||
|
||||
impl ESTree for NewTargetMeta<'_> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) {
|
||||
serialize_meta_property_identifier(
|
||||
serializer,
|
||||
Span::sized(self.0.span.start, 3),
|
||||
static_ident!("new"),
|
||||
);
|
||||
let mut span = self.0.span;
|
||||
if span != SPAN {
|
||||
span.end = span.start + 3;
|
||||
}
|
||||
|
||||
serialize_meta_property_identifier(serializer, span, static_ident!("new"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +122,7 @@ impl ESTree for NewTargetMeta<'_> {
|
||||
ts_type = "IdentifierName",
|
||||
raw_deser = "
|
||||
const targetEnd = DESER[i32](POS_OFFSET.span.end);
|
||||
const targetStart = targetEnd - 6;
|
||||
const targetStart = targetEnd === 0 ? 0 : targetEnd - 6;
|
||||
const targetIdent = {
|
||||
type: 'Identifier',
|
||||
...(IS_TS && { decorators: [] }),
|
||||
@@ -137,11 +140,12 @@ pub struct NewTargetProperty<'b>(pub &'b NewTarget);
|
||||
|
||||
impl ESTree for NewTargetProperty<'_> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) {
|
||||
serialize_meta_property_identifier(
|
||||
serializer,
|
||||
Span::new(self.0.span.end.saturating_sub(6), self.0.span.end),
|
||||
static_ident!("target"),
|
||||
);
|
||||
let mut span = self.0.span;
|
||||
if span != SPAN {
|
||||
span.start = span.end - 6;
|
||||
}
|
||||
|
||||
serialize_meta_property_identifier(serializer, span, static_ident!("target"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -611,13 +611,13 @@ function deserializeImportMeta(pos) {
|
||||
type: "Identifier",
|
||||
name: "import",
|
||||
start: importStart,
|
||||
end: importStart + 6,
|
||||
end: deserializeI32(pos + 4) === 0 ? 0 : importStart + 6,
|
||||
},
|
||||
metaEnd = deserializeI32(pos + 4),
|
||||
metaIdent = {
|
||||
type: "Identifier",
|
||||
name: "meta",
|
||||
start: metaEnd - 4,
|
||||
start: metaEnd === 0 ? 0 : metaEnd - 4,
|
||||
end: metaEnd,
|
||||
};
|
||||
node.meta = importIdent;
|
||||
@@ -638,13 +638,13 @@ function deserializeNewTarget(pos) {
|
||||
type: "Identifier",
|
||||
name: "new",
|
||||
start: newStart,
|
||||
end: newStart + 3,
|
||||
end: deserializeI32(pos + 4) === 0 ? 0 : newStart + 3,
|
||||
},
|
||||
targetEnd = deserializeI32(pos + 4),
|
||||
targetIdent = {
|
||||
type: "Identifier",
|
||||
name: "target",
|
||||
start: targetEnd - 6,
|
||||
start: targetEnd === 0 ? 0 : targetEnd - 6,
|
||||
end: targetEnd,
|
||||
};
|
||||
node.meta = newIdent;
|
||||
|
||||
@@ -652,14 +652,14 @@ function deserializeImportMeta(pos) {
|
||||
type: "Identifier",
|
||||
name: "import",
|
||||
start: importStart,
|
||||
end: importStart + 6,
|
||||
end: deserializeI32(pos + 4) === 0 ? 0 : importStart + 6,
|
||||
parent,
|
||||
},
|
||||
metaEnd = deserializeI32(pos + 4),
|
||||
metaIdent = {
|
||||
type: "Identifier",
|
||||
name: "meta",
|
||||
start: metaEnd - 4,
|
||||
start: metaEnd === 0 ? 0 : metaEnd - 4,
|
||||
end: metaEnd,
|
||||
parent,
|
||||
};
|
||||
@@ -684,14 +684,14 @@ function deserializeNewTarget(pos) {
|
||||
type: "Identifier",
|
||||
name: "new",
|
||||
start: newStart,
|
||||
end: newStart + 3,
|
||||
end: deserializeI32(pos + 4) === 0 ? 0 : newStart + 3,
|
||||
parent,
|
||||
},
|
||||
targetEnd = deserializeI32(pos + 4),
|
||||
targetIdent = {
|
||||
type: "Identifier",
|
||||
name: "target",
|
||||
start: targetEnd - 6,
|
||||
start: targetEnd === 0 ? 0 : targetEnd - 6,
|
||||
end: targetEnd,
|
||||
parent,
|
||||
};
|
||||
|
||||
@@ -652,7 +652,7 @@ function deserializeImportMeta(pos) {
|
||||
range: [start, end],
|
||||
},
|
||||
importStart = deserializeI32(pos),
|
||||
importEnd = importStart + 6,
|
||||
importEnd = deserializeI32(pos + 4) === 0 ? 0 : importStart + 6,
|
||||
importIdent = {
|
||||
type: "Identifier",
|
||||
name: "import",
|
||||
@@ -661,7 +661,7 @@ function deserializeImportMeta(pos) {
|
||||
range: [importStart, importEnd],
|
||||
},
|
||||
metaEnd = deserializeI32(pos + 4),
|
||||
metaStart = metaEnd - 4,
|
||||
metaStart = metaEnd === 0 ? 0 : metaEnd - 4,
|
||||
metaIdent = {
|
||||
type: "Identifier",
|
||||
name: "meta",
|
||||
@@ -686,7 +686,7 @@ function deserializeNewTarget(pos) {
|
||||
range: [start, end],
|
||||
},
|
||||
newStart = deserializeI32(pos),
|
||||
newEnd = newStart + 3,
|
||||
newEnd = deserializeI32(pos + 4) === 0 ? 0 : newStart + 3,
|
||||
newIdent = {
|
||||
type: "Identifier",
|
||||
name: "new",
|
||||
@@ -695,7 +695,7 @@ function deserializeNewTarget(pos) {
|
||||
range: [newStart, newEnd],
|
||||
},
|
||||
targetEnd = deserializeI32(pos + 4),
|
||||
targetStart = targetEnd - 6,
|
||||
targetStart = targetEnd === 0 ? 0 : targetEnd - 6,
|
||||
targetIdent = {
|
||||
type: "Identifier",
|
||||
name: "target",
|
||||
|
||||
@@ -693,7 +693,7 @@ function deserializeImportMeta(pos) {
|
||||
parent,
|
||||
}),
|
||||
importStart = deserializeI32(pos),
|
||||
importEnd = importStart + 6,
|
||||
importEnd = deserializeI32(pos + 4) === 0 ? 0 : importStart + 6,
|
||||
importIdent = {
|
||||
type: "Identifier",
|
||||
name: "import",
|
||||
@@ -703,7 +703,7 @@ function deserializeImportMeta(pos) {
|
||||
parent,
|
||||
},
|
||||
metaEnd = deserializeI32(pos + 4),
|
||||
metaStart = metaEnd - 4,
|
||||
metaStart = metaEnd === 0 ? 0 : metaEnd - 4,
|
||||
metaIdent = {
|
||||
type: "Identifier",
|
||||
name: "meta",
|
||||
@@ -732,7 +732,7 @@ function deserializeNewTarget(pos) {
|
||||
parent,
|
||||
}),
|
||||
newStart = deserializeI32(pos),
|
||||
newEnd = newStart + 3,
|
||||
newEnd = deserializeI32(pos + 4) === 0 ? 0 : newStart + 3,
|
||||
newIdent = {
|
||||
type: "Identifier",
|
||||
name: "new",
|
||||
@@ -742,7 +742,7 @@ function deserializeNewTarget(pos) {
|
||||
parent,
|
||||
},
|
||||
targetEnd = deserializeI32(pos + 4),
|
||||
targetStart = targetEnd - 6,
|
||||
targetStart = targetEnd === 0 ? 0 : targetEnd - 6,
|
||||
targetIdent = {
|
||||
type: "Identifier",
|
||||
name: "target",
|
||||
|
||||
@@ -652,7 +652,7 @@ function deserializeImportMeta(pos) {
|
||||
optional: false,
|
||||
typeAnnotation: null,
|
||||
start: importStart,
|
||||
end: importStart + 6,
|
||||
end: deserializeI32(pos + 4) === 0 ? 0 : importStart + 6,
|
||||
},
|
||||
metaEnd = deserializeI32(pos + 4),
|
||||
metaIdent = {
|
||||
@@ -661,7 +661,7 @@ function deserializeImportMeta(pos) {
|
||||
name: "meta",
|
||||
optional: false,
|
||||
typeAnnotation: null,
|
||||
start: metaEnd - 4,
|
||||
start: metaEnd === 0 ? 0 : metaEnd - 4,
|
||||
end: metaEnd,
|
||||
};
|
||||
node.meta = importIdent;
|
||||
@@ -685,7 +685,7 @@ function deserializeNewTarget(pos) {
|
||||
optional: false,
|
||||
typeAnnotation: null,
|
||||
start: newStart,
|
||||
end: newStart + 3,
|
||||
end: deserializeI32(pos + 4) === 0 ? 0 : newStart + 3,
|
||||
},
|
||||
targetEnd = deserializeI32(pos + 4),
|
||||
targetIdent = {
|
||||
@@ -694,7 +694,7 @@ function deserializeNewTarget(pos) {
|
||||
name: "target",
|
||||
optional: false,
|
||||
typeAnnotation: null,
|
||||
start: targetEnd - 6,
|
||||
start: targetEnd === 0 ? 0 : targetEnd - 6,
|
||||
end: targetEnd,
|
||||
};
|
||||
node.meta = newIdent;
|
||||
|
||||
@@ -693,7 +693,7 @@ function deserializeImportMeta(pos) {
|
||||
optional: false,
|
||||
typeAnnotation: null,
|
||||
start: importStart,
|
||||
end: importStart + 6,
|
||||
end: deserializeI32(pos + 4) === 0 ? 0 : importStart + 6,
|
||||
parent,
|
||||
},
|
||||
metaEnd = deserializeI32(pos + 4),
|
||||
@@ -703,7 +703,7 @@ function deserializeImportMeta(pos) {
|
||||
name: "meta",
|
||||
optional: false,
|
||||
typeAnnotation: null,
|
||||
start: metaEnd - 4,
|
||||
start: metaEnd === 0 ? 0 : metaEnd - 4,
|
||||
end: metaEnd,
|
||||
parent,
|
||||
};
|
||||
@@ -731,7 +731,7 @@ function deserializeNewTarget(pos) {
|
||||
optional: false,
|
||||
typeAnnotation: null,
|
||||
start: newStart,
|
||||
end: newStart + 3,
|
||||
end: deserializeI32(pos + 4) === 0 ? 0 : newStart + 3,
|
||||
parent,
|
||||
},
|
||||
targetEnd = deserializeI32(pos + 4),
|
||||
@@ -741,7 +741,7 @@ function deserializeNewTarget(pos) {
|
||||
name: "target",
|
||||
optional: false,
|
||||
typeAnnotation: null,
|
||||
start: targetEnd - 6,
|
||||
start: targetEnd === 0 ? 0 : targetEnd - 6,
|
||||
end: targetEnd,
|
||||
parent,
|
||||
};
|
||||
|
||||
@@ -690,7 +690,7 @@ function deserializeImportMeta(pos) {
|
||||
range: [start, end],
|
||||
},
|
||||
importStart = deserializeI32(pos),
|
||||
importEnd = importStart + 6,
|
||||
importEnd = deserializeI32(pos + 4) === 0 ? 0 : importStart + 6,
|
||||
importIdent = {
|
||||
type: "Identifier",
|
||||
decorators: [],
|
||||
@@ -702,7 +702,7 @@ function deserializeImportMeta(pos) {
|
||||
range: [importStart, importEnd],
|
||||
},
|
||||
metaEnd = deserializeI32(pos + 4),
|
||||
metaStart = metaEnd - 4,
|
||||
metaStart = metaEnd === 0 ? 0 : metaEnd - 4,
|
||||
metaIdent = {
|
||||
type: "Identifier",
|
||||
decorators: [],
|
||||
@@ -730,7 +730,7 @@ function deserializeNewTarget(pos) {
|
||||
range: [start, end],
|
||||
},
|
||||
newStart = deserializeI32(pos),
|
||||
newEnd = newStart + 3,
|
||||
newEnd = deserializeI32(pos + 4) === 0 ? 0 : newStart + 3,
|
||||
newIdent = {
|
||||
type: "Identifier",
|
||||
decorators: [],
|
||||
@@ -742,7 +742,7 @@ function deserializeNewTarget(pos) {
|
||||
range: [newStart, newEnd],
|
||||
},
|
||||
targetEnd = deserializeI32(pos + 4),
|
||||
targetStart = targetEnd - 6,
|
||||
targetStart = targetEnd === 0 ? 0 : targetEnd - 6,
|
||||
targetIdent = {
|
||||
type: "Identifier",
|
||||
decorators: [],
|
||||
|
||||
@@ -731,7 +731,7 @@ function deserializeImportMeta(pos) {
|
||||
parent,
|
||||
}),
|
||||
importStart = deserializeI32(pos),
|
||||
importEnd = importStart + 6,
|
||||
importEnd = deserializeI32(pos + 4) === 0 ? 0 : importStart + 6,
|
||||
importIdent = {
|
||||
type: "Identifier",
|
||||
decorators: [],
|
||||
@@ -744,7 +744,7 @@ function deserializeImportMeta(pos) {
|
||||
parent,
|
||||
},
|
||||
metaEnd = deserializeI32(pos + 4),
|
||||
metaStart = metaEnd - 4,
|
||||
metaStart = metaEnd === 0 ? 0 : metaEnd - 4,
|
||||
metaIdent = {
|
||||
type: "Identifier",
|
||||
decorators: [],
|
||||
@@ -776,7 +776,7 @@ function deserializeNewTarget(pos) {
|
||||
parent,
|
||||
}),
|
||||
newStart = deserializeI32(pos),
|
||||
newEnd = newStart + 3,
|
||||
newEnd = deserializeI32(pos + 4) === 0 ? 0 : newStart + 3,
|
||||
newIdent = {
|
||||
type: "Identifier",
|
||||
decorators: [],
|
||||
@@ -789,7 +789,7 @@ function deserializeNewTarget(pos) {
|
||||
parent,
|
||||
},
|
||||
targetEnd = deserializeI32(pos + 4),
|
||||
targetStart = targetEnd - 6,
|
||||
targetStart = targetEnd === 0 ? 0 : targetEnd - 6,
|
||||
targetIdent = {
|
||||
type: "Identifier",
|
||||
decorators: [],
|
||||
|
||||
Reference in new issue
Block a user