Skip to content

Commit 0ae4d05

Browse files
committed
Introduce NodeFlags
1 parent 5004ee0 commit 0ae4d05

File tree

6 files changed

+20
-23
lines changed

6 files changed

+20
-23
lines changed

src/LuaAST.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,32 @@ export type Operator = UnaryOperator | BinaryOperator;
125125

126126
export type SymbolId = number & { _symbolIdBrand: any };
127127

128+
export enum NodeFlags {
129+
None = 0,
130+
Inline = 1 << 0, // Keep function body on same line
131+
Declaration = 1 << 1, // Prefer declaration syntax `function foo()` over assignment syntax `foo = function()`
132+
}
133+
128134
export interface TextRange {
129135
line?: number;
130136
column?: number;
131137
}
132138

133139
export interface Node extends TextRange {
134140
kind: SyntaxKind;
141+
flags: NodeFlags;
135142
}
136143

137144
export function createNode(kind: SyntaxKind, tsOriginal?: ts.Node): Node {
138145
if (tsOriginal === undefined) {
139-
return { kind };
146+
return { kind, flags: NodeFlags.None };
140147
}
141148

142149
const sourcePosition = getSourcePosition(tsOriginal);
143150
if (sourcePosition) {
144-
return { kind, line: sourcePosition.line, column: sourcePosition.column };
151+
return { kind, line: sourcePosition.line, column: sourcePosition.column, flags: NodeFlags.None };
145152
} else {
146-
return { kind };
153+
return { kind, flags: NodeFlags.None };
147154
}
148155
}
149156

@@ -578,18 +585,11 @@ export function isLiteral(
578585
);
579586
}
580587

581-
export enum FunctionExpressionFlags {
582-
None = 1 << 0,
583-
Inline = 1 << 1, // Keep function body on same line
584-
Declaration = 1 << 2, // Prefer declaration syntax `function foo()` over assignment syntax `foo = function()`
585-
}
586-
587588
export interface FunctionExpression extends Expression {
588589
kind: SyntaxKind.FunctionExpression;
589590
params?: Identifier[];
590591
dots?: DotsLiteral;
591592
body: Block;
592-
flags: FunctionExpressionFlags;
593593
}
594594

595595
export function isFunctionExpression(node: Node): node is FunctionExpression {
@@ -600,7 +600,7 @@ export function createFunctionExpression(
600600
body: Block,
601601
params?: Identifier[],
602602
dots?: DotsLiteral,
603-
flags = FunctionExpressionFlags.None,
603+
flags = NodeFlags.None,
604604
tsOriginal?: ts.Node
605605
): FunctionExpression {
606606
const expression = createNode(SyntaxKind.FunctionExpression, tsOriginal) as FunctionExpression;
@@ -819,6 +819,6 @@ export function isInlineFunctionExpression(expression: FunctionExpression): expr
819819
expression.body.statements?.length === 1 &&
820820
isReturnStatement(expression.body.statements[0]) &&
821821
expression.body.statements[0].expressions !== undefined &&
822-
(expression.flags & FunctionExpressionFlags.Inline) !== 0
822+
(expression.flags & NodeFlags.Inline) !== 0
823823
);
824824
}

src/LuaPrinter.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,7 @@ export class LuaPrinter {
433433

434434
chunks.push(this.indent());
435435

436-
if (
437-
lua.isFunctionDefinition(statement) &&
438-
(statement.right[0].flags & lua.FunctionExpressionFlags.Declaration) !== 0
439-
) {
436+
if (lua.isFunctionDefinition(statement) && (statement.right[0].flags & lua.NodeFlags.Declaration) !== 0) {
440437
// Use `function foo()` instead of `foo = function()`
441438
const name = this.printExpression(statement.left[0]);
442439
if (isValidLuaFunctionDeclarationName(name.toString(), this.options)) {

src/transformation/visitors/class/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function transformClassLikeDeclaration(
155155
lua.createBlock(constructorBody),
156156
[createSelfIdentifier()],
157157
lua.createDotsLiteral(),
158-
lua.FunctionExpressionFlags.Declaration
158+
lua.NodeFlags.Declaration
159159
);
160160
result.push(
161161
lua.createAssignmentStatement(createConstructorName(localClassName), constructorFunction, classDeclaration)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { createPrototypeName } from "./constructor";
1111
function transformAccessor(context: TransformationContext, node: ts.AccessorDeclaration): lua.FunctionExpression {
1212
const [params, dot, restParam] = transformParameters(context, node.parameters, createSelfIdentifier());
1313
const body = node.body ? transformFunctionBody(context, node.parameters, node.body, restParam)[0] : [];
14-
return lua.createFunctionExpression(lua.createBlock(body), params, dot, lua.FunctionExpressionFlags.Declaration);
14+
return lua.createFunctionExpression(lua.createBlock(body), params, dot, lua.NodeFlags.Declaration);
1515
}
1616

1717
export function transformAccessorDeclarations(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function transformConstructorDeclaration(
9090

9191
return lua.createAssignmentStatement(
9292
createConstructorName(className),
93-
lua.createFunctionExpression(block, params, dotsLiteral, lua.FunctionExpressionFlags.Declaration),
93+
lua.createFunctionExpression(block, params, dotsLiteral, lua.NodeFlags.Declaration),
9494
constructorWasGenerated ? classDeclaration : statement
9595
);
9696
}

src/transformation/visitors/function.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function createCallableTable(functionExpression: lua.Expression): lua.Exp
6666
]),
6767
[lua.createAnonymousIdentifier()],
6868
lua.createDotsLiteral(),
69-
lua.FunctionExpressionFlags.Inline
69+
lua.NodeFlags.Inline
7070
);
7171
}
7272
return lua.createCallExpression(lua.createIdentifier("setmetatable"), [
@@ -231,10 +231,10 @@ export function transformFunctionToExpression(
231231
}
232232
}
233233

234-
let flags = lua.FunctionExpressionFlags.None;
235-
if (!ts.isBlock(node.body)) flags |= lua.FunctionExpressionFlags.Inline;
234+
let flags = lua.NodeFlags.None;
235+
if (!ts.isBlock(node.body)) flags |= lua.NodeFlags.Inline;
236236
if (ts.isFunctionDeclaration(node) || ts.isMethodDeclaration(node)) {
237-
flags |= lua.FunctionExpressionFlags.Declaration;
237+
flags |= lua.NodeFlags.Declaration;
238238
}
239239

240240
const [paramNames, dotsLiteral, spreadIdentifier] = transformParameters(context, node.parameters, functionContext);

0 commit comments

Comments
 (0)