From 216a8d89f2bad6b2eec1b93c98f8963285df3eec Mon Sep 17 00:00:00 2001 From: ark120202 Date: Sun, 19 Jan 2020 10:21:18 +0000 Subject: [PATCH 1/6] Remove `noHoisting` option --- src/CompilerOptions.ts | 1 - src/cli/parse.ts | 5 -- src/transformation/utils/lua-ast.ts | 20 ++++--- src/transformation/utils/scope.ts | 4 -- src/transformation/utils/symbols.ts | 10 ---- src/transformation/visitors/function.ts | 2 +- src/transformation/visitors/modules/import.ts | 2 +- test/cli/parse.spec.ts | 8 +-- test/unit/functions/functions.spec.ts | 54 ++++++++----------- test/unit/hoisting.spec.ts | 24 --------- 10 files changed, 36 insertions(+), 94 deletions(-) diff --git a/src/CompilerOptions.ts b/src/CompilerOptions.ts index be3aa09b4..0887efd81 100644 --- a/src/CompilerOptions.ts +++ b/src/CompilerOptions.ts @@ -24,7 +24,6 @@ export type CompilerOptions = OmitIndexSignature & { luaBundleEntry?: string; luaTarget?: LuaTarget; luaLibImport?: LuaLibImportKind; - noHoisting?: boolean; sourceMapTraceback?: boolean; plugins?: Array; [option: string]: ts.CompilerOptions[string] | Array; diff --git a/src/cli/parse.ts b/src/cli/parse.ts index 40f564268..9f05f2f9c 100644 --- a/src/cli/parse.ts +++ b/src/cli/parse.ts @@ -61,11 +61,6 @@ export const optionDeclarations: CommandLineOption[] = [ description: "Specify if a header will be added to compiled files.", type: "boolean", }, - { - name: "noHoisting", - description: "Disables hoisting.", - type: "boolean", - }, { name: "sourceMapTraceback", description: "Applies the source map to show source TS files and lines in error tracebacks.", diff --git a/src/transformation/utils/lua-ast.ts b/src/transformation/utils/lua-ast.ts index effc18814..b702cdd43 100644 --- a/src/transformation/utils/lua-ast.ts +++ b/src/transformation/utils/lua-ast.ts @@ -97,7 +97,7 @@ export function createHoistableVariableDeclarationStatement( tsOriginal?: ts.Node ): lua.AssignmentStatement | lua.VariableDeclarationStatement { const declaration = lua.createVariableDeclarationStatement(identifier, initializer, tsOriginal); - if (!context.options.noHoisting && identifier.symbolId) { + if (identifier.symbolId !== undefined) { const scope = peekScope(context); assert(scope.type !== ScopeType.Switch); @@ -160,17 +160,15 @@ export function createLocalOrExportedOrGlobalDeclaration( declaration = lua.createVariableDeclarationStatement(lhs, rhs, tsOriginal); } - if (!context.options.noHoisting) { - // Remember local variable declarations for hoisting later - if (!scope.variableDeclarations) { - scope.variableDeclarations = []; - } + // Remember local variable declarations for hoisting later + if (!scope.variableDeclarations) { + scope.variableDeclarations = []; + } - scope.variableDeclarations.push(declaration); + scope.variableDeclarations.push(declaration); - if (scope.type === ScopeType.Switch) { - declaration = undefined; - } + if (scope.type === ScopeType.Switch) { + declaration = undefined; } } else if (rhs) { // global @@ -180,7 +178,7 @@ export function createLocalOrExportedOrGlobalDeclaration( } } - if (!context.options.noHoisting && isFunctionDeclaration) { + if (isFunctionDeclaration) { // Remember function definitions for hoisting later const functionSymbolId = (lhs as lua.Identifier).symbolId; const scope = peekScope(context); diff --git a/src/transformation/utils/scope.ts b/src/transformation/utils/scope.ts index 5f23d2984..44430f763 100644 --- a/src/transformation/utils/scope.ts +++ b/src/transformation/utils/scope.ts @@ -98,10 +98,6 @@ export function popScope(context: TransformationContext): Scope { } export function performHoisting(context: TransformationContext, statements: lua.Statement[]): lua.Statement[] { - if (context.options.noHoisting) { - return statements; - } - const scope = peekScope(context); let result = statements; result = hoistFunctionDefinitions(context, scope, result); diff --git a/src/transformation/utils/symbols.ts b/src/transformation/utils/symbols.ts index a08197c72..c99b41a25 100644 --- a/src/transformation/utils/symbols.ts +++ b/src/transformation/utils/symbols.ts @@ -2,9 +2,7 @@ import * as ts from "typescript"; import * as lua from "../../LuaAST"; import { getOrUpdate } from "../../utils"; import { TransformationContext } from "../context"; -import { ReferencedBeforeDeclaration } from "./errors"; import { markSymbolAsReferencedInCurrentScopes } from "./scope"; -import { getFirstDeclarationInFile } from "./typescript"; const symbolIdCounters = new WeakMap(); function nextSymbolId(context: TransformationContext): lua.SymbolId { @@ -46,14 +44,6 @@ export function trackSymbolReference( symbolInfo.set(symbolId, { symbol, firstSeenAtPos: identifier.pos }); } - if (context.options.noHoisting) { - // Check for reference-before-declaration - const declaration = getFirstDeclarationInFile(symbol, context.sourceFile); - if (declaration && identifier.pos < declaration.pos) { - throw ReferencedBeforeDeclaration(identifier); - } - } - markSymbolAsReferencedInCurrentScopes(context, symbolId, identifier); return symbolId; diff --git a/src/transformation/visitors/function.ts b/src/transformation/visitors/function.ts index d39caf1ba..dc7a1b0a4 100644 --- a/src/transformation/visitors/function.ts +++ b/src/transformation/visitors/function.ts @@ -267,7 +267,7 @@ export const transformFunctionDeclaration: FunctionVisitor = (statement, context) => { const scope = peekScope(context); - if (!context.options.noHoisting && !scope.importStatements) { + if (!scope.importStatements) { scope.importStatements = []; } diff --git a/test/cli/parse.spec.ts b/test/cli/parse.spec.ts index ae95751e9..3ce9b35ba 100644 --- a/test/cli/parse.spec.ts +++ b/test/cli/parse.spec.ts @@ -82,11 +82,11 @@ describe("command line", () => { }); test("shouldn't parse following arguments as values", () => { - const result = tstl.parseCommandLine(["--noHeader", "--noHoisting"]); + const result = tstl.parseCommandLine(["--noHeader", "--noImplicitSelf"]); expect(result.errors).not.toHaveDiagnostics(); expect(result.options.noHeader).toBe(true); - expect(result.options.noHoisting).toBe(true); + expect(result.options.noImplicitSelf).toBe(true); }); test("shouldn't parse following files as values", () => { @@ -101,8 +101,6 @@ describe("command line", () => { test.each<[string, string, tstl.CompilerOptions]>([ ["noHeader", "false", { noHeader: false }], ["noHeader", "true", { noHeader: true }], - ["noHoisting", "false", { noHoisting: false }], - ["noHoisting", "true", { noHoisting: true }], ["sourceMapTraceback", "false", { sourceMapTraceback: false }], ["sourceMapTraceback", "true", { sourceMapTraceback: true }], @@ -209,8 +207,6 @@ describe("tsconfig", () => { test.each<[string, any, tstl.CompilerOptions]>([ ["noHeader", false, { noHeader: false }], ["noHeader", true, { noHeader: true }], - ["noHoisting", false, { noHoisting: false }], - ["noHoisting", true, { noHoisting: true }], ["sourceMapTraceback", false, { sourceMapTraceback: false }], ["sourceMapTraceback", true, { sourceMapTraceback: true }], diff --git a/test/unit/functions/functions.spec.ts b/test/unit/functions/functions.spec.ts index 1a94507a2..aab7b151a 100644 --- a/test/unit/functions/functions.spec.ts +++ b/test/unit/functions/functions.spec.ts @@ -432,19 +432,17 @@ test("Function rest binding pattern", () => { `.expectToMatchJsResult(); }); -test.each([{}, { noHoisting: true }])("Function rest parameter", compilerOptions => { - const code = ` +test("Function rest parameter", () => { + util.testFunction` function foo(a: unknown, ...b: string[]) { return b.join(""); } return foo("A", "B", "C", "D"); - `; - - expect(util.transpileAndExecute(code, compilerOptions)).toBe("BCD"); + `.expectToMatchJsResult(); }); -test.each([{}, { noHoisting: true }])("Function nested rest parameter", compilerOptions => { - const code = ` +test("Function nested rest parameter", () => { + util.testFunction` function foo(a: unknown, ...b: string[]) { function bar() { return b.join(""); @@ -452,13 +450,11 @@ test.each([{}, { noHoisting: true }])("Function nested rest parameter", compiler return bar(); } return foo("A", "B", "C", "D"); - `; - - expect(util.transpileAndExecute(code, compilerOptions)).toBe("BCD"); + `.expectToMatchJsResult(); }); -test.each([{}, { noHoisting: true }])("Function nested rest spread", compilerOptions => { - const code = ` +test("Function nested rest spread", () => { + util.testFunction` function foo(a: unknown, ...b: string[]) { function bar() { const c = [...b]; @@ -467,40 +463,36 @@ test.each([{}, { noHoisting: true }])("Function nested rest spread", compilerOpt return bar(); } return foo("A", "B", "C", "D"); - `; - - expect(util.transpileAndExecute(code, compilerOptions)).toBe("BCD"); + `.expectToMatchJsResult(); }); -test.each([{}, { noHoisting: true }])("Function rest parameter (unreferenced)", compilerOptions => { - const code = ` +test("Function rest parameter (unreferenced)", () => { + util.testFunction` function foo(a: unknown, ...b: string[]) { return "foobar"; } return foo("A", "B", "C", "D"); - `; - - expect(util.transpileString(code, compilerOptions)).not.toMatch("b = ({...})"); - expect(util.transpileAndExecute(code, compilerOptions)).toBe("foobar"); + ` + .tap(builder => expect(builder.getMainLuaCodeChunk()).not.toMatch("{...}")) + .expectToMatchJsResult(); }); -test.each([{}, { noHoisting: true }])("Function rest parameter (referenced in property shorthand)", compilerOptions => { - const code = ` +test("Function rest parameter (referenced in property shorthand)", () => { + util.testFunction` function foo(a: unknown, ...b: string[]) { const c = { b }; return c.b.join(""); } return foo("A", "B", "C", "D"); - `; - - expect(util.transpileAndExecute(code, compilerOptions)).toBe("BCD"); + `.expectToMatchJsResult(); }); test("named function expression reference", () => { - const code = ` - const y = function x(inp: string) { - return inp + typeof x; + util.testFunction` + const y = function x() { + return { x: typeof x, y: typeof y }; }; - return y("foo-");`; - expect(util.transpileAndExecute(code)).toBe("foo-function"); + + return y(); + `.expectToMatchJsResult(); }); diff --git a/test/unit/hoisting.spec.ts b/test/unit/hoisting.spec.ts index 880908793..88f5ed2c5 100644 --- a/test/unit/hoisting.spec.ts +++ b/test/unit/hoisting.spec.ts @@ -182,30 +182,6 @@ test("Enum Hoisting", () => { expect(result).toBe("foo"); }); -test.each([ - { code: `foo = "foo"; var foo;`, identifier: "foo" }, - { code: `foo = "foo"; export var foo;`, identifier: "foo" }, - { code: `function setBar() { const bar = foo; } let foo = "foo";`, identifier: "foo" }, - { code: `function setBar() { const bar = foo; } const foo = "foo";`, identifier: "foo" }, - { code: `function setBar() { const bar = foo; } export let foo = "foo";`, identifier: "foo" }, - { code: `function setBar() { const bar = foo; } export const foo = "foo";`, identifier: "foo" }, - { code: `const foo = bar(); function bar() { return "bar"; }`, identifier: "bar" }, - { code: `export const foo = bar(); function bar() { return "bar"; }`, identifier: "bar" }, - { code: `const foo = bar(); export function bar() { return "bar"; }`, identifier: "bar" }, - { code: `function bar() { return NS.foo; } namespace NS { export let foo = "foo"; }`, identifier: "NS" }, - { - code: `export namespace O { export function f() { return I.foo; } namespace I { export let foo = "foo"; } }`, - identifier: "I", - }, - { code: `function makeFoo() { return new Foo(); } class Foo {}`, identifier: "Foo" }, - { code: `function bar() { return E.A; } enum E { A = "foo" }`, identifier: "E" }, - { code: `function setBar() { const bar = { foo }; } let foo = "foo";`, identifier: "foo" }, -])("No Hoisting (%p)", ({ code, identifier }) => { - expect(() => util.transpileString(code, { noHoisting: true })).toThrowExactError( - ReferencedBeforeDeclaration(ts.createIdentifier(identifier)) - ); -}); - test("Import hoisting (named)", () => { util.testBundle` export const result = foo; From a8e1be37b70126489faefcc934713712818b6a03 Mon Sep 17 00:00:00 2001 From: ark120202 Date: Sun, 19 Jan 2020 10:28:05 +0000 Subject: [PATCH 2/6] Remove unused declarations --- src/transformation/utils/errors.ts | 7 ------- test/unit/hoisting.spec.ts | 2 -- 2 files changed, 9 deletions(-) diff --git a/src/transformation/utils/errors.ts b/src/transformation/utils/errors.ts index 09634fcc8..91b07159b 100644 --- a/src/transformation/utils/errors.ts +++ b/src/transformation/utils/errors.ts @@ -130,13 +130,6 @@ export const UnsupportedNonDestructuringLuaIterator = (node: ts.Node) => export const UnresolvableRequirePath = (node: ts.Node, reason: string, path?: string) => new TranspileError(`${reason}. TypeScript path: ${path}.`, node); -export const ReferencedBeforeDeclaration = (node: ts.Identifier) => - new TranspileError( - `Identifier "${node.text}" was referenced before it was declared. The declaration ` + - "must be moved before the identifier's use, or hoisting must be enabled.", - node - ); - export const UnsupportedObjectDestructuringInForOf = (node: ts.Node) => new TranspileError(`Unsupported object destructuring in for...of statement.`, node); diff --git a/test/unit/hoisting.spec.ts b/test/unit/hoisting.spec.ts index 88f5ed2c5..3d146bb9d 100644 --- a/test/unit/hoisting.spec.ts +++ b/test/unit/hoisting.spec.ts @@ -1,5 +1,3 @@ -import * as ts from "typescript"; -import { ReferencedBeforeDeclaration } from "../../src/transformation/utils/errors"; import * as util from "../util"; test.each(["let", "const"])("Let/Const Hoisting (%p)", varType => { From d40d46b0e3e9dda1b32bd58d15e424b14644ebb6 Mon Sep 17 00:00:00 2001 From: ark120202 Date: Sun, 19 Jan 2020 10:35:48 +0000 Subject: [PATCH 3/6] Remove `noHoisting` references from `vararg` tests --- test/unit/annotations/vararg.spec.ts | 35 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/test/unit/annotations/vararg.spec.ts b/test/unit/annotations/vararg.spec.ts index 2a99fa4a7..000510ec4 100644 --- a/test/unit/annotations/vararg.spec.ts +++ b/test/unit/annotations/vararg.spec.ts @@ -1,8 +1,9 @@ import * as util from "../../util"; -test.each([{}, { noHoisting: true }])("@vararg", compilerOptions => { - const code = ` - /** @vararg */ type LuaVarArg = A & { __luaVarArg?: never }; +test("@vararg", () => { + util.testFunction` + /** @vararg */ + type LuaVarArg = A & { __luaVarArg?: never }; function foo(a: unknown, ...b: LuaVarArg) { const c = [...b]; return c.join(""); @@ -11,36 +12,34 @@ test.each([{}, { noHoisting: true }])("@vararg", compilerOptions => { return foo(a, ...b); } return bar("A", "B", "C", "D"); - `; - - const lua = util.transpileString(code, compilerOptions); - expect(lua).not.toMatch("b = ({...})"); - expect(lua).not.toMatch("unpack"); - expect(util.transpileAndExecute(code, compilerOptions)).toBe("BCD"); + ` + .tap(builder => expect(builder.getMainLuaCodeChunk()).not.toMatch("{...}")) + .tap(builder => expect(builder.getMainLuaCodeChunk()).not.toMatch("unpack")) + .expectToMatchJsResult(); }); -test.each([{}, { noHoisting: true }])("@vararg array access", compilerOptions => { - const code = ` - /** @vararg */ type LuaVarArg = A & { __luaVarArg?: never }; +test("@vararg array access", () => { + util.testFunction` + /** @vararg */ + type LuaVarArg = A & { __luaVarArg?: never }; function foo(a: unknown, ...b: LuaVarArg) { const c = [...b]; return c.join("") + b[0]; } return foo("A", "B", "C", "D"); - `; - - expect(util.transpileAndExecute(code, compilerOptions)).toBe("BCDB"); + `.expectToMatchJsResult(); }); -test.each([{}, { noHoisting: true }])("@vararg global", compilerOptions => { +test("@vararg global", () => { const code = ` - /** @vararg */ type LuaVarArg = A & { __luaVarArg?: never }; + /** @vararg */ + type LuaVarArg = A & { __luaVarArg?: never }; declare const arg: LuaVarArg; const arr = [...arg]; const result = arr.join(""); `; - const luaBody = util.transpileString(code, compilerOptions, false); + const luaBody = util.transpileString(code, undefined, false); expect(luaBody).not.toMatch("unpack"); const lua = ` From 8d0508fc9946fafc95e34cc5171c8d6aa96b4722 Mon Sep 17 00:00:00 2001 From: ark120202 Date: Sun, 19 Jan 2020 10:42:45 +0000 Subject: [PATCH 4/6] Fix vararg tests --- test/unit/annotations/vararg.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/annotations/vararg.spec.ts b/test/unit/annotations/vararg.spec.ts index 000510ec4..f4f0a7e94 100644 --- a/test/unit/annotations/vararg.spec.ts +++ b/test/unit/annotations/vararg.spec.ts @@ -13,7 +13,7 @@ test("@vararg", () => { } return bar("A", "B", "C", "D"); ` - .tap(builder => expect(builder.getMainLuaCodeChunk()).not.toMatch("{...}")) + .tap(builder => expect(builder.getMainLuaCodeChunk()).not.toMatch("b = ")) .tap(builder => expect(builder.getMainLuaCodeChunk()).not.toMatch("unpack")) .expectToMatchJsResult(); }); From 4a943fa1084ad4ff52bd9e3b16f15de90ddc5955 Mon Sep 17 00:00:00 2001 From: ark120202 Date: Mon, 20 Jan 2020 09:39:08 +0000 Subject: [PATCH 5/6] Use test builder in `@vararg global` test --- test/unit/annotations/vararg.spec.ts | 37 ++++----- test/util.ts | 117 ++++++++++++++------------- 2 files changed, 77 insertions(+), 77 deletions(-) diff --git a/test/unit/annotations/vararg.spec.ts b/test/unit/annotations/vararg.spec.ts index f4f0a7e94..982109261 100644 --- a/test/unit/annotations/vararg.spec.ts +++ b/test/unit/annotations/vararg.spec.ts @@ -1,9 +1,13 @@ import * as util from "../../util"; +const varargDeclaration = ` + /** @vararg */ + type LuaVarArg = A & { __luaVararg?: never }; +`; + test("@vararg", () => { util.testFunction` - /** @vararg */ - type LuaVarArg = A & { __luaVarArg?: never }; + ${varargDeclaration} function foo(a: unknown, ...b: LuaVarArg) { const c = [...b]; return c.join(""); @@ -20,8 +24,7 @@ test("@vararg", () => { test("@vararg array access", () => { util.testFunction` - /** @vararg */ - type LuaVarArg = A & { __luaVarArg?: never }; + ${varargDeclaration} function foo(a: unknown, ...b: LuaVarArg) { const c = [...b]; return c.join("") + b[0]; @@ -31,24 +34,12 @@ test("@vararg array access", () => { }); test("@vararg global", () => { - const code = ` - /** @vararg */ - type LuaVarArg = A & { __luaVarArg?: never }; + util.testModule` + ${varargDeclaration} declare const arg: LuaVarArg; - const arr = [...arg]; - const result = arr.join(""); - `; - - const luaBody = util.transpileString(code, undefined, false); - expect(luaBody).not.toMatch("unpack"); - - const lua = ` - function test(...) - ${luaBody} - return result - end - return test("A", "B", "C", "D") - `; - - expect(util.executeLua(lua)).toBe("ABCD"); + export const result = [...arg].join(""); + ` + .setLuaFactory(code => `return (function(...) ${code} end)("A", "B", "C", "D")`) + .tap(builder => expect(builder.getMainLuaCodeChunk()).not.toMatch("unpack")) + .expectToEqual({ result: "ABCD" }); }); diff --git a/test/util.ts b/test/util.ts index 6ca57509b..a3ac1ca8c 100644 --- a/test/util.ts +++ b/test/util.ts @@ -123,8 +123,57 @@ function executeLua(code: string): any { } } +const minimalTestLib = fs.readFileSync(path.join(__dirname, "json.lua"), "utf8") + "\n"; +const lualibContent = fs.readFileSync(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"), "utf8"); export function executeLuaModule(code: string): any { - return executeLua(`${minimalTestLib}return JSONStringify((function()\n${code}\nend)())`); + const lualibImport = code.includes('require("lualib_bundle")') + ? `package.preload.lualib_bundle = function()\n${lualibContent}\nend\n` + : ""; + + return executeLua(` + ${minimalTestLib} + ${lualibImport} + return JSONStringify((function()\n${code}\nend)()) + `); +} + +function executeJsModule(code: string): any { + const exports = {}; + const context = vm.createContext({ exports, module: { exports } }); + context.global = context; + let result: unknown; + try { + result = vm.runInContext(code, context); + } catch (error) { + return new ExecutionError(error.message); + } + + function transform(currentValue: any): any { + if (currentValue === null) { + return undefined; + } + + if (Array.isArray(currentValue)) { + return currentValue.map(transform); + } + + if (typeof currentValue === "object") { + for (const [key, value] of Object.entries(currentValue)) { + currentValue[key] = transform(value); + if (currentValue[key] === undefined) { + delete currentValue[key]; + } + } + + if (Object.keys(currentValue).length === 0) { + return []; + } + } + + return currentValue; + } + + return transform(result); } const memoize: MethodDecorator = (_target, _propertyKey, descriptor) => { @@ -177,6 +226,13 @@ export abstract class TestBuilder { return this; } + protected abstract getLuaCodeWithWrapper: (code: string) => string; + public setLuaFactory(luaFactory: (code: string) => string): this { + expect(this.hasProgram).toBe(false); + this.getLuaCodeWithWrapper = luaFactory; + return this; + } + private semanticCheck = true; public disableSemanticCheck(): this { expect(this.hasProgram).toBe(false); @@ -253,11 +309,9 @@ export abstract class TestBuilder { return header + this.getMainLuaFileResult().lua.trimRight(); } - public abstract getLuaCodeWithWrapper(): string; - @memoize public getLuaExecutionResult(): any { - return executeLua(this.getLuaCodeWithWrapper()); + return executeLuaModule(this.getLuaCodeWithWrapper(this.getMainLuaCodeChunk())); } @memoize @@ -281,42 +335,7 @@ export abstract class TestBuilder { @memoize public getJsExecutionResult(): any { - const exports = {}; - const context = vm.createContext({ exports, module: { exports } }); - context.global = context; - let result: unknown; - try { - result = vm.runInContext(this.getJsCodeWithWrapper(), context); - } catch (error) { - return new ExecutionError(error.message); - } - - function transform(currentValue: any): any { - if (currentValue === null) { - return undefined; - } - - if (Array.isArray(currentValue)) { - return currentValue.map(transform); - } - - if (typeof currentValue === "object") { - for (const [key, value] of Object.entries(currentValue)) { - currentValue[key] = transform(value); - if (currentValue[key] === undefined) { - delete currentValue[key]; - } - } - - if (Object.keys(currentValue).length === 0) { - return []; - } - } - - return currentValue; - } - - return transform(result); + return executeJsModule(this.getJsCodeWithWrapper()); } // Utilities @@ -329,9 +348,9 @@ export abstract class TestBuilder { // Actions public debug(): this { - const luaCode = this.getMainLuaCodeChunk().replace(/(^|\n)/g, "\n "); - const value = prettyFormat(this.getLuaExecutionResult()); - console.log(`Lua Code:${luaCode}\nValue: ${value}`); + const luaCode = this.getMainLuaCodeChunk().replace(/^/gm, " "); + const value = prettyFormat(this.getLuaExecutionResult()).replace(/^/gm, " "); + console.log(`Lua Code:\n${luaCode}\n\nValue:\n${value}`); return this; } @@ -408,20 +427,10 @@ export abstract class TestBuilder { } } -const lualibContent = fs.readFileSync(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"), "utf8"); -const minimalTestLib = fs.readFileSync(path.join(__dirname, "json.lua"), "utf8") + "\n"; class AccessorTestBuilder extends TestBuilder { protected accessor = ""; - @memoize - public getLuaCodeWithWrapper(): string { - let code = this.getMainLuaCodeChunk(); - if (code.includes('require("lualib_bundle")')) { - code = `package.preload.lualib_bundle = function()\n${lualibContent}\nend\n${code}`; - } - - return `${minimalTestLib}\nreturn JSONStringify((function()\n${code}\nend)()${this.accessor})`; - } + protected getLuaCodeWithWrapper = (code: string) => `return (function()\n${code}\nend)()${this.accessor}`; @memoize protected getJsCodeWithWrapper(): string { From 5b65fad1c4449fab59e3ec417d51f65eefcbbd1f Mon Sep 17 00:00:00 2001 From: ark120202 Date: Mon, 20 Jan 2020 10:04:04 +0000 Subject: [PATCH 6/6] Fix bundle tests --- test/util.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/test/util.ts b/test/util.ts index a3ac1ca8c..3caf10ec4 100644 --- a/test/util.ts +++ b/test/util.ts @@ -123,18 +123,14 @@ function executeLua(code: string): any { } } -const minimalTestLib = fs.readFileSync(path.join(__dirname, "json.lua"), "utf8") + "\n"; +const minimalTestLib = fs.readFileSync(path.join(__dirname, "json.lua"), "utf8"); const lualibContent = fs.readFileSync(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"), "utf8"); export function executeLuaModule(code: string): any { const lualibImport = code.includes('require("lualib_bundle")') - ? `package.preload.lualib_bundle = function()\n${lualibContent}\nend\n` + ? `package.preload.lualib_bundle = function()\n${lualibContent}\nend` : ""; - return executeLua(` - ${minimalTestLib} - ${lualibImport} - return JSONStringify((function()\n${code}\nend)()) - `); + return executeLua(`${minimalTestLib}\n${lualibImport}\nreturn JSONStringify((function()\n${code}\nend)())`); } function executeJsModule(code: string): any {