Skip to content

Commit a46b36c

Browse files
committed
refactor(all): remove binding pattern wierdness
1 parent 4b9d8d2 commit a46b36c

File tree

231 files changed

+10423
-2723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

231 files changed

+10423
-2723
lines changed

crates/oxc_ast/src/ast/js.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ pub struct IdentifierReference<'a> {
270270
///
271271
/// See: [13.1 Identifiers](https://tc39.es/ecma262/#sec-identifiers)
272272
///
273-
/// Also see other examples in docs for [`BindingPatternKind`].
273+
/// Also see other examples in docs for [`BindingPattern`].
274274
#[ast(visit)]
275275
#[derive(Debug, Clone)]
276276
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree, UnstableAddress)]
@@ -1230,6 +1230,8 @@ pub struct VariableDeclarator<'a> {
12301230
#[estree(skip)]
12311231
pub kind: VariableDeclarationKind,
12321232
pub id: BindingPattern<'a>,
1233+
#[ts]
1234+
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
12331235
pub init: Option<Expression<'a>>,
12341236
#[ts]
12351237
pub definite: bool,
@@ -1521,6 +1523,8 @@ pub struct CatchParameter<'a> {
15211523
/// The bound error
15221524
#[estree(flatten)]
15231525
pub pattern: BindingPattern<'a>,
1526+
#[ts]
1527+
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
15241528
}
15251529

15261530
/// Debugger Statement
@@ -1537,36 +1541,10 @@ pub struct DebuggerStatement {
15371541
pub span: Span,
15381542
}
15391543

1540-
/// Destructuring Binding Patterns.
1541-
/// <https://tc39.es/ecma262/#prod-BindingPattern>
1542-
///
1543-
/// Binding patterns can be nested within other binding patterns
1544-
/// e.g. `const [ { a: [ {x} ] } ] = arr;`.
1545-
///
1546-
/// Type annotations are valid in some positions e.g. `const x: T = f();`
1547-
/// but invalid in others e.g. `const [x: T] = f();`.
1548-
#[ast(visit)]
1549-
#[derive(Debug)]
1550-
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree, UnstableAddress)]
1551-
#[estree(no_type, via = BindingPatternConverter, field_order(kind, optional, type_annotation))]
1552-
pub struct BindingPattern<'a> {
1553-
// estree(flatten) the attributes because estree has no `BindingPattern`
1554-
#[estree(
1555-
flatten,
1556-
ts_type = "(BindingIdentifier | ObjectPattern | ArrayPattern | AssignmentPattern)"
1557-
)]
1558-
#[span]
1559-
pub kind: BindingPatternKind<'a>,
1560-
#[ts]
1561-
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
1562-
#[ts]
1563-
pub optional: bool,
1564-
}
1565-
15661544
#[ast(visit)]
15671545
#[derive(Debug)]
15681546
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, GetAddress, ContentEq, ESTree)]
1569-
pub enum BindingPatternKind<'a> {
1547+
pub enum BindingPattern<'a> {
15701548
/// `x` in `const x = 1;`.
15711549
///
15721550
/// Also e.g. `x` in:
@@ -1617,7 +1595,7 @@ pub enum BindingPatternKind<'a> {
16171595

16181596
/// `x = 1` in `const {x = 1} = obj;`.
16191597
///
1620-
/// See other examples in docs for [`BindingPatternKind`].
1598+
/// See other examples in docs for [`BindingPattern`].
16211599
#[ast(visit)]
16221600
#[derive(Debug)]
16231601
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree, UnstableAddress)]
@@ -1633,7 +1611,7 @@ pub struct AssignmentPattern<'a> {
16331611

16341612
/// `{x}` in `const {x} = 1;`.
16351613
///
1636-
/// See other examples in docs for [`BindingPatternKind`].
1614+
/// See other examples in docs for [`BindingPattern`].
16371615
#[ast(visit)]
16381616
#[derive(Debug)]
16391617
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree, UnstableAddress)]
@@ -1666,7 +1644,7 @@ pub struct BindingProperty<'a> {
16661644

