Skip to content

Commit 08589e7

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

File tree

233 files changed

+10462
-2774
lines changed

Some content is hidden

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

233 files changed

+10462
-2774
lines changed

apps/oxlint/src-js/generated/deserialize.js

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,15 +1599,17 @@ function deserializeVariableDeclarator(pos) {
15991599
__proto__: NodeProto,
16001600
type: 'VariableDeclarator',
16011601
id: null,
1602+
typeAnnotation: null,
16021603
init: null,
1603-
definite: deserializeBool(pos + 57),
1604+
definite: deserializeBool(pos + 49),
16041605
start,
16051606
end,
16061607
range: [start, end],
16071608
parent,
16081609
});
16091610
node.id = deserializeBindingPattern(pos + 8);
1610-
node.init = deserializeOptionExpression(pos + 40);
1611+
node.typeAnnotation = deserializeOptionBoxTSTypeAnnotation(pos + 24);
1612+
node.init = deserializeOptionExpression(pos + 32);
16111613
parent = previousParent;
16121614
return node;
16131615
}
@@ -2088,13 +2090,15 @@ function deserializeCatchClause(pos) {
20882090
parent,
20892091
});
20902092
node.param = deserializeOptionCatchParameter(pos + 8);
2091-
node.body = deserializeBoxBlockStatement(pos + 48);
2093+
node.body = deserializeBoxBlockStatement(pos + 40);
20922094
parent = previousParent;
20932095
return node;
20942096
}
20952097

20962098
function deserializeCatchParameter(pos) {
2097-
return deserializeBindingPattern(pos + 8);
2099+
let pattern = deserializeBindingPattern(pos + 8);
2100+
pattern.typeAnnotation = deserializeOptionBoxTSTypeAnnotation(pos + 24);
2101+
return pattern;
20982102
}
20992103

