Skip to content

Commit df82c97

Browse files
committed
Try out array destructuring
1 parent 2cffe00 commit df82c97

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

resources/build-npm.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ts from 'typescript';
66

77
import { changeExtensionInImportPaths } from './change-extension-in-import-paths.js';
88
import { inlineInvariant } from './inline-invariant.js';
9+
import { optimizeArrayDestructuring } from './optimize-array-destructuring.js';
910
import { optimizeForOf } from './optimize-for-of.js';
1011
import {
1112
prettify,
@@ -146,7 +147,7 @@ function emitTSFiles(options: {
146147

147148
const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost);
148149
const tsResult = tsProgram.emit(undefined, undefined, undefined, undefined, {
149-
before: [optimizeForOf(tsProgram)],
150+
before: [optimizeForOf(tsProgram), optimizeArrayDestructuring()],
150151
after: [changeExtensionInImportPaths({ extension }), inlineInvariant],
151152
});
152153
assert(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import ts from 'typescript';
2+
3+
export function optimizeArrayDestructuring() {
4+
const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
5+
const { factory } = context;
6+
7+
return (sourceFile: ts.SourceFile): ts.SourceFile => {
8+
const visitor = (node: ts.Node): ts.Node => {
9+
if (ts.isArrayBindingPattern(node)) {
10+
const elements = node.elements
11+
.map((el, i) => {
12+
if (!el.getText()) {
13+
return undefined;
14+
}
15+
return { key: String(i), name: el.getText() };
16+
})
17+
.filter(Boolean) as Array<{ key: string; name: string }>;
18+
19+
const objectBindingPattern = factory.createObjectBindingPattern(
20+
elements.map((el) => {
21+
const key = factory.createIdentifier(el.key);
22+
const name = factory.createIdentifier(el.name);
23+
return factory.createBindingElement(undefined, key, name);
24+
}),
25+
);
26+
return objectBindingPattern;
27+
}
28+
29+
return ts.visitEachChild(node, visitor, context);
30+
};
31+
return ts.visitNode(sourceFile, visitor) as ts.SourceFile;
32+
};
33+
};
34+
35+
return transformer;
36+
}

resources/optimize-for-of.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export function optimizeForOf(program: ts.Program) {
126126
);
127127
}
128128

129-
const forInitiliazer = factory.createVariableDeclarationList(
129+
const forInitializer = factory.createVariableDeclarationList(
130130
forDeclarations,
131131
ts.NodeFlags.Let,
132132
);
@@ -151,7 +151,7 @@ export function optimizeForOf(program: ts.Program) {
151151
]);
152152

153153
return factory.createForStatement(
154-
forInitiliazer,
154+
forInitializer,
155155
forCondition,
156156
forIncrementor,
157157
forBody,

0 commit comments

Comments
 (0)