Skip to content

Commit b019174

Browse files
ark120202Perryvw
authored andcommitted
Remove noHoisting option (#808)
* Remove `noHoisting` option * Remove unused declarations * Remove `noHoisting` references from `vararg` tests * Fix vararg tests * Use test builder in `@vararg global` test * Fix bundle tests
1 parent 29632f3 commit b019174

File tree

13 files changed

+119
-190
lines changed

13 files changed

+119
-190
lines changed

src/CompilerOptions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
2424
luaBundleEntry?: string;
2525
luaTarget?: LuaTarget;
2626
luaLibImport?: LuaLibImportKind;
27-
noHoisting?: boolean;
2827
sourceMapTraceback?: boolean;
2928
plugins?: Array<ts.PluginImport | TransformerImport>;
3029
[option: string]: ts.CompilerOptions[string] | Array<ts.PluginImport | TransformerImport>;

src/cli/parse.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ export const optionDeclarations: CommandLineOption[] = [
6161
description: "Specify if a header will be added to compiled files.",
6262
type: "boolean",
6363
},
64-
{
65-
name: "noHoisting",
66-
description: "Disables hoisting.",
67-
type: "boolean",
68-
},
6964
{
7065
name: "sourceMapTraceback",
7166
description: "Applies the source map to show source TS files and lines in error tracebacks.",

src/transformation/utils/errors.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,6 @@ export const UnsupportedNonDestructuringLuaIterator = (node: ts.Node) =>
130130
export const UnresolvableRequirePath = (node: ts.Node, reason: string, path?: string) =>
131131
new TranspileError(`${reason}. TypeScript path: ${path}.`, node);
132132

133-
export const ReferencedBeforeDeclaration = (node: ts.Identifier) =>
134-
new TranspileError(
135-
`Identifier "${node.text}" was referenced before it was declared. The declaration ` +
136-
"must be moved before the identifier's use, or hoisting must be enabled.",
137-
node
138-
);
139-
140133
export const UnsupportedObjectDestructuringInForOf = (node: ts.Node) =>
141134
new TranspileError(`Unsupported object destructuring in for...of statement.`, node);
142135

src/transformation/utils/lua-ast.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function createHoistableVariableDeclarationStatement(
9595
tsOriginal?: ts.Node
9696
): lua.AssignmentStatement | lua.VariableDeclarationStatement {
9797
const declaration = lua.createVariableDeclarationStatement(identifier, initializer, tsOriginal);
98-
if (!context.options.noHoisting && identifier.symbolId) {
98+
if (identifier.symbolId !== undefined) {
9999
const scope = peekScope(context);
100100
assert(scope.type !== ScopeType.Switch);
101101

@@ -158,17 +158,15 @@ export function createLocalOrExportedOrGlobalDeclaration(
158158
declaration = lua.createVariableDeclarationStatement(lhs, rhs, tsOriginal);
159159
}
160160

161-
if (!context.options.noHoisting) {
162-
// Remember local variable declarations for hoisting later
163-
if (!scope.variableDeclarations) {
164-
scope.variableDeclarations = [];
165-
}
161+
// Remember local variable declarations for hoisting later
162+
if (!scope.variableDeclarations) {
163+
scope.variableDeclarations = [];
164+
}
166165

167-
scope.variableDeclarations.push(declaration);
166+
scope.variableDeclarations.push(declaration);
168167

169-
if (scope.type === ScopeType.Switch) {
170-
declaration = undefined;
171-
}
168+
if (scope.type === ScopeType.Switch) {
169+
declaration = undefined;
172170
}
173171
} else if (rhs) {
174172
// global
@@ -178,7 +176,7 @@ export function createLocalOrExportedOrGlobalDeclaration(
178176
}
179177
}
180178

181-
if (!context.options.noHoisting && isFunctionDeclaration) {
179+
if (isFunctionDeclaration) {
182180
// Remember function definitions for hoisting later
183181
const functionSymbolId = (lhs as lua.Identifier).symbolId;
184182
const scope = peekScope(context);

src/transformation/utils/scope.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ export function popScope(context: TransformationContext): Scope {
9898
}
9999

100100
export function performHoisting(context: TransformationContext, statements: lua.Statement[]): lua.Statement[] {
101-
if (context.options.noHoisting) {
102-
return statements;
103-
}
104-
105101
const scope = peekScope(context);
106102
let result = statements;
107103
result = hoistFunctionDefinitions(context, scope, result);

src/transformation/utils/symbols.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
33
import { getOrUpdate } from "../../utils";
44
import { TransformationContext } from "../context";
5-
import { ReferencedBeforeDeclaration } from "./errors";
65
import { markSymbolAsReferencedInCurrentScopes } from "./scope";
7-
import { getFirstDeclarationInFile } from "./typescript";
86

97
const symbolIdCounters = new WeakMap<TransformationContext, number>();
108
function nextSymbolId(context: TransformationContext): lua.SymbolId {
@@ -46,14 +44,6 @@ export function trackSymbolReference(
4644
symbolInfo.set(symbolId, { symbol, firstSeenAtPos: identifier.pos });
4745
}
4846

49-
if (context.options.noHoisting) {
50-
// Check for reference-before-declaration
51-
const declaration = getFirstDeclarationInFile(symbol, context.sourceFile);
52-
if (declaration && identifier.pos < declaration.pos) {
53-
throw ReferencedBeforeDeclaration(identifier);
54-
}
55-
}
56-
5747
markSymbolAsReferencedInCurrentScopes(context, symbolId, identifier);
5848

5949
return symbolId;

src/transformation/visitors/function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ export const transformFunctionDeclaration: FunctionVisitor<ts.FunctionDeclaratio
267267
}
268268

269269
// Remember symbols referenced in this function for hoisting later
270-
if (!context.options.noHoisting && name.symbolId !== undefined) {
270+
if (name.symbolId !== undefined) {
271271
const scope = peekScope(context);
272272
if (!scope.functionDefinitions) {
273273
scope.functionDefinitions = new Map();

src/transformation/visitors/modules/import.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function transformImportSpecifier(
9696
export const transformImportDeclaration: FunctionVisitor<ts.ImportDeclaration> = (statement, context) => {
9797
const scope = peekScope(context);
9898

99-
if (!context.options.noHoisting && !scope.importStatements) {
99+
if (!scope.importStatements) {
100100
scope.importStatements = [];
101101
}
102102

test/cli/parse.spec.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ describe("command line", () => {
8282
});
8383

8484
test("shouldn't parse following arguments as values", () => {
85-
const result = tstl.parseCommandLine(["--noHeader", "--noHoisting"]);
85+
const result = tstl.parseCommandLine(["--noHeader", "--noImplicitSelf"]);
8686

8787
expect(result.errors).not.toHaveDiagnostics();
8888
expect(result.options.noHeader).toBe(true);
89-
expect(result.options.noHoisting).toBe(true);
89+
expect(result.options.noImplicitSelf).toBe(true);
9090
});
9191

9292
test("shouldn't parse following files as values", () => {
@@ -101,8 +101,6 @@ describe("command line", () => {
101101
test.each<[string, string, tstl.CompilerOptions]>([
102102
["noHeader", "false", { noHeader: false }],
103103
["noHeader", "true", { noHeader: true }],
104-
["noHoisting", "false", { noHoisting: false }],
105-
["noHoisting", "true", { noHoisting: true }],
106104
["sourceMapTraceback", "false", { sourceMapTraceback: false }],
107105
["sourceMapTraceback", "true", { sourceMapTraceback: true }],
108106

@@ -209,8 +207,6 @@ describe("tsconfig", () => {
209207
test.each<[string, any, tstl.CompilerOptions]>([
210208
["noHeader", false, { noHeader: false }],
211209
["noHeader", true, { noHeader: true }],
212-
["noHoisting", false, { noHoisting: false }],
213-
["noHoisting", true, { noHoisting: true }],
214210
["sourceMapTraceback", false, { sourceMapTraceback: false }],
215211
["sourceMapTraceback", true, { sourceMapTraceback: true }],
216212

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import * as util from "../../util";
22

3-
test.each([{}, { noHoisting: true }])("@vararg", compilerOptions => {
4-
const code = `
5-
/** @vararg */ type LuaVarArg<A extends unknown[]> = A & { __luaVarArg?: never };
3+
const varargDeclaration = `
4+
/** @vararg */
5+
type LuaVarArg<A extends unknown[]> = A & { __luaVararg?: never };
6+
`;
7+
8+
test("@vararg", () => {
9+
util.testFunction`
10+
${varargDeclaration}
611
function foo(a: unknown, ...b: LuaVarArg<unknown[]>) {
712
const c = [...b];
813
return c.join("");
@@ -11,45 +16,30 @@ test.each([{}, { noHoisting: true }])("@vararg", compilerOptions => {
1116
return foo(a, ...b);
1217
}
1318
return bar("A", "B", "C", "D");
14-
`;
15-
16-
const lua = util.transpileString(code, compilerOptions);
17-
expect(lua).not.toMatch("b = ({...})");
18-
expect(lua).not.toMatch("unpack");
19-
expect(util.transpileAndExecute(code, compilerOptions)).toBe("BCD");
19+
`
20+
.tap(builder => expect(builder.getMainLuaCodeChunk()).not.toMatch("b = "))
21+
.tap(builder => expect(builder.getMainLuaCodeChunk()).not.toMatch("unpack"))
22+
.expectToMatchJsResult();
2023
});
2124

22-
test.each([{}, { noHoisting: true }])("@vararg array access", compilerOptions => {
23-
const code = `
24-
/** @vararg */ type LuaVarArg<A extends unknown[]> = A & { __luaVarArg?: never };
25+
test("@vararg array access", () => {
26+
util.testFunction`
27+
${varargDeclaration}
2528
function foo(a: unknown, ...b: LuaVarArg<unknown[]>) {
2629
const c = [...b];
2730
return c.join("") + b[0];
2831
}
2932
return foo("A", "B", "C", "D");
30-
`;
31-
32-
expect(util.transpileAndExecute(code, compilerOptions)).toBe("BCDB");
33+
`.expectToMatchJsResult();
3334
});
3435

35-
test.each([{}, { noHoisting: true }])("@vararg global", compilerOptions => {
36-
const code = `
37-
/** @vararg */ type LuaVarArg<A extends unknown[]> = A & { __luaVarArg?: never };
36+
test("@vararg global", () => {
37+
util.testModule`
38+
${varargDeclaration}
3839
declare const arg: LuaVarArg<string[]>;
39-
const arr = [...arg];
40-
const result = arr.join("");
41-
`;
42-
43-
const luaBody = util.transpileString(code, compilerOptions, false);
44-
expect(luaBody).not.toMatch("unpack");
45-
46-
const lua = `
47-
function test(...)
48-
${luaBody}
49-
return result
50-
end
51-
return test("A", "B", "C", "D")
52-
`;
53-
54-
expect(util.executeLua(lua)).toBe("ABCD");
40+
export const result = [...arg].join("");
41+
`
42+
.setLuaFactory(code => `return (function(...) ${code} end)("A", "B", "C", "D")`)
43+
.tap(builder => expect(builder.getMainLuaCodeChunk()).not.toMatch("unpack"))
44+
.expectToEqual({ result: "ABCD" });
5545
});

0 commit comments

Comments
 (0)