Skip to content

Commit 1f85f1a

Browse files
committed
refactor(ast)!: revert adding span field to the BindingPattern type. (#3899)
Since this is a temporary solution in the time that we are waiting for the `#[span]` hint, And there are already other workarounds used in our `ast_codegen` I propose removing it right away - sorry in my opinion adding it in the first place was a mistake - in favor of adding an edge case in the codegen. It is better to do the refactoring in the codegen instead of the production code which people may depend on.
1 parent 4bf405d commit 1f85f1a

File tree

17 files changed

+19
-48
lines changed

17 files changed

+19
-48
lines changed

crates/oxc_ast/src/ast/js.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,6 @@ pub struct DebuggerStatement {
13271327
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
13281328
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
13291329
pub struct BindingPattern<'a> {
1330-
#[cfg_attr(feature = "serialize", serde(skip))]
1331-
pub span: Span,
13321330
// serde(flatten) the attributes because estree has no `BindingPattern`
13331331
#[cfg_attr(feature = "serialize", serde(flatten))]
13341332
#[cfg_attr(

crates/oxc_ast/src/ast_builder.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1191,12 +1191,11 @@ impl<'a> AstBuilder<'a> {
11911191
#[inline]
11921192
pub fn binding_pattern(
11931193
self,
1194-
span: Span,
11951194
kind: BindingPatternKind<'a>,
11961195
type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
11971196
optional: bool,
11981197
) -> BindingPattern<'a> {
1199-
BindingPattern { span, kind, type_annotation, optional }
1198+
BindingPattern { kind, type_annotation, optional }
12001199
}
12011200

12021201
#[inline]
@@ -1257,7 +1256,6 @@ impl<'a> AstBuilder<'a> {
12571256
) -> BindingPattern<'a> {
12581257
let pattern = self.alloc(AssignmentPattern { span, left, right });
12591258
BindingPattern {
1260-
span,
12611259
kind: BindingPatternKind::AssignmentPattern(pattern),
12621260
type_annotation: None,
12631261
optional: false,

crates/oxc_ast/src/ast_impl/js.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,8 @@ impl<'a> Hash for CatchClause<'a> {
872872
}
873873

874874
impl<'a> BindingPattern<'a> {
875-
pub fn new_with_kind(span: Span, kind: BindingPatternKind<'a>) -> Self {
876-
Self { span, kind, type_annotation: None, optional: false }
875+
pub fn new_with_kind(kind: BindingPatternKind<'a>) -> Self {
876+
Self { kind, type_annotation: None, optional: false }
877877
}
878878

879879
pub fn get_identifier(&self) -> Option<Atom<'a>> {

crates/oxc_isolated_declarations/src/class.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl<'a> IsolatedDeclarations<'a> {
505505
kind: BindingPatternKind<'a>,
506506
type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
507507
) -> Box<'a, FormalParameters<'a>> {
508-
let pattern = BindingPattern { span: SPAN, kind, type_annotation, optional: false };
508+
let pattern = BindingPattern { kind, type_annotation, optional: false };
509509
let parameter =
510510
self.ast.formal_parameter(SPAN, pattern, None, false, false, self.ast.new_vec());
511511
let items = self.ast.new_vec_single(parameter);

crates/oxc_isolated_declarations/src/declaration.rs

-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ impl<'a> IsolatedDeclarations<'a> {
9595
|| self.ast.copy(&decl.id),
9696
|ts_type| {
9797
self.ast.binding_pattern(
98-
SPAN,
9998
self.ast.copy(&decl.id.kind),
10099
Some(self.ast.ts_type_annotation(SPAN, ts_type)),
101100
decl.id.optional,

crates/oxc_isolated_declarations/src/function.rs

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ impl<'a> IsolatedDeclarations<'a> {
9999
});
100100

101101
pattern = self.ast.binding_pattern(
102-
SPAN,
103102
self.ast.copy(&pattern.kind),
104103
type_annotation,
105104
// if it's assignment pattern, it's optional

crates/oxc_isolated_declarations/src/module.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ impl<'a> IsolatedDeclarations<'a> {
6565
self.error(default_export_inferred(expr.span()));
6666
}
6767

68-
let id =
69-
BindingPattern { span: SPAN, kind: id, type_annotation, optional: false };
68+
let id = BindingPattern { kind: id, type_annotation, optional: false };
7069
let declarations = self
7170
.ast
7271
.new_vec_single(self.ast.variable_declarator(SPAN, kind, id, None, true));

crates/oxc_parser/src/js/arrow.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,9 @@ impl<'a> ParserImpl<'a> {
216216
}
217217
_ => unreachable!(),
218218
};
219-
let span = ident.span;
220-
let params_span = self.end_span(span);
219+
let params_span = self.end_span(ident.span);
221220
let ident = self.ast.binding_pattern_identifier(ident);
222-
let pattern = self.ast.binding_pattern(span, ident, None, false);
221+
let pattern = self.ast.binding_pattern(ident, None, false);
223222
let formal_parameter = self.ast.plain_formal_parameter(params_span, pattern);
224223
self.ast.formal_parameters(
225224
params_span,

crates/oxc_parser/src/js/binding.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ impl<'a> ParserImpl<'a> {
2020
&mut self,
2121
allow_question: bool,
2222
) -> Result<BindingPattern<'a>> {
23-
let span = self.start_span();
2423
let mut kind = self.parse_binding_pattern_kind()?;
2524
let optional =
2625
if allow_question && self.ts_enabled() { self.eat(Kind::Question) } else { false };
2726
let type_annotation = self.parse_ts_type_annotation()?;
2827
if let Some(type_annotation) = &type_annotation {
2928
Self::extend_binding_pattern_span_end(type_annotation.span, &mut kind);
3029
}
31-
Ok(self.ast.binding_pattern(self.end_span(span), kind, type_annotation, optional))
30+
Ok(self.ast.binding_pattern(kind, type_annotation, optional))
3231
}
3332

3433
pub(crate) fn parse_binding_pattern_kind(&mut self) -> Result<BindingPatternKind<'a>> {
@@ -73,7 +72,7 @@ impl<'a> ParserImpl<'a> {
7372
}
7473
// The span is not extended to its type_annotation
7574
let type_annotation = self.parse_ts_type_annotation()?;
76-
let pattern = self.ast.binding_pattern(self.end_span(span), kind, type_annotation, false);
75+
let pattern = self.ast.binding_pattern(kind, type_annotation, false);
7776
// Rest element does not allow `= initializer`, .
7877
let argument = self
7978
.context(Context::In, Context::empty(), |p| p.parse_initializer(init_span, pattern))?;
@@ -110,7 +109,7 @@ impl<'a> ParserImpl<'a> {
110109
shorthand = true;
111110
let binding_identifier = BindingIdentifier::new(ident.span, ident.name.clone());
112111
let identifier = self.ast.binding_pattern_identifier(binding_identifier);
113-
let left = self.ast.binding_pattern(ident.span, identifier, None, false);
112+
let left = self.ast.binding_pattern(identifier, None, false);
114113
self.context(Context::In, Context::empty(), |p| p.parse_initializer(span, left))?
115114
} else {
116115
return Err(self.unexpected());

crates/oxc_parser/src/js/declaration.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,9 @@ impl<'a> ParserImpl<'a> {
114114
if let Some(type_annotation) = &type_annotation {
115115
Self::extend_binding_pattern_span_end(type_annotation.span, &mut binding_kind);
116116
}
117-
(
118-
self.ast.binding_pattern(
119-
self.end_span(span),
120-
binding_kind,
121-
type_annotation,
122-
optional,
123-
),
124-
definite,
125-
)
117+
(self.ast.binding_pattern(binding_kind, type_annotation, optional), definite)
126118
} else {
127-
(self.ast.binding_pattern(self.end_span(span), binding_kind, None, false), false)
119+
(self.ast.binding_pattern(binding_kind, None, false), false)
128120
};
129121

130122
let init =

crates/oxc_transformer/src/es2015/arrow_functions.rs

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ impl<'a> ArrowFunctions<'a> {
114114

115115
if let Some(id) = &self.this_var {
116116
let binding_pattern = self.ctx.ast.binding_pattern(
117-
SPAN,
118117
self.ctx.ast.binding_pattern_identifier(id.create_binding_identifier()),
119118
None,
120119
false,

crates/oxc_transformer/src/helpers/module_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<'a> ModuleImports<'a> {
134134
name: name.imported,
135135
symbol_id: Cell::new(Some(name.symbol_id)),
136136
};
137-
self.ast.binding_pattern(SPAN, self.ast.binding_pattern_identifier(ident), None, false)
137+
self.ast.binding_pattern(self.ast.binding_pattern_identifier(ident), None, false)
138138
};
139139
let decl = {
140140
let init = self.ast.call_expression(SPAN, callee, args, false, None);

crates/oxc_transformer/src/react/jsx_source.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<'a> ReactJsxSource<'a> {
152152
let id = {
153153
let ident = filename_var.create_binding_identifier();
154154
let ident = self.ctx.ast.binding_pattern_identifier(ident);
155-
self.ctx.ast.binding_pattern(SPAN, ident, None, false)
155+
self.ctx.ast.binding_pattern(ident, None, false)
156156
};
157157
let decl = {
158158
let string = self.ctx.ast.string_literal(SPAN, &self.ctx.source_path.to_string_lossy());

crates/oxc_transformer/src/typescript/enum.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a> TypeScriptEnum<'a> {
6767
let span = decl.span;
6868
let ident = decl.id.clone();
6969
let kind = self.ctx.ast.binding_pattern_identifier(ident);
70-
let id = self.ctx.ast.binding_pattern(SPAN, kind, None, false);
70+
let id = self.ctx.ast.binding_pattern(kind, None, false);
7171

7272
// ((Foo) => {
7373
let params =
@@ -127,7 +127,7 @@ impl<'a> TypeScriptEnum<'a> {
127127

128128
let binding_identifier = BindingIdentifier::new(SPAN, enum_name.clone());
129129
let binding_pattern_kind = self.ctx.ast.binding_pattern_identifier(binding_identifier);
130-
let binding = self.ctx.ast.binding_pattern(SPAN, binding_pattern_kind, None, false);
130+
let binding = self.ctx.ast.binding_pattern(binding_pattern_kind, None, false);
131131
let decl =
132132
self.ctx.ast.variable_declarator(SPAN, kind, binding, Some(call_expression), false);
133133

crates/oxc_transformer/src/typescript/module.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'a> TypeScript<'a> {
3535
let decls = {
3636
let binding_identifier = BindingIdentifier::new(SPAN, decl.id.name.clone());
3737
let binding_pattern_kind = self.ctx.ast.binding_pattern_identifier(binding_identifier);
38-
let binding = self.ctx.ast.binding_pattern(SPAN, binding_pattern_kind, None, false);
38+
let binding = self.ctx.ast.binding_pattern(binding_pattern_kind, None, false);
3939
let decl_span = decl.span;
4040

4141
let init = match &mut decl.module_reference {

crates/oxc_transformer/src/typescript/namespace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl<'a> TypeScript<'a> {
282282
let declarations = {
283283
let ident = BindingIdentifier::new(SPAN, name);
284284
let pattern_kind = self.ctx.ast.binding_pattern_identifier(ident);
285-
let binding = self.ctx.ast.binding_pattern(SPAN, pattern_kind, None, false);
285+
let binding = self.ctx.ast.binding_pattern(pattern_kind, None, false);
286286
let decl = self.ctx.ast.variable_declarator(SPAN, kind, binding, None, false);
287287
self.ctx.ast.new_vec_single(decl)
288288
};
@@ -314,7 +314,7 @@ impl<'a> TypeScript<'a> {
314314
let params = {
315315
let ident =
316316
self.ctx.ast.binding_pattern_identifier(BindingIdentifier::new(SPAN, arg_name));
317-
let pattern = self.ctx.ast.binding_pattern(SPAN, ident, None, false);
317+
let pattern = self.ctx.ast.binding_pattern(ident, None, false);
318318
let items =
319319
self.ctx.ast.new_vec_single(self.ctx.ast.plain_formal_parameter(SPAN, pattern));
320320
self.ctx.ast.formal_parameters(

crates/oxc_traverse/src/ancestor.rs

-11
Original file line numberDiff line numberDiff line change
@@ -4922,7 +4922,6 @@ impl<'a> CatchParameterWithoutPattern<'a> {
49224922
}
49234923
}
49244924

4925-
pub(crate) const OFFSET_BINDING_PATTERN_SPAN: usize = offset_of!(BindingPattern, span);
49264925
pub(crate) const OFFSET_BINDING_PATTERN_KIND: usize = offset_of!(BindingPattern, kind);
49274926
pub(crate) const OFFSET_BINDING_PATTERN_TYPE_ANNOTATION: usize =
49284927
offset_of!(BindingPattern, type_annotation);
@@ -4933,11 +4932,6 @@ pub(crate) const OFFSET_BINDING_PATTERN_OPTIONAL: usize = offset_of!(BindingPatt
49334932
pub struct BindingPatternWithoutKind<'a>(pub(crate) *const BindingPattern<'a>);
49344933

49354934
impl<'a> BindingPatternWithoutKind<'a> {
4936-
#[inline]
4937-
pub fn span(&self) -> &Span {
4938-
unsafe { &*((self.0 as *const u8).add(OFFSET_BINDING_PATTERN_SPAN) as *const Span) }
4939-
}
4940-
49414935
#[inline]
49424936
pub fn type_annotation(&self) -> &Option<Box<'a, TSTypeAnnotation<'a>>> {
49434937
unsafe {
@@ -4957,11 +4951,6 @@ impl<'a> BindingPatternWithoutKind<'a> {
49574951
pub struct BindingPatternWithoutTypeAnnotation<'a>(pub(crate) *const BindingPattern<'a>);
49584952

49594953
impl<'a> BindingPatternWithoutTypeAnnotation<'a> {
4960-
#[inline]
4961-
pub fn span(&self) -> &Span {
4962-
unsafe { &*((self.0 as *const u8).add(OFFSET_BINDING_PATTERN_SPAN) as *const Span) }
4963-
}
4964-
49654954
#[inline]
49664955
pub fn kind(&self) -> &BindingPatternKind<'a> {
49674956
unsafe {

0 commit comments

Comments
 (0)