|
1 | 1 | import * as path from "path"; |
2 | 2 | import * as util from "../util"; |
| 3 | +import { TranspileVirtualProjectResult } from "../../src"; |
| 4 | +import { lineAndColumnOf } from "../unit/printer/utils"; |
| 5 | +import * as fs from "fs"; |
3 | 6 |
|
4 | | -const projectDir = path.join(__dirname, "bundle"); |
5 | | -const inputProject = path.join(projectDir, "tsconfig.json"); |
| 7 | +describe("bundle two files", () => { |
| 8 | + const projectDir = path.join(__dirname, "bundle", "bundle-two-files"); |
| 9 | + const inputProject = path.join(projectDir, "tsconfig.json"); |
6 | 10 |
|
7 | | -test("should transpile into one file", () => { |
8 | | - const { diagnostics, transpiledFiles } = util.testProject(inputProject).getLuaResult(); |
| 11 | + let transpileResult: TranspileVirtualProjectResult = { |
| 12 | + transpiledFiles: [], |
| 13 | + diagnostics: [], |
| 14 | + }; |
9 | 15 |
|
10 | | - expect(diagnostics).not.toHaveDiagnostics(); |
11 | | - expect(transpiledFiles).toHaveLength(1); |
| 16 | + beforeAll(() => { |
| 17 | + transpileResult = util.testProject(inputProject).getLuaResult(); |
| 18 | + }); |
| 19 | + |
| 20 | + test("should transpile into one file (with no errors)", () => { |
| 21 | + expect(transpileResult.diagnostics).not.toHaveDiagnostics(); |
| 22 | + expect(transpileResult.transpiledFiles).toHaveLength(1); |
| 23 | + }); |
12 | 24 |
|
13 | | - const { outPath, lua } = transpiledFiles[0]; |
14 | 25 | // Verify the name is as specified in tsconfig |
15 | | - expect(outPath.endsWith(path.join("bundle", "bundle.lua"))).toBe(true); |
| 26 | + test("should have name, specified in tsconfig.json", () => { |
| 27 | + const { outPath } = transpileResult.transpiledFiles[0]; |
| 28 | + expect(outPath.endsWith(path.join(projectDir, "bundle.lua"))).toBe(true); |
| 29 | + }); |
| 30 | + |
| 31 | + // Verify exported module by executing |
| 32 | + // Use an empty TS string because we already transpiled the TS project |
| 33 | + test("executing should act correctly", () => { |
| 34 | + const { lua } = transpileResult.transpiledFiles[0]; |
| 35 | + util.testModule("").setLuaHeader(lua!).expectToEqual({ myNumber: 3 }); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("bundle with source maps", () => { |
| 40 | + const projectDir = path.join(__dirname, "bundle", "bundle-source-maps"); |
| 41 | + const inputProject = path.join(projectDir, "tsconfig.json"); |
| 42 | + |
| 43 | + let transpileResult: TranspileVirtualProjectResult = { |
| 44 | + transpiledFiles: [], |
| 45 | + diagnostics: [], |
| 46 | + }; |
| 47 | + |
| 48 | + beforeAll(() => { |
| 49 | + transpileResult = util.testProject(inputProject).getLuaResult(); |
| 50 | + }); |
| 51 | + |
| 52 | + // See https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1109 |
| 53 | + test('the result file should not contain "{#SourceMapTraceback}" macro-string', () => { |
| 54 | + const { lua } = transpileResult.transpiledFiles[0]; |
| 55 | + expect(lua).not.toBeUndefined(); |
| 56 | + expect(lua!).not.toContain("{#SourceMapTraceback}"); |
| 57 | + }); |
| 58 | + |
16 | 59 | // Verify exported module by executing |
17 | 60 | // Use an empty TS string because we already transpiled the TS project |
18 | | - util.testModule("").setLuaHeader(lua!).expectToEqual({ myNumber: 3 }); |
| 61 | + test("executing should act correctly", () => { |
| 62 | + const { lua } = transpileResult.transpiledFiles[0]; |
| 63 | + const result = util.testModule("").setLuaHeader(lua!).getLuaExecutionResult(); |
| 64 | + |
| 65 | + expect(result.myNumber).toEqual(3 * 4 * (5 + 6)); |
| 66 | + }); |
| 67 | + |
| 68 | + test("sourceMapTraceback saves correct sourcemap", () => { |
| 69 | + const code = { |
| 70 | + index: fs.readFileSync(path.join(projectDir, "index.ts"), "utf8"), |
| 71 | + largeFile: fs.readFileSync(path.join(projectDir, "largeFile.ts"), "utf8"), |
| 72 | + }; |
| 73 | + |
| 74 | + const { lua } = transpileResult.transpiledFiles[0]; |
| 75 | + const builder = util.testModule("").setLuaHeader(lua!); |
| 76 | + const result = builder.getLuaExecutionResult(); |
| 77 | + const sourceMap = result.sourceMap; |
| 78 | + |
| 79 | + expect(sourceMap).toEqual(expect.any(Object)); |
| 80 | + const sourceMapFiles = Object.keys(sourceMap); |
| 81 | + expect(sourceMapFiles).toHaveLength(1); |
| 82 | + const mainSourceMap = sourceMap[sourceMapFiles[0]]; |
| 83 | + |
| 84 | + const transpiledLua = builder.getMainLuaCodeChunk(); |
| 85 | + |
| 86 | + const assertPatterns: Array<{ |
| 87 | + file: keyof typeof code; |
| 88 | + luaPattern: string; |
| 89 | + typeScriptPattern: string; |
| 90 | + }> = [ |
| 91 | + { |
| 92 | + file: "index", |
| 93 | + luaPattern: "____exports.myNumber = getNumber(", |
| 94 | + typeScriptPattern: "const myNumber = getNumber(", |
| 95 | + }, |
| 96 | + { |
| 97 | + file: "largeFile", |
| 98 | + luaPattern: "local Calculator = __TS__Class()", |
| 99 | + typeScriptPattern: "abstract class Calculator", |
| 100 | + }, |
| 101 | + { |
| 102 | + file: "largeFile", |
| 103 | + luaPattern: "local CalculatorMul = __TS__Class()", |
| 104 | + typeScriptPattern: "class CalculatorMul extends Calculator {", |
| 105 | + }, |
| 106 | + { |
| 107 | + file: "largeFile", |
| 108 | + luaPattern: "local function resolveCalculatorClass(", |
| 109 | + typeScriptPattern: "function resolveCalculatorClass(", |
| 110 | + }, |
| 111 | + { |
| 112 | + file: "largeFile", |
| 113 | + luaPattern: "function ____exports.getNumber(", |
| 114 | + typeScriptPattern: "export function getNumber(", |
| 115 | + }, |
| 116 | + { |
| 117 | + file: "largeFile", |
| 118 | + luaPattern: 'Error,\n "Unknown operation "', |
| 119 | + typeScriptPattern: "throw new Error(", |
| 120 | + }, |
| 121 | + ]; |
| 122 | + |
| 123 | + for (const { file: currentFile, luaPattern, typeScriptPattern } of assertPatterns) { |
| 124 | + const luaPosition = lineAndColumnOf(transpiledLua, luaPattern); |
| 125 | + const mappedLine: { file: string; line: number } = mainSourceMap[luaPosition.line.toString()]; |
| 126 | + |
| 127 | + const typescriptPosition = lineAndColumnOf(code[currentFile], typeScriptPattern); |
| 128 | + expect(mappedLine.line).toEqual(typescriptPosition.line); |
| 129 | + expect(mappedLine.file).toEqual(`${currentFile}.ts`); |
| 130 | + } |
| 131 | + }); |
19 | 132 | }); |
0 commit comments