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
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/transformation/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ export class TransformationContext {
public readonly scopeStack: Scope[] = [];
private lastScopeId = 0;

public pushScope(type: ScopeType): Scope {
const scope = { type, id: ++this.lastScopeId };
public pushScope(type: ScopeType, node: ts.Node): Scope {
const scope: Scope = { type, id: ++this.lastScopeId, node };
this.scopeStack.push(scope);
return scope;
}
Expand Down
8 changes: 2 additions & 6 deletions src/transformation/utils/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export enum LoopContinued {
export interface Scope {
type: ScopeType;
id: number;
node?: ts.Node;
node: ts.Node;
referencedSymbols?: Map<lua.SymbolId, ts.Node[]>;
variableDeclarations?: lua.VariableDeclarationStatement[];
functionDefinitions?: Map<lua.SymbolId, FunctionDefinitionInfo>;
Expand Down Expand Up @@ -99,7 +99,7 @@ function isHoistableFunctionDeclaredInScope(symbol: ts.Symbol, scopeNode: ts.Nod
// Checks for references to local functions which haven't been defined yet,
// and thus will be hoisted above the current position.
export function hasReferencedUndefinedLocalFunction(context: TransformationContext, scope: Scope) {
if (!scope.referencedSymbols || !scope.node) {
if (!scope.referencedSymbols) {
return false;
}
for (const [symbolId, nodes] of scope.referencedSymbols) {
Expand Down Expand Up @@ -127,10 +127,6 @@ export function hasReferencedSymbol(context: TransformationContext, scope: Scope
return false;
}

export function isFunctionScopeWithDefinition(scope: Scope): scope is Scope & { node: ts.SignatureDeclaration } {
return scope.node !== undefined && ts.isFunctionLike(scope.node);
}

export function separateHoistedStatements(context: TransformationContext, statements: lua.Statement[]): HoistingResult {
const scope = peekScope(context);
const allHoistedStatments: lua.Statement[] = [];
Expand Down
4 changes: 2 additions & 2 deletions src/transformation/visitors/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export function transformScopeBlock(
node: ts.Block,
scopeType: ScopeType
): [lua.Block, Scope] {
context.pushScope(scopeType);
context.pushScope(scopeType, node);
const statements = performHoisting(context, context.transformStatements(node.statements));
const scope = context.popScope();
return [lua.createBlock(statements, node), scope];
}

export const transformBlock: FunctionVisitor<ts.Block> = (node, context) => {
context.pushScope(ScopeType.Block);
context.pushScope(ScopeType.Block, node);
const statements = performHoisting(context, context.transformStatements(node.statements));
context.popScope();
return lua.createDoStatement(statements, node);
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/class/members/accessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function transformAccessor(
className: lua.Identifier
): lua.Expression {
const [params, dot, restParam] = transformParameters(context, node.parameters, createSelfIdentifier());
const body = node.body ? transformFunctionBody(context, node.parameters, node.body, restParam)[0] : [];
const body = node.body ? transformFunctionBody(context, node.parameters, node.body, node, restParam)[0] : [];
const accessorFunction = lua.createFunctionExpression(
lua.createBlock(body),
params,
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/class/members/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function transformConstructorDeclaration(
}

// Transform body
const scope = context.pushScope(ScopeType.Function);
const scope = context.pushScope(ScopeType.Function, statement);
const body = transformFunctionBodyContent(context, statement.body);

const [params, dotsLiteral, restParamName] = transformParameters(
Expand Down
4 changes: 2 additions & 2 deletions src/transformation/visitors/conditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const transformConditionalExpression: FunctionVisitor<ts.ConditionalExpre
};

export function transformIfStatement(statement: ts.IfStatement, context: TransformationContext): lua.IfStatement {
context.pushScope(ScopeType.Conditional);
context.pushScope(ScopeType.Conditional, statement);

// Check if we need to add diagnostic about Lua truthiness
checkOnlyTruthyCondition(statement.expression, context);
Expand Down Expand Up @@ -107,7 +107,7 @@ export function transformIfStatement(statement: ts.IfStatement, context: Transfo
return lua.createIfStatement(condition, ifBlock, elseStatement);
}
} else {
context.pushScope(ScopeType.Conditional);
context.pushScope(ScopeType.Conditional, statement);
const elseStatements = performHoisting(
context,
transformBlockOrStatement(context, statement.elseStatement)
Expand Down
11 changes: 5 additions & 6 deletions src/transformation/visitors/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ export function transformFunctionBody(
context: TransformationContext,
parameters: ts.NodeArray<ts.ParameterDeclaration>,
body: ts.ConciseBody,
spreadIdentifier?: lua.Identifier,
node?: ts.FunctionLikeDeclaration
node: ts.FunctionLikeDeclaration,
spreadIdentifier?: lua.Identifier
): [lua.Statement[], Scope] {
const scope = context.pushScope(ScopeType.Function);
scope.node = node;
const scope = context.pushScope(ScopeType.Function, node);
let bodyStatements = transformFunctionBodyContent(context, body);
if (node && isAsyncFunction(node)) {
bodyStatements = [lua.createReturnStatement([wrapInAsyncAwaiter(context, bodyStatements)])];
Expand Down Expand Up @@ -258,8 +257,8 @@ export function transformFunctionToExpression(
context,
node.parameters,
node.body,
spreadIdentifier,
node
node,
spreadIdentifier
);

const functionExpression = lua.createFunctionExpression(
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/loops/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ScopeType } from "../../utils/scope";
export const transformForStatement: FunctionVisitor<ts.ForStatement> = (statement, context) => {
const result: lua.Statement[] = [];

context.pushScope(ScopeType.Loop);
context.pushScope(ScopeType.Loop, statement);

if (statement.initializer) {
if (ts.isVariableDeclarationList(statement.initializer)) {
Expand Down
4 changes: 2 additions & 2 deletions src/transformation/visitors/loops/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function transformLoopBody(
context: TransformationContext,
loop: ts.WhileStatement | ts.DoStatement | ts.ForStatement | ts.ForOfStatement | ts.ForInOrOfStatement
): lua.Statement[] {
context.pushScope(ScopeType.Loop);
context.pushScope(ScopeType.Loop, loop);
const body = performHoisting(context, transformBlockOrStatement(context, loop.statement));
const scope = context.popScope();
const scopeId = scope.id;
Expand Down Expand Up @@ -79,7 +79,7 @@ export function transformForInitializer(
): lua.Identifier {
const valueVariable = lua.createIdentifier("____value");

context.pushScope(ScopeType.LoopInitializer);
context.pushScope(ScopeType.LoopInitializer, initializer);

if (ts.isVariableDeclarationList(initializer)) {
// Declaration of new variable
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const transformModuleDeclaration: FunctionVisitor<ts.ModuleDeclaration> =

// Transform moduleblock to block and visit it
if (moduleHasEmittedBody(node)) {
context.pushScope(ScopeType.Block);
context.pushScope(ScopeType.Block, node);
const statements = performHoisting(
context,
context.transformStatements(ts.isModuleBlock(node.body) ? node.body.statements : node.body)
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/sourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const transformSourceFileNode: FunctionVisitor<ts.SourceFile> = (node, co
statements.push(lua.createExpressionStatement(errorCall));
}
} else {
context.pushScope(ScopeType.File);
context.pushScope(ScopeType.File, node);

statements = performHoisting(context, context.transformStatements(node.statements));
context.popScope();
Expand Down
10 changes: 2 additions & 8 deletions src/transformation/visitors/spread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import { FunctionVisitor, TransformationContext } from "../context";
import { getIterableExtensionKindForNode, IterableExtensionKind } from "../utils/language-extensions";
import { createUnpackCall } from "../utils/lua-ast";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import {
findScope,
hasReferencedSymbol,
hasReferencedUndefinedLocalFunction,
isFunctionScopeWithDefinition,
ScopeType,
} from "../utils/scope";
import { findScope, hasReferencedSymbol, hasReferencedUndefinedLocalFunction, ScopeType } from "../utils/scope";
import { findFirstNonOuterParent, isAlwaysArrayType } from "../utils/typescript";
import { isMultiReturnCall } from "./language-extensions/multi";
import { isGlobalVarargConstant } from "./language-extensions/vararg";
Expand All @@ -34,7 +28,7 @@ export function isOptimizedVarArgSpread(context: TransformationContext, symbol:
}

// Scope must be a function scope associated with a real ts function
if (!isFunctionScopeWithDefinition(scope)) {
if (!ts.isFunctionLike(scope.node)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const coalesceCondition = (
};

export const transformSwitchStatement: FunctionVisitor<ts.SwitchStatement> = (statement, context) => {
const scope = context.pushScope(ScopeType.Switch);
const scope = context.pushScope(ScopeType.Switch, statement);

// Give the switch and condition accumulator a unique name to prevent nested switches from acting up.
const switchName = `____switch${scope.id}`;
Expand Down
12 changes: 12 additions & 0 deletions test/unit/classes/classes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,3 +909,15 @@ test("get inherted __index member from super (DotA 2 inheritance) (#1537)", () =
)
.expectToEqual("foo");
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1673
test("vararg spread optimization in class constructor (#1673)", () => {
const lua = util.testModule`
class C {
constructor(...args: any[]) {
console.log(...args);
}
}`.getMainLuaCodeChunk();

expect(lua).not.toContain("table.unpack");
});
Loading