Skip to content

Commit a6230a7

Browse files
authored
Fix constructor scope breaking vararg spread optimization (#1675)
* Fix constructor scope breaking vararg spread optimization * remove test debug * fix typo in test name
1 parent bd00c15 commit a6230a7

File tree

15 files changed

+41
-40
lines changed

15 files changed

+41
-40
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/transformation/context/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ export class TransformationContext {
247247
public readonly scopeStack: Scope[] = [];
248248
private lastScopeId = 0;
249249

250-
public pushScope(type: ScopeType): Scope {
251-
const scope = { type, id: ++this.lastScopeId };
250+
public pushScope(type: ScopeType, node: ts.Node): Scope {
251+
const scope: Scope = { type, id: ++this.lastScopeId, node };
252252
this.scopeStack.push(scope);
253253
return scope;
254254
}

src/transformation/utils/scope.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export enum LoopContinued {
3131
export interface Scope {
3232
type: ScopeType;
3333
id: number;
34-
node?: ts.Node;
34+
node: ts.Node;
3535
referencedSymbols?: Map<lua.SymbolId, ts.Node[]>;
3636
variableDeclarations?: lua.VariableDeclarationStatement[];
3737
functionDefinitions?: Map<lua.SymbolId, FunctionDefinitionInfo>;
@@ -99,7 +99,7 @@ function isHoistableFunctionDeclaredInScope(symbol: ts.Symbol, scopeNode: ts.Nod
9999
// Checks for references to local functions which haven't been defined yet,
100100
// and thus will be hoisted above the current position.
101101
export function hasReferencedUndefinedLocalFunction(context: TransformationContext, scope: Scope) {
102-
if (!scope.referencedSymbols || !scope.node) {
102+
if (!scope.referencedSymbols) {
103103
return false;
104104
}
105105
for (const [symbolId, nodes] of scope.referencedSymbols) {
@@ -127,10 +127,6 @@ export function hasReferencedSymbol(context: TransformationContext, scope: Scope
127127
return false;
128128
}
129129

130-
export function isFunctionScopeWithDefinition(scope: Scope): scope is Scope & { node: ts.SignatureDeclaration } {
131-
return scope.node !== undefined && ts.isFunctionLike(scope.node);
132-
}
133-
134130
export function separateHoistedStatements(context: TransformationContext, statements: lua.Statement[]): HoistingResult {
135131
const scope = peekScope(context);
136132
const allHoistedStatments: lua.Statement[] = [];

src/transformation/visitors/block.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ export function transformScopeBlock(
1212
node: ts.Block,
1313
scopeType: ScopeType
1414
): [lua.Block, Scope] {
15-
context.pushScope(scopeType);
15+
context.pushScope(scopeType, node);
1616
const statements = performHoisting(context, context.transformStatements(node.statements));
1717
const scope = context.popScope();
1818
return [lua.createBlock(statements, node), scope];
1919
}
2020

2121
export const transformBlock: FunctionVisitor<ts.Block> = (node, context) => {
22-
context.pushScope(ScopeType.Block);
22+
context.pushScope(ScopeType.Block, node);
2323
const statements = performHoisting(context, context.transformStatements(node.statements));
2424
context.popScope();
2525
return lua.createDoStatement(statements, node);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function transformAccessor(
1515
className: lua.Identifier
1616
): lua.Expression {
1717
const [params, dot, restParam] = transformParameters(context, node.parameters, createSelfIdentifier());
18-
const body = node.body ? transformFunctionBody(context, node.parameters, node.body, restParam)[0] : [];
18+
const body = node.body ? transformFunctionBody(context, node.parameters, node.body, node, restParam)[0] : [];
1919
const accessorFunction = lua.createFunctionExpression(
2020
lua.createBlock(body),
2121
params,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function transformConstructorDeclaration(
2828
}
2929

3030
// Transform body
31-
const scope = context.pushScope(ScopeType.Function);
31+
const scope = context.pushScope(ScopeType.Function, statement);
3232
const body = transformFunctionBodyContent(context, statement.body);
3333

3434
const [params, dotsLiteral, restParamName] = transformParameters(

src/transformation/visitors/conditional.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const transformConditionalExpression: FunctionVisitor<ts.ConditionalExpre
7676
};
7777

7878
export function transformIfStatement(statement: ts.IfStatement, context: TransformationContext): lua.IfStatement {
79-
context.pushScope(ScopeType.Conditional);
79+
context.pushScope(ScopeType.Conditional, statement);
8080

8181
// Check if we need to add diagnostic about Lua truthiness
8282
checkOnlyTruthyCondition(statement.expression, context);
@@ -107,7 +107,7 @@ export function transformIfStatement(statement: ts.IfStatement, context: Transfo
107107
return lua.createIfStatement(condition, ifBlock, elseStatement);
108108
}
109109
} else {
110-
context.pushScope(ScopeType.Conditional);
110+
context.pushScope(ScopeType.Conditional, statement);
111111
const elseStatements = performHoisting(
112112
context,
113113
transformBlockOrStatement(context, statement.elseStatement)

src/transformation/visitors/function.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,10 @@ export function transformFunctionBody(
164164
context: TransformationContext,
165165
parameters: ts.NodeArray<ts.ParameterDeclaration>,
166166
body: ts.ConciseBody,
167-
spreadIdentifier?: lua.Identifier,
168-
node?: ts.FunctionLikeDeclaration
167+
node: ts.FunctionLikeDeclaration,
168+
spreadIdentifier?: lua.Identifier
169169
): [lua.Statement[], Scope] {
170-
const scope = context.pushScope(ScopeType.Function);
171-
scope.node = node;
170+
const scope = context.pushScope(ScopeType.Function, node);
172171
let bodyStatements = transformFunctionBodyContent(context, body);
173172
if (node && isAsyncFunction(node)) {
174173
bodyStatements = [lua.createReturnStatement([wrapInAsyncAwaiter(context, bodyStatements)])];
@@ -258,8 +257,8 @@ export function transformFunctionToExpression(
258257
context,
259258
node.parameters,
260259
node.body,
261-
spreadIdentifier,
262-
node
260+
node,
261+
spreadIdentifier
263262
);
264263

265264
const functionExpression = lua.createFunctionExpression(

src/transformation/visitors/loops/for.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ScopeType } from "../../utils/scope";
99
export const transformForStatement: FunctionVisitor<ts.ForStatement> = (statement, context) => {
1010
const result: lua.Statement[] = [];
1111

12-
context.pushScope(ScopeType.Loop);
12+
context.pushScope(ScopeType.Loop, statement);
1313

1414
if (statement.initializer) {
1515
if (ts.isVariableDeclarationList(statement.initializer)) {

src/transformation/visitors/loops/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function transformLoopBody(
1414
context: TransformationContext,
1515
loop: ts.WhileStatement | ts.DoStatement | ts.ForStatement | ts.ForOfStatement | ts.ForInOrOfStatement
1616
): lua.Statement[] {
17-
context.pushScope(ScopeType.Loop);
17+
context.pushScope(ScopeType.Loop, loop);
1818
const body = performHoisting(context, transformBlockOrStatement(context, loop.statement));
1919
const scope = context.popScope();
2020
const scopeId = scope.id;
@@ -79,7 +79,7 @@ export function transformForInitializer(
7979
): lua.Identifier {
8080
const valueVariable = lua.createIdentifier("____value");
8181

82-
context.pushScope(ScopeType.LoopInitializer);
82+
context.pushScope(ScopeType.LoopInitializer, initializer);
8383

8484
if (ts.isVariableDeclarationList(initializer)) {
8585
// Declaration of new variable

0 commit comments

Comments
 (0)