Skip to content

Commit 313e6ea

Browse files
authored
Hoisting (#369)
* initial hoisting implementation (needs tests) * fixed issues with switch and loop continue * dealing with hoisting of imports and exports * hoisting tests * fixed namespace hoisting and added more tests * rebuilt translation tests with hoisting * fixed tests * More Hoisting Adjustments - pushing scope for try/catch/finally - some refactoring and commenting - fixed more tests * reverting accidentally changed test files * renamed some things for clarity * working on possible smart-hoisting detection * addressed comments * smart-hoisting working * fixed tests * refactored hoisting detection and added command line options to control hoisting * Another full refactor - checking variables for reference-before-declaration when they are first seen to avoid scanning later - caching nested functions in scope to avoid repeat scan later - added error for attempting to hoist when hoisting is off since it's easy to detect now - added tests for hoisting errors * change ScopeType to bitfields * rewrote hoisting to defer all logic to popScope, so that there's no need for additional scanning * reworked hoisting to avoid printer hacks * removing unnecessary symbol lookups * removing accidentally added import * replaced ts.Symbol stored in identifiers with generic ids * split out stuff to separate functions * reverted bad logic which broke referencing exports inside functions
1 parent 661d5b0 commit 313e6ea

25 files changed

+726
-137
lines changed

src/CommandLineParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export const optionDeclarations: YargsOptions = {
3232
describe: "Specify if a header will be added to compiled files.",
3333
type: "boolean",
3434
},
35+
noHoisting: {
36+
default: false,
37+
describe: "Disables hoisting.",
38+
type: "boolean",
39+
},
3540
};
3641

3742
class CLIError extends Error {}

src/Compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,4 @@ export function transpileString(
119119
const result = transpiler.transpileSourceFile(program.getSourceFile("file.ts"));
120120

121121
return result.trim();
122-
}
122+
}

src/CompilerOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface CompilerOptions extends ts.CompilerOptions {
44
noHeader?: boolean;
55
luaTarget?: LuaTarget;
66
luaLibImport?: LuaLibImportKind;
7+
noHoisting?: boolean;
78
}
89

910
export enum LuaLibImportKind {

src/LuaAST.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export type BinaryOperator =
9797

9898
export type Operator = UnaryOperator | BinaryOperator;
9999

100+
export type SymbolId = number;
101+
100102
// TODO For future sourcemap support?
101103
export interface TextRange {
102104
pos: number;
@@ -791,15 +793,23 @@ export function createMethodCallExpression(
791793
export interface Identifier extends Expression {
792794
kind: SyntaxKind.Identifier;
793795
text: string;
796+
symbolId?: SymbolId;
794797
}
795798

796799
export function isIdentifier(node: Node): node is Identifier {
797800
return node.kind === SyntaxKind.Identifier;
798801
}
799802

800-
export function createIdentifier(text: string | ts.__String, tsOriginal?: ts.Node, parent?: Node): Identifier {
803+
export function createIdentifier(
804+
text: string | ts.__String,
805+
tsOriginal?: ts.Node,
806+
symbolId?: SymbolId,
807+
parent?: Node
808+
): Identifier
809+
{
801810
const expression = createNode(SyntaxKind.Identifier, tsOriginal, parent) as Identifier;
802811
expression.text = text as string;
812+
expression.symbolId = symbolId;
803813
return expression;
804814
}
805815

0 commit comments

Comments
 (0)