Skip to content

Commit 16bde88

Browse files
Add support for constructor parameter decorators (#1439)
1 parent 186cd90 commit 16bde88

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/transformation/visitors/class/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import {
1919
transformClassInstanceFields,
2020
transformStaticPropertyDeclaration,
2121
} from "./members/fields";
22-
import { createMethodDecoratingExpression, transformMethodDeclaration } from "./members/method";
22+
import {
23+
createConstructorDecoratingExpression,
24+
createMethodDecoratingExpression,
25+
transformMethodDeclaration,
26+
} from "./members/method";
2327
import { getExtendedNode, getExtendedType, isStaticNode } from "./utils";
2428
import { createClassSetup } from "./setup";
2529
import { LuaTarget } from "../../../CompilerOptions";
@@ -116,6 +120,9 @@ function transformClassLikeDeclaration(
116120
);
117121

118122
if (constructorResult) result.push(constructorResult);
123+
124+
const decoratingExpression = createConstructorDecoratingExpression(context, constructor, localClassName);
125+
if (decoratingExpression) result.push(decoratingExpression);
119126
} else if (!extendedType) {
120127
// Generate a constructor if none was defined in a base class
121128
const constructorResult = transformConstructorDeclaration(

src/transformation/visitors/class/members/method.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,11 @@ export function transformMethodDeclaration(
4343
);
4444
}
4545

46-
export function createMethodDecoratingExpression(
46+
export function getParameterDecorators(
4747
context: TransformationContext,
48-
node: ts.MethodDeclaration,
49-
className: lua.Identifier
50-
): lua.Statement | undefined {
51-
const methodTable = transformMemberExpressionOwnerName(node, className);
52-
const methodName = transformMethodName(context, node);
53-
54-
const parameterDecorators = node.parameters
48+
node: ts.FunctionLikeDeclarationBase
49+
): lua.CallExpression[] {
50+
return node.parameters
5551
.flatMap((parameter, index) =>
5652
ts
5753
.getDecorators(parameter)
@@ -66,7 +62,30 @@ export function createMethodDecoratingExpression(
6662
)
6763
)
6864
.filter(isNonNull);
65+
}
66+
67+
export function createConstructorDecoratingExpression(
68+
context: TransformationContext,
69+
node: ts.ConstructorDeclaration,
70+
className: lua.Identifier
71+
): lua.Statement | undefined {
72+
const parameterDecorators = getParameterDecorators(context, node);
73+
74+
if (parameterDecorators.length > 0) {
75+
const decorateMethod = createDecoratingExpression(context, node.kind, parameterDecorators, className);
76+
return lua.createExpressionStatement(decorateMethod);
77+
}
78+
}
79+
80+
export function createMethodDecoratingExpression(
81+
context: TransformationContext,
82+
node: ts.MethodDeclaration,
83+
className: lua.Identifier
84+
): lua.Statement | undefined {
85+
const methodTable = transformMemberExpressionOwnerName(node, className);
86+
const methodName = transformMethodName(context, node);
6987

88+
const parameterDecorators = getParameterDecorators(context, node);
7089
const methodDecorators = ts.getDecorators(node)?.map(d => transformDecoratorExpression(context, d)) ?? [];
7190

7291
if (methodDecorators.length > 0 || parameterDecorators.length > 0) {

test/unit/classes/decorators.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ test.each([
142142
["@decorator static ['evaluated property'];"],
143143
["method(@decorator a) {}"],
144144
["static method(@decorator a) {}"],
145+
["constructor(@decorator a) {}"],
145146
])("Decorate class member (%p)", classMember => {
146147
util.testFunction`
147148
let decoratorParameters: any;

0 commit comments

Comments
 (0)