16671645
/// `[x]` in `const [x] = 1;`
16681646
///
1669-
/// See other examples in docs for [`BindingPatternKind`].
1647+
/// See other examples in docs for [`BindingPattern`].
16701648
#[ast(visit)]
16711649
#[derive(Debug)]
16721650
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree, UnstableAddress)]
@@ -1863,7 +1841,7 @@ pub struct FormalParameters<'a> {
18631841
#[estree(ts_type = "Array<FormalParameter | TSParameterProperty | FormalParameterRest>")]
18641842
pub items: Vec<'a, FormalParameter<'a>>,
18651843
#[estree(skip)]
1866-
pub rest: Option<Box<'a, BindingRestElement<'a>>>,
1844+
pub rest: Option<Box<'a, FormalParameterRest<'a>>>,
18671845
}
18681846

18691847
#[ast(visit)]
@@ -1901,6 +1879,11 @@ pub struct FormalParameter<'a> {
19011879
#[estree(flatten)]
19021880
pub pattern: BindingPattern<'a>,
19031881
#[ts]
1882+
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
1883+
pub initializer: Option<Box<'a, Expression<'a>>>,
1884+
#[ts]
1885+
pub optional: bool,
1886+
#[ts]
19041887
#[estree(skip)]
19051888
pub accessibility: Option<TSAccessibility>,
19061889
#[ts]
@@ -1926,6 +1909,18 @@ pub enum FormalParameterKind {
19261909
Signature = 3,
19271910
}
19281911

1912+
/// Rest parameter in a function's formal parameters.
1913+
///
1914+
/// Wrapper around [`BindingRestElement`] with optional type annotation for TypeScript.
1915+
#[ast(visit)]
1916+
#[derive(Debug)]
1917+
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, UnstableAddress)]
1918+
pub struct FormalParameterRest<'a> {
1919+
pub span: Span,
1920+
pub rest: BindingRestElement<'a>,
1921+
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
1922+
}
1923+
19291924
/// <https://tc39.es/ecma262/#prod-FunctionBody>
19301925
#[ast(visit)]
19311926
#[derive(Debug)]

crates/oxc_ast/src/ast_builder_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'a> AstBuilder<'a> {
160160
span: Span,
161161
pattern: BindingPattern<'a>,
162162
) -> FormalParameter<'a> {
163-
self.formal_parameter(span, self.vec(), pattern, None, false, false)
163+
self.formal_parameter(span, self.vec(), pattern, NONE, NONE, false, None, false, false)
164164
}
165165

166166
/// Create a [`Function`] with no "extras".

crates/oxc_ast/src/ast_impl/js.rs

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,43 +1247,6 @@ impl SwitchCase<'_> {
12471247
}
12481248

