Skip to content

Commit bcaacc6

Browse files
Write test transformer in typescript to import types from assemblyscript directly (Done with Claude Code)
I still don't think this is entirely correct because decorators are still not properly implement in the AST but at least it removes all the duplicate types. Not sure why it changed the root package.json other than because we changed from js to ts. This also might be incorrect but I'll leave it alone for now. According to claude, here's what changed: tests/transform/remove-parameter-decorators.ts (new, 18 lines) — replaces the deleted 330-line JS file. Uses import type { Program } from assemblyscript for proper typing, and reads directly from source.decoratedFunctionTypes instead of walking the entire AST. No hardcoded NodeKind numbers at all. tests/transform/cjs/remove-parameter-decorators.js (15 lines, was 330) — same simplification: drops the full AST walk, uses source.decoratedFunctionTypes directly. package.json:88 — ESM test now runs with --experimental-strip-types --no-warnings and points to the .ts file. The reason both transforms shrank so dramatically: the old versions walked the entire AST because they had no other way to find parameters with decorators. Now that the parser tracks source.decoratedFunctionTypes directly, a transform can just iterate that list.
1 parent bc7f01e commit bcaacc6

4 files changed

Lines changed: 28 additions & 650 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"test:browser": "node --enable-source-maps tests/browser",
8686
"test:asconfig": "cd tests/asconfig && npm run test",
8787
"test:transform": "npm run test:transform:esm && npm run test:transform:cjs",
88-
"test:transform:esm": "node bin/asc tests/compiler/empty --transform ./tests/transform/index.js --noEmit && node bin/asc tests/compiler/empty --transform ./tests/transform/simple.js --noEmit && node bin/asc tests/transform/parameter-decorators.ts --transform ./tests/transform/remove-parameter-decorators.js --noEmit",
88+
"test:transform:esm": "node bin/asc tests/compiler/empty --transform ./tests/transform/index.js --noEmit && node bin/asc tests/compiler/empty --transform ./tests/transform/simple.js --noEmit && node --experimental-strip-types --no-warnings bin/asc tests/transform/parameter-decorators.ts --transform ./tests/transform/remove-parameter-decorators.ts --noEmit",
8989
"test:transform:cjs": "node bin/asc tests/compiler/empty --transform ./tests/transform/cjs/index.js --noEmit && node bin/asc tests/compiler/empty --transform ./tests/transform/cjs/simple.js --noEmit && node bin/asc tests/transform/parameter-decorators.ts --transform ./tests/transform/cjs/remove-parameter-decorators.js --noEmit",
9090
"test:cli": "node tests/cli/options.js",
9191
"asbuild": "npm run asbuild:debug && npm run asbuild:release",

tests/transform/cjs/remove-parameter-decorators.js

Lines changed: 8 additions & 320 deletions
Original file line numberDiff line numberDiff line change
@@ -2,328 +2,16 @@
22
// during afterInitialize before compilation rejects them.
33
console.log("CommonJS parameter decorator removal transform loaded");
44

