Skip to content

Commit 5b422f0

Browse files
authored
fix: support binding patterns in variable declarations (#356)
1 parent 9b623e8 commit 5b422f0

3 files changed

Lines changed: 42 additions & 34 deletions

File tree

‎src/fake-js/exports.ts‎

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { b } from 'yuku'
22
import { is } from 'yuku/utils'
33
import { isRuntimeBindingVariableDeclaration } from './runtime-binding.ts'
4-
import { getIdFromTSEntityName, nameOf } from './utils.ts'
4+
import {
5+
collectPatternIdentifiers,
6+
getIdFromTSEntityName,
7+
nameOf,
8+
} from './utils.ts'
59
import type {
610
ChunkExportPlan,
711
DeclarationInfo,
@@ -65,7 +69,9 @@ function collectTypeOnlyLocals(
6569

6670
function collectDeclarationNames(node: t.Node): string[] {
6771
if (node.type === 'VariableDeclaration') {
68-
return node.declarations.flatMap((decl) => collectPatternNames(decl.id))
72+
return node.declarations.flatMap((decl) =>
73+
collectPatternIdentifiers(decl.id).map(({ name }) => name),
74+
)
6975
}
7076

7177
if ('id' in node && node.id) {
@@ -80,37 +86,6 @@ function collectDeclarationNames(node: t.Node): string[] {
8086
return []
8187
}
8288

83-
function collectPatternNames(node: t.Node | null | undefined): string[] {
84-
if (!node) return []
85-
86-
if (node.type === 'Identifier') {
87-
return [node.name]
88-
}
89-
90-
if (node.type === 'RestElement') {
91-
return collectPatternNames(node.argument)
92-
}
93-
94-
if (node.type === 'AssignmentPattern') {
95-
return collectPatternNames(node.left)
96-
}
97-
98-
if (node.type === 'ArrayPattern') {
99-
return node.elements.flatMap((element) => collectPatternNames(element))
100-
}
101-
102-
if (node.type === 'ObjectPattern') {
103-
return node.properties.flatMap((property) => {
104-
if (property.type === 'RestElement') {
105-
return collectPatternNames(property.argument)
106-
}
107-
return collectPatternNames(property.value)
108-
})
109-
}
110-
111-
return []
112-
}
113-
11489
function isTypeOnlyExport(
11590
node: t.ExportNamedDeclaration,
11691
specifier: t.ExportSpecifier,

‎src/fake-js/index.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
runtimeBindingArrayExpression,
2929
} from './runtime-binding.ts'
3030
import {
31+
collectPatternIdentifiers,
3132
collectReferenceDirectives,
3233
findDefaultExportRef,
3334
getIdentifierIndex,
@@ -298,8 +299,12 @@ export function createFakeJsPlugin({
298299

299300
const bindings: t.Identifier[] = []
300301
if (decl.type === 'VariableDeclaration') {
302+
// a binding pattern, e.g. `declare const [a, b]: [A, B]` emitted by
303+
// TypeScript 7.1, binds each of its identifiers
301304
bindings.push(
302-
...decl.declarations.map((decl) => decl.id as t.Identifier),
305+
...decl.declarations.flatMap((decl) =>
306+
collectPatternIdentifiers(decl.id),
307+
),
303308
)
304309
} else if ('id' in decl && decl.id) {
305310
let binding: t.Node = decl.id

‎src/fake-js/utils.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@ export function isInfer(node: t.Node): node is t.Identifier {
1313
return is.Identifier(node) && node.name === 'infer'
1414
}
1515

16+
/** Identifiers bound by a declaration id, e.g. `[a, { b }]` */
17+
export function collectPatternIdentifiers(
18+
node: t.Node | null | undefined,
19+
): t.Identifier[] {
20+
if (!node) return []
21+
22+
switch (node.type) {
23+
case 'Identifier':
24+
return [node]
25+
case 'RestElement':
26+
return collectPatternIdentifiers(node.argument)
27+
case 'AssignmentPattern':
28+
return collectPatternIdentifiers(node.left)
29+
case 'ArrayPattern':
30+
return node.elements.flatMap((element) =>
31+
collectPatternIdentifiers(element),
32+
)
33+
case 'ObjectPattern':
34+
return node.properties.flatMap((property) =>
35+
collectPatternIdentifiers(
36+
property.type === 'RestElement' ? property.argument : property.value,
37+
),
38+
)
39+
}
40+
41+
return []
42+
}
43+
1644
export function nameOf(node: t.ModuleExportName): string {
1745
return node.type === 'Identifier' ? node.name : node.value
1846
}

0 commit comments

Comments
 (0)