12491249
impl<'a> BindingPattern<'a> {
1250-
/// Returns the name of the bound identifier in this binding pattern, if it has one, or `None` otherwise.
1251-
///
1252-
/// ## Example
1253-
///
1254-
/// - calling on `a = 1` in `let a = 1` would return `Some("a")`
1255-
/// - calling on `a = 1` in `let {a = 1} = c` would return `Some("a")`
1256-
/// - calling on `a: b` in `let {a: b} = c` would return `None`
1257-
pub fn get_identifier_name(&self) -> Option<Atom<'a>> {
1258-
self.kind.get_identifier_name()
1259-
}
1260-
1261-
/// Returns the bound identifier in this binding pattern, if it has one, or `None` otherwise.
1262-
///
1263-
/// To just get the name of the bound identifier, use [`BindingPattern::get_identifier_name`].
1264-
///
1265-
/// ## Example
1266-
///
1267-
/// - calling on `a = 1` in `let a = 1` would return `Some(BindingIdentifier { name: "a", .. })`
1268-
/// - calling on `a = 1` in `let {a = 1} = c` would return `Some(BindingIdentifier { name: "a", .. })`
1269-
/// - calling on `a: b` in `let {a: b} = c` would return `None`
1270-
pub fn get_binding_identifier(&self) -> Option<&BindingIdentifier<'a>> {
1271-
self.kind.get_binding_identifier()
1272-
}
1273-
1274-
/// Returns the bound identifiers in this binding pattern.
1275-
///
1276-
/// ## Example
1277-
///
1278-
/// - `let {} = obj` would return `[]`
1279-
/// - `let {a, b} = obj` would return `[a, b]`
1280-
/// - `let {a = 1, b: c} = obj` would return `[a, c]`
1281-
pub fn get_binding_identifiers(&self) -> std::vec::Vec<&BindingIdentifier<'a>> {
1282-
self.kind.get_binding_identifiers()
1283-
}
1284-
}
1285-
1286-
impl<'a> BindingPatternKind<'a> {
12871250
/// Returns the name of the bound identifier in this binding pattern, if it has one, or `None` otherwise.
12881251
///
12891252
/// ## Example
@@ -1322,23 +1285,23 @@ impl<'a> BindingPatternKind<'a> {
13221285
) {
13231286
match self {
13241287
Self::BindingIdentifier(ident) => idents.push(ident),
1325-
Self::AssignmentPattern(assign) => assign.left.kind.append_binding_identifiers(idents),
1288+
Self::AssignmentPattern(assign) => assign.left.append_binding_identifiers(idents),
13261289
Self::ArrayPattern(pattern) => {
13271290
pattern
13281291
.elements
13291292
.iter()
13301293
.filter_map(|item| item.as_ref())
1331-
.for_each(|item| item.kind.append_binding_identifiers(idents));
1294+
.for_each(|item| item.append_binding_identifiers(idents));
13321295
if let Some(rest) = &pattern.rest {
1333-
rest.argument.kind.append_binding_identifiers(idents);
1296+
rest.argument.append_binding_identifiers(idents);
13341297
}
13351298
}
13361299
Self::ObjectPattern(pattern) => {
13371300
pattern.properties.iter().for_each(|item| {
1338-
item.value.kind.append_binding_identifiers(idents);
1301+
item.value.append_binding_identifiers(idents);
13391302
});
13401303
if let Some(rest) = &pattern.rest {
1341-
rest.argument.kind.append_binding_identifiers(idents);
1304+
rest.argument.append_binding_identifiers(idents);
13421305
}
13431306
}
13441307
}
@@ -1368,7 +1331,7 @@ impl<'a> BindingPatternKind<'a> {
13681331
pub fn is_destructuring_pattern(&self) -> bool {
13691332
match self {
13701333
Self::ObjectPattern(_) | Self::ArrayPattern(_) => true,
1371-
Self::AssignmentPattern(pattern) => pattern.left.kind.is_destructuring_pattern(),
1334+
Self::AssignmentPattern(pattern) => pattern.left.is_destructuring_pattern(),
13721335
Self::BindingIdentifier(_) => false,
13731336
}
13741337
}
@@ -1475,7 +1438,7 @@ impl<'a> FormalParameters<'a> {
14751438
self.items
14761439
.iter()
14771440
.map(|param| &param.pattern)
1478-
.chain(self.rest.iter().map(|rest| &rest.argument))
1441+
.chain(self.rest.iter().map(|param| &param.rest.argument))
14791442
}
14801443
}
14811444

crates/oxc_ast/src/ast_kind_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ impl AstKind<'_> {
477477
p.pattern.get_identifier_name().unwrap_or(Atom::from(DESTRUCTURE.as_ref()))
478478
)
479479
.into(),
480+
Self::FormalParameterRest(_) => "FormalParameterRest".into(),
480481
Self::CatchParameter(_) => "CatchParameter".into(),
481482

482483
Self::Class(c) => format!("Class({})", or_anonymous(c.id.as_ref())).into(),

0 commit comments

Comments
 (0)