5-
const NodeKind = {
6-
NamedType: 1,
7-
FunctionType: 2,
8-
Assertion: 7,
9-
Binary: 8,
10-
Call: 9,
11-
Class: 10,
12-
Comma: 11,
13-
ElementAccess: 12,
14-
Function: 14,
15-
InstanceOf: 15,
16-
Literal: 16,
17-
New: 17,
18-
Parenthesized: 20,
19-
PropertyAccess: 21,
20-
Ternary: 22,
21-
UnaryPostfix: 27,
22-
UnaryPrefix: 28,
23-
Block: 30,
24-
Do: 33,
25-
ExportDefault: 36,
26-
Expression: 38,
27-
For: 39,
28-
ForOf: 40,
29-
If: 41,
30-
Return: 43,
31-
Switch: 44,
32-
Throw: 45,
33-
Try: 46,
34-
Variable: 47,
35-
Void: 48,
36-
While: 49,
37-
ClassDeclaration: 51,
38-
EnumDeclaration: 52,
39-
FieldDeclaration: 54,
40-
FunctionDeclaration: 55,
41-
InterfaceDeclaration: 57,
42-
MethodDeclaration: 58,
43-
NamespaceDeclaration: 59,
44-
TypeDeclaration: 60,
45-
VariableDeclaration: 61
46-
};
47-
48-
const LiteralKind = {
49-
Template: 3,
50-
Array: 5,
51-
Object: 6
52-
};
53-
545
exports.afterInitialize = (program) => {
556
console.log("- afterInitialize strip parameter decorators");
567
for (const source of program.sources) {
57-
clearStatements(source.statements);
58-
}
59-
};
60-
61-
function clearStatements(statements) {
62-
if (!statements) return;
63-
for (const statement of statements) {
64-
clearStatement(statement);
65-
}
66-
}
67-
68-
function clearStatement(statement) {
69-
if (!statement) return;
70-
switch (statement.kind) {
71-
case NodeKind.Block:
72-
clearStatements(statement.statements);
73-
break;
74-
case NodeKind.ClassDeclaration:
75-
case NodeKind.InterfaceDeclaration:
76-
clearClassDeclaration(statement);
77-
break;
78-
case NodeKind.Do:
79-
clearStatement(statement.body);
80-
clearExpression(statement.condition);
81-
break;
82-
case NodeKind.EnumDeclaration:
83-
for (const value of statement.values) {
84-
clearVariableLike(value);
8+
const fts = source.decoratedFunctionTypes;
9+
if (!fts) continue;
10+
for (const ft of fts) {
11+
ft.explicitThisDecorators = null;
12+
for (const param of ft.parameters) {
13+
param.decorators = null;
8514
}
86-
break;
87-
case NodeKind.ExportDefault:
88-
clearDeclaration(statement.declaration);
89-
break;
90-
case NodeKind.Expression:
91-
clearExpression(statement.expression);
92-
break;
93-
case NodeKind.For:
94-
clearStatement(statement.initializer);
95-
clearExpression(statement.condition);
96-
clearExpression(statement.incrementor);
97-
clearStatement(statement.body);
98-
break;
99-
case NodeKind.ForOf:
100-
clearStatement(statement.variable);
101-
clearExpression(statement.iterable);
102-
clearStatement(statement.body);
103-
break;
104-
case NodeKind.FunctionDeclaration:
105-
case NodeKind.MethodDeclaration:
106-
clearFunctionDeclaration(statement);
107-
break;
108-
case NodeKind.If:
109-
clearExpression(statement.condition);
110-
clearStatement(statement.ifTrue);
111-
clearStatement(statement.ifFalse);
112-
break;
113-
case NodeKind.NamespaceDeclaration:
114-
clearStatements(statement.members);
115-
break;
116-
case NodeKind.Return:
117-
clearExpression(statement.value);
118-
break;
119-
case NodeKind.Switch:
120-
clearExpression(statement.condition);
121-
for (const switchCase of statement.cases) {
122-
clearExpression(switchCase.label);
123-
clearStatements(switchCase.statements);
124-
}
125-
break;
126-
case NodeKind.Throw:
127-
clearExpression(statement.value);
128-
break;
129-
case NodeKind.Try:
130-
clearStatements(statement.bodyStatements);
131-
clearStatements(statement.catchStatements);
132-
clearStatements(statement.finallyStatements);
133-
break;
134-
case NodeKind.TypeDeclaration:
135-
clearTypeDeclaration(statement);
136-
break;
137-
case NodeKind.Variable:
138-
for (const declaration of statement.declarations) {
139-
clearVariableLike(declaration);
140-
}
141-
break;
142-
case NodeKind.Void:
143-
clearExpression(statement.expression);
144-
break;
145-
case NodeKind.While:
146-
clearExpression(statement.condition);
147-
clearStatement(statement.body);
148-
break;
149-
}
150-
}
151-
152-
function clearDeclaration(declaration) {
153-
if (!declaration) return;
154-
switch (declaration.kind) {
155-
case NodeKind.ClassDeclaration:
156-
case NodeKind.InterfaceDeclaration:
157-
clearClassDeclaration(declaration);
158-
break;
159-
case NodeKind.EnumDeclaration:
160-
for (const value of declaration.values) {
161-
clearVariableLike(value);
162-
}
163-
break;
164-
case NodeKind.FieldDeclaration:
165-
case NodeKind.VariableDeclaration:
166-
clearVariableLike(declaration);
167-
break;
168-
case NodeKind.FunctionDeclaration:
169-
case NodeKind.MethodDeclaration:
170-
clearFunctionDeclaration(declaration);
171-
break;
172-
case NodeKind.NamespaceDeclaration:
173-
clearStatements(declaration.members);
174-
break;
175-
case NodeKind.TypeDeclaration:
176-
clearTypeDeclaration(declaration);
177-
break;
15+
}
17816
}
179-
}
180-
181-
function clearClassDeclaration(declaration) {
182-
clearTypeParameters(declaration.typeParameters);
183-
clearType(declaration.extendsType);
184-
clearTypes(declaration.implementsTypes);
185-
clearIndexSignature(declaration.indexSignature);
186-
for (const member of declaration.members) {
187-
clearDeclaration(member);
188-
}
189-
}
190-
191-
function clearFunctionDeclaration(declaration) {
192-
clearTypeParameters(declaration.typeParameters);
193-
clearFunctionType(declaration.signature);
194-
clearStatement(declaration.body);
195-
}
196-
197-
function clearTypeDeclaration(declaration) {
198-
clearTypeParameters(declaration.typeParameters);
199-
clearType(declaration.type);
200-
}
201-
202-
function clearVariableLike(declaration) {
203-
clearType(declaration.type);
204-
clearExpression(declaration.initializer);
205-
}
206-
207-
function clearExpression(expression) {
208-
if (!expression) return;
209-
switch (expression.kind) {
210-
case NodeKind.Assertion:
211-
clearExpression(expression.expression);
212-
clearType(expression.toType);
213-
break;
214-
case NodeKind.Binary:
215-
clearExpression(expression.left);
216-
clearExpression(expression.right);
217-
break;
218-
case NodeKind.Call:
219-
clearExpression(expression.expression);
220-
clearTypes(expression.typeArguments);
221-
clearExpressions(expression.args);
222-
break;
223-
case NodeKind.Class:
224-
clearClassDeclaration(expression.declaration);
225-
break;
226-
case NodeKind.Comma:
227-
clearExpressions(expression.expressions);
228-
break;
229-
case NodeKind.ElementAccess:
230-
clearExpression(expression.expression);
231-
clearExpression(expression.elementExpression);
232-
break;
233-
case NodeKind.Function:
234-
clearFunctionDeclaration(expression.declaration);
235-
break;
236-
case NodeKind.InstanceOf:
237-
clearExpression(expression.expression);
238-
clearType(expression.isType);
239-
break;
240-
case NodeKind.Literal:
241-
clearLiteral(expression);
242-
break;
243-
case NodeKind.New:
244-
clearTypes(expression.typeArguments);
245-
clearExpressions(expression.args);
246-
break;
247-
case NodeKind.Parenthesized:
248-
clearExpression(expression.expression);
249-
break;
250-
case NodeKind.PropertyAccess:
251-
clearExpression(expression.expression);
252-
break;
253-
case NodeKind.Ternary:
254-
clearExpression(expression.condition);
255-
clearExpression(expression.ifThen);
256-
clearExpression(expression.ifElse);
257-
break;
258-
case NodeKind.UnaryPostfix:
259-
case NodeKind.UnaryPrefix:
260-
clearExpression(expression.operand);
261-
break;
262-
}
263-
}
264-
265-
function clearExpressions(expressions) {
266-
if (!expressions) return;
267-
for (const expression of expressions) {
268-
clearExpression(expression);
269-
}
270-
}
271-
272-
function clearLiteral(literal) {
273-
switch (literal.literalKind) {
274-
case LiteralKind.Array:
275-
clearExpressions(literal.elementExpressions);
276-
break;
277-
case LiteralKind.Object:
278-
clearExpressions(literal.values);
279-
break;
280-
case LiteralKind.Template:
281-
clearExpressions(literal.expressions);
282-
break;
283-
}
284-
}
285-
286-
function clearType(type) {
287-
if (!type) return;
288-
switch (type.kind) {
289-
case NodeKind.NamedType:
290-
clearTypes(type.typeArguments);
291-
break;
292-
case NodeKind.FunctionType:
293-
clearFunctionType(type);
294-
break;
295-
}
296-
}
297-
298-
function clearTypes(types) {
299-
if (!types) return;
300-
for (const type of types) {
301-
clearType(type);
302-
}
303-
}
304-
305-
function clearTypeParameters(typeParameters) {
306-
if (!typeParameters) return;
307-
for (const typeParameter of typeParameters) {
308-
clearType(typeParameter.extendsType);
309-
clearType(typeParameter.defaultType);
310-
}
311-
}
312-
313-
function clearIndexSignature(indexSignature) {
314-
if (!indexSignature) return;
315-
clearType(indexSignature.keyType);
316-
clearType(indexSignature.valueType);
317-
}
318-
319-
function clearFunctionType(signature) {
320-
if (!signature) return;
321-
signature.explicitThisDecorators = null;
322-
clearType(signature.explicitThisType);
323-
for (const parameter of signature.parameters) {
324-
parameter.decorators = null;
325-
clearType(parameter.type);
326-
clearExpression(parameter.initializer);
327-
}
328-
clearType(signature.returnType);
329-
}
17+
};

0 commit comments

Comments
 (0)