Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/transformation/visitors/class/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import {
transformClassInstanceFields,
transformStaticPropertyDeclaration,
} from "./members/fields";
import { createMethodDecoratingExpression, transformMethodDeclaration } from "./members/method";
import {
createConstructorDecoratingExpression,
createMethodDecoratingExpression,
transformMethodDeclaration,
} from "./members/method";
import { getExtendedNode, getExtendedType, isStaticNode } from "./utils";
import { createClassSetup } from "./setup";
import { LuaTarget } from "../../../CompilerOptions";
Expand Down Expand Up @@ -116,6 +120,9 @@ function transformClassLikeDeclaration(
);

if (constructorResult) result.push(constructorResult);

const decoratingExpression = createConstructorDecoratingExpression(context, constructor, localClassName);
if (decoratingExpression) result.push(decoratingExpression);
} else if (!extendedType) {
// Generate a constructor if none was defined in a base class
const constructorResult = transformConstructorDeclaration(
Expand Down
35 changes: 27 additions & 8 deletions src/transformation/visitors/class/members/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ export function transformMethodDeclaration(
);
}

export function createMethodDecoratingExpression(
export function getParameterDecorators(
context: TransformationContext,
node: ts.MethodDeclaration,
className: lua.Identifier
): lua.Statement | undefined {
const methodTable = transformMemberExpressionOwnerName(node, className);
const methodName = transformMethodName(context, node);

const parameterDecorators = node.parameters
node: ts.FunctionLikeDeclarationBase
): lua.CallExpression[] {
return node.parameters
.flatMap((parameter, index) =>
ts
.getDecorators(parameter)
Expand All @@ -66,7 +62,30 @@ export function createMethodDecoratingExpression(
)
)
.filter(isNonNull);
}

export function createConstructorDecoratingExpression(
context: TransformationContext,
node: ts.ConstructorDeclaration,
className: lua.Identifier
): lua.Statement | undefined {
const parameterDecorators = getParameterDecorators(context, node);

if (parameterDecorators.length > 0) {
const decorateMethod = createDecoratingExpression(context, node.kind, parameterDecorators, className);
return lua.createExpressionStatement(decorateMethod);
}
}

export function createMethodDecoratingExpression(
context: TransformationContext,
node: ts.MethodDeclaration,
className: lua.Identifier
): lua.Statement | undefined {
const methodTable = transformMemberExpressionOwnerName(node, className);
const methodName = transformMethodName(context, node);

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

if (methodDecorators.length > 0 || parameterDecorators.length > 0) {
Expand Down
1 change: 1 addition & 0 deletions test/unit/classes/decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ test.each([
["@decorator static ['evaluated property'];"],
["method(@decorator a) {}"],
["static method(@decorator a) {}"],
["constructor(@decorator a) {}"],
])("Decorate class member (%p)", classMember => {
util.testFunction`
let decoratorParameters: any;
Expand Down