21002104
function deserializeDebuggerStatement(pos) {
@@ -2111,18 +2115,6 @@ function deserializeDebuggerStatement(pos) {
21112115
}
21122116

21132117
function deserializeBindingPattern(pos) {
2114-
let pattern = deserializeBindingPatternKind(pos);
2115-
{
2116-
let previousParent = parent;
2117-
parent = pattern;
2118-
pattern.optional = deserializeBool(pos + 24);
2119-
pattern.typeAnnotation = deserializeOptionBoxTSTypeAnnotation(pos + 16);
2120-
parent = previousParent;
2121-
}
2122-
return pattern;
2123-
}
2124-
2125-
function deserializeBindingPatternKind(pos) {
21262118
switch (uint8[pos]) {
21272119
case 0:
21282120
return deserializeBoxBindingIdentifier(pos + 8);
@@ -2133,7 +2125,7 @@ function deserializeBindingPatternKind(pos) {
21332125
case 3:
21342126
return deserializeBoxAssignmentPattern(pos + 8);
21352127
default:
2136-
throw Error(`Unexpected discriminant ${uint8[pos]} for BindingPatternKind`);
2128+
throw Error(`Unexpected discriminant ${uint8[pos]} for BindingPattern`);
21372129
}
21382130
}
21392131

@@ -2156,7 +2148,7 @@ function deserializeAssignmentPattern(pos) {
21562148
});
21572149
node.decorators = [];
21582150
node.left = deserializeBindingPattern(pos + 8);
2159-
node.right = deserializeExpression(pos + 40);
2151+
node.right = deserializeExpression(pos + 24);
21602152
node.optional = false;
21612153
parent = previousParent;
21622154
return node;
@@ -2199,8 +2191,8 @@ function deserializeBindingProperty(pos) {
21992191
key: null,
22002192
value: null,
22012193
method: null,
2202-
shorthand: deserializeBool(pos + 56),
2203-
computed: deserializeBool(pos + 57),
2194+
shorthand: deserializeBool(pos + 40),
2195+
computed: deserializeBool(pos + 41),
22042196
optional: null,
22052197
start,
22062198
end,
@@ -2329,16 +2321,16 @@ function deserializeFormalParameters(pos) {
23292321
type: 'RestElement',
23302322
decorators: [],
23312323
argument: null,
2332-
optional: deserializeBool(pos + 32),
2324+
optional: false,
23332325
typeAnnotation: null,
23342326
value: null,
23352327
start: (start = deserializeU32(pos)),
23362328
end: (end = deserializeU32(pos + 4)),
23372329
range: [start, end],
23382330
parent,
23392331
});
2340-
rest.argument = deserializeBindingPatternKind(pos + 8);
2341-
rest.typeAnnotation = deserializeOptionBoxTSTypeAnnotation(pos + 24);
2332+
rest.argument = deserializeBindingPattern(pos + 16);
2333+
rest.typeAnnotation = deserializeOptionBoxTSTypeAnnotation(pos + 32);
23422334
params.push(rest);
23432335
parent = previousParent;
23442336
}
@@ -2348,14 +2340,14 @@ function deserializeFormalParameters(pos) {
23482340
function deserializeFormalParameter(pos) {
23492341
let param;
23502342
{
2351-
let accessibility = deserializeOptionTSAccessibility(pos + 64),
2352-
readonly = deserializeBool(pos + 65),
2353-
override = deserializeBool(pos + 66),
2343+
let accessibility = deserializeOptionTSAccessibility(pos + 65),
2344+
readonly = deserializeBool(pos + 66),
2345+
override = deserializeBool(pos + 67),
23542346
previousParent = parent;
23552347
if (accessibility === null && !readonly && !override) {
2356-
param = parent = deserializeBindingPatternKind(pos + 32);
2348+
param = parent = deserializeBindingPattern(pos + 32);
23572349
param.decorators = deserializeVecDecorator(pos + 8);
2358-
param.optional = deserializeBool(pos + 56);
2350+
param.optional = deserializeBool(pos + 64);
23592351
param.typeAnnotation = deserializeOptionBoxTSTypeAnnotation(pos + 48);
23602352
} else {
23612353
let start, end;
@@ -6308,14 +6300,23 @@ function deserializeVecVariableDeclarator(pos) {
63086300
let arr = [],
63096301
pos32 = pos >> 2;
63106302
pos = uint32[pos32];
6311-
let endPos = pos + uint32[pos32 + 2] * 64;
6303+
let endPos = pos + uint32[pos32 + 2] * 56;
63126304
for (; pos !== endPos; ) {
63136305
arr.push(deserializeVariableDeclarator(pos));
6314-
pos += 64;
6306+
pos += 56;
63156307
}
63166308
return arr;
63176309
}
63186310

6311+
function deserializeBoxTSTypeAnnotation(pos) {
6312+
return deserializeTSTypeAnnotation(uint32[pos >> 2]);
6313+
}
6314+
6315+
function deserializeOptionBoxTSTypeAnnotation(pos) {
6316+
if (uint32[pos >> 2] === 0 && uint32[(pos + 4) >> 2] === 0) return null;
6317+
return deserializeBoxTSTypeAnnotation(pos);
6318+
}
6319+
63196320
function deserializeOptionStatement(pos) {
63206321
if (uint8[pos] === 70) return null;
63216322
return deserializeStatement(pos);
@@ -6358,19 +6359,10 @@ function deserializeOptionBoxBlockStatement(pos) {
63586359
}
63596360

63606361
function deserializeOptionCatchParameter(pos) {
6361-
if (uint8[pos + 32] === 2) return null;
6362+
if (uint8[pos + 8] === 4) return null;
63626363
return deserializeCatchParameter(pos);
63636364
}
63646365

6365-
function deserializeBoxTSTypeAnnotation(pos) {
6366-
return deserializeTSTypeAnnotation(uint32[pos >> 2]);
6367-
}
6368-
6369-
function deserializeOptionBoxTSTypeAnnotation(pos) {
6370-
if (uint32[pos >> 2] === 0 && uint32[(pos + 4) >> 2] === 0) return null;
6371-
return deserializeBoxTSTypeAnnotation(pos);
6372-
}
6373-
63746366
function deserializeBoxBindingIdentifier(pos) {
63756367
return deserializeBindingIdentifier(uint32[pos >> 2]);
63766368
}
@@ -6391,10 +6383,10 @@ function deserializeVecBindingProperty(pos) {
63916383
let arr = [],
63926384
pos32 = pos >> 2;
63936385
pos = uint32[pos32];
6394-
let endPos = pos + uint32[pos32 + 2] * 64;
6386+
let endPos = pos + uint32[pos32 + 2] * 48;
63956387
for (; pos !== endPos; ) {
63966388
arr.push(deserializeBindingProperty(pos));
6397-
pos += 64;
6389+
pos += 48;
63986390
}
63996391
return arr;
64006392
}
@@ -6409,18 +6401,18 @@ function deserializeOptionBoxBindingRestElement(pos) {
64096401
}
64106402

64116403
function deserializeOptionBindingPattern(pos) {
6412-
if (uint8[pos + 24] === 2) return null;
6404+
if (uint8[pos] === 4) return null;
64136405
return deserializeBindingPattern(pos);
64146406
}
64156407

64166408
function deserializeVecOptionBindingPattern(pos) {
64176409
let arr = [],
64186410
pos32 = pos >> 2;
64196411
pos = uint32[pos32];
6420-
let endPos = pos + uint32[pos32 + 2] * 32;
6412+
let endPos = pos + uint32[pos32 + 2] * 16;
64216413
for (; pos !== endPos; ) {
64226414
arr.push(deserializeOptionBindingPattern(pos));
6423-
pos += 32;
6415+
pos += 16;
64246416
}
64256417
return arr;
64266418
}

apps/oxlint/src-js/generated/types.d.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ export type VariableDeclarationKind = 'var' | 'let' | 'const' | 'using' | 'await
448448
export interface VariableDeclarator extends Span {
449449
type: 'VariableDeclarator';
450450
id: BindingPattern;
451+
typeAnnotation?: TSTypeAnnotation | null;
451452
init: Expression | null;
452453
definite?: boolean;
453454
parent: Node;
@@ -589,12 +590,7 @@ export interface DebuggerStatement extends Span {
589590
parent: Node;
590591
}
591592

592-
export type BindingPattern = {
593-
optional?: boolean;
594-
typeAnnotation?: TSTypeAnnotation | null;
595-
} & (BindingIdentifier | ObjectPattern | ArrayPattern | AssignmentPattern);
596-
597-
export type BindingPatternKind = BindingIdentifier | ObjectPattern | ArrayPattern | AssignmentPattern;
593+
export type BindingPattern = BindingIdentifier | ObjectPattern | ArrayPattern | AssignmentPattern;
598594

599595
export interface AssignmentPattern extends Span {
600596
type: 'AssignmentPattern';

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".

0 commit comments

Comments
 (0)