Skip to content

Commit f4f7a92

Browse files
committed
Fix tests and update snapshots
1 parent 558b7b4 commit f4f7a92

14 files changed

Lines changed: 74 additions & 28 deletions

File tree

src/LuaPrinter.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class LuaPrinter {
173173

174174
public print(file: lua.File): PrintResult {
175175
// Add traceback lualib if sourcemap traceback option is enabled
176-
if (this.options.sourceMapTraceback) {
176+
if (this.options.sourceMapTraceback && !isBundleEnabled(this.options)) {
177177
file.luaLibFeatures.add(LuaLibFeature.SourceMapTraceBack);
178178
}
179179

@@ -232,6 +232,7 @@ export class LuaPrinter {
232232
if (!this.options.noHeader) {
233233
header += tstlHeader;
234234
}
235+
let statements = file.statements;
235236

236237
const luaLibImport = this.options.luaLibImport ?? LuaLibImportKind.Require;
237238
if (
@@ -244,7 +245,8 @@ export class LuaPrinter {
244245
this.emitHost,
245246
luaLibImport === LuaLibImportKind.Always
246247
);
247-
header += this.concatNodes(...this.printStatementArray(importStatements)).toString();
248+
249+
statements = importStatements.concat(statements);
248250
} else if (luaLibImport === LuaLibImportKind.Inline && file.luaLibFeatures.size > 0) {
249251
// Inline lualib features
250252
header += "-- Lua Library inline imports\n";
@@ -257,7 +259,7 @@ export class LuaPrinter {
257259
header += `${LuaPrinter.sourceMapTracebackPlaceholder}\n`;
258260
}
259261

260-
return this.concatNodes(header, ...this.printStatementArray(file.statements));
262+
return this.concatNodes(header, ...this.printStatementArray(statements));
261263
}
262264

263265
protected pushIndent(): void {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { TransformationContext } from "../context";
2+
import * as ts from "typescript";
3+
import { isStandardLibraryType } from "../utils/typescript";
4+
5+
const errorClasses = new Set([
6+
"Error",
7+
"EvalError",
8+
"RangeError",
9+
"ReferenceError",
10+
"SyntaxError",
11+
"TypeError",
12+
"URIError",
13+
]);
14+
15+
export function isErrorClass(context: TransformationContext, node: ts.Identifier) {
16+
const type = context.checker.getTypeAtLocation(node);
17+
return isStandardLibraryType(context, type, undefined) && errorClasses.has(node.text);
18+
}

src/transformation/visitors/identifier.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { isRangeFunctionNode } from "./language-extensions/range";
2222
import { isTableExtensionIdentifier } from "./language-extensions/table";
2323
import { isVarargConstantNode } from "./language-extensions/vararg";
2424
import { isOptionalContinuation } from "./optional-chaining";
25+
import { isErrorClass } from "../builtins/error";
2526

2627
export function transformIdentifier(context: TransformationContext, identifier: ts.Identifier): lua.Identifier {
2728
if (isOptionalContinuation(identifier)) {
@@ -59,9 +60,9 @@ export function transformIdentifier(context: TransformationContext, identifier:
5960
importLuaLibFeature(context, LuaLibFeature.Promise);
6061
return lua.createIdentifier("__TS__Promise", identifier);
6162
}
62-
if (isPromiseClass(context, identifier)) {
63-
importLuaLibFeature(context, LuaLibFeature.Promise);
64-
return lua.createIdentifier("__TS__Promise", identifier);
63+
64+
if (isErrorClass(context, identifier)) {
65+
importLuaLibFeature(context, LuaLibFeature.Error);
6566
}
6667

6768
const text = hasUnsafeIdentifierName(context, identifier) ? createSafeName(identifier.text) : identifier.text;

src/transpilation/bundle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function getBundleResult(program: ts.Program, files: ProcessedFile[]): [t
105105
if (options.sourceMapTraceback) {
106106
// Generates SourceMapTraceback for the entire file
107107
// TODO: remove
108-
footers.push('require("lualib_bundle")\n');
108+
footers.push('local __TS__SourceMapTraceBack = require("lualib_bundle").__TS__SourceMapTraceBack\n');
109109
footers.push(`${sourceMapTracebackBundlePlaceholder}\n`);
110110
}
111111

test/translation/__snapshots__/transformation.spec.ts.snap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ return ____exports"
5858
`;
5959

6060
exports[`Transformation (methodRestArguments) 1`] = `
61-
"require(\\"lualib_bundle\\");
61+
"local ____lualib = require(\\"lualib_bundle\\")
62+
local __TS__Class = ____lualib.__TS__Class
6263
MyClass = __TS__Class()
6364
MyClass.name = \\"MyClass\\"
6465
function MyClass.prototype.____constructor(self)
@@ -74,7 +75,8 @@ return ____exports"
7475
`;
7576

7677
exports[`Transformation (modulesClassExport) 1`] = `
77-
"require(\\"lualib_bundle\\");
78+
"local ____lualib = require(\\"lualib_bundle\\")
79+
local __TS__Class = ____lualib.__TS__Class
7880
local ____exports = {}
7981
____exports.TestClass = __TS__Class()
8082
local TestClass = ____exports.TestClass
@@ -85,7 +87,8 @@ return ____exports"
8587
`;
8688

8789
exports[`Transformation (modulesClassWithMemberExport) 1`] = `
88-
"require(\\"lualib_bundle\\");
90+
"local ____lualib = require(\\"lualib_bundle\\")
91+
local __TS__Class = ____lualib.__TS__Class
8992
local ____exports = {}
9093
____exports.TestClass = __TS__Class()
9194
local TestClass = ____exports.TestClass

test/unit/__snapshots__/expressions.spec.ts.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ return ____exports"
503503
`;
504504
505505
exports[`Unary expressions basic ("delete tbl.test") 1`] = `
506-
"require(\\"lualib_bundle\\");
506+
"local ____lualib = require(\\"lualib_bundle\\")
507+
local __TS__Delete = ____lualib.__TS__Delete
507508
local ____exports = {}
508509
function ____exports.__main(self)
509510
__TS__Delete(tbl, \\"test\\")
@@ -512,7 +513,8 @@ return ____exports"
512513
`;
513514
514515
exports[`Unary expressions basic ("delete tbl['test']") 1`] = `
515-
"require(\\"lualib_bundle\\");
516+
"local ____lualib = require(\\"lualib_bundle\\")
517+
local __TS__Delete = ____lualib.__TS__Delete
516518
local ____exports = {}
517519
function ____exports.__main(self)
518520
__TS__Delete(tbl, \\"test\\")
@@ -537,7 +539,8 @@ return ____exports"
537539
`;
538540
539541
exports[`Unary expressions basic ("let a = delete tbl.test") 1`] = `
540-
"require(\\"lualib_bundle\\");
542+
"local ____lualib = require(\\"lualib_bundle\\")
543+
local __TS__Delete = ____lualib.__TS__Delete
541544
local ____exports = {}
542545
function ____exports.__main(self)
543546
local a = __TS__Delete(tbl, \\"test\\")
@@ -546,7 +549,8 @@ return ____exports"
546549
`;
547550
548551
exports[`Unary expressions basic ("let a = delete tbl['test']") 1`] = `
549-
"require(\\"lualib_bundle\\");
552+
"local ____lualib = require(\\"lualib_bundle\\")
553+
local __TS__Delete = ____lualib.__TS__Delete
550554
local ____exports = {}
551555
function ____exports.__main(self)
552556
local a = __TS__Delete(tbl, \\"test\\")

test/unit/__snapshots__/optionalChaining.spec.ts.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Unsupported optional chains Builtin global method: code 1`] = `
4-
"require(\\"lualib_bundle\\");
4+
"local ____lualib = require(\\"lualib_bundle\\")
5+
local __TS__Number = ____lualib.__TS__Number
56
local ____Number_result_0 = Number
67
if ____Number_result_0 ~= nil then
78
____Number_result_0 = nil

test/unit/__snapshots__/switch.spec.ts.snap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`switch empty fallthrough to default (0) 1`] = `
4-
"require(\\"lualib_bundle\\");
4+
"local ____lualib = require(\\"lualib_bundle\\")
5+
local __TS__ArrayPush = ____lualib.__TS__ArrayPush
56
local ____exports = {}
67
function ____exports.__main(self)
78
local out = {}
@@ -18,7 +19,8 @@ return ____exports"
1819
`;
1920

2021
exports[`switch empty fallthrough to default (1) 1`] = `
21-
"require(\\"lualib_bundle\\");
22+
"local ____lualib = require(\\"lualib_bundle\\")
23+
local __TS__ArrayPush = ____lualib.__TS__ArrayPush
2224
local ____exports = {}
2325
function ____exports.__main(self)
2426
local out = {}
@@ -111,7 +113,8 @@ return ____exports"
111113
`;
112114

113115
exports[`switch produces optimal output 1`] = `
114-
"require(\\"lualib_bundle\\");
116+
"local ____lualib = require(\\"lualib_bundle\\")
117+
local __TS__ArrayPush = ____lualib.__TS__ArrayPush
115118
local ____exports = {}
116119
function ____exports.__main(self)
117120
local x = 0

test/unit/annotations/__snapshots__/customConstructor.spec.ts.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`IncorrectUsage: code 1`] = `
4-
"require(\\"lualib_bundle\\");
4+
"local ____lualib = require(\\"lualib_bundle\\")
5+
local __TS__Class = ____lualib.__TS__Class
6+
local __TS__New = ____lualib.__TS__New
57
local ____exports = {}
68
function ____exports.__main(self)
79
local Point2D = __TS__Class()

test/unit/annotations/__snapshots__/deprecated.spec.ts.snap

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ return ____exports"
1111
exports[`LuaTable deprecation warning property access set: diagnostics 1`] = `"main.ts(12,16): error TSTL: '@luaTable' has been removed and will no longer have any effect.See https://typescripttolua.github.io/docs/advanced/compiler-annotations#luatable for more information."`;
1212

1313
exports[`LuaTable removed warning constructor: code 1`] = `
14-
"require(\\"lualib_bundle\\");
14+
"local ____lualib = require(\\"lualib_bundle\\")
15+
local __TS__New = ____lualib.__TS__New
1516
____table = __TS__New(Table)"
1617
`;
1718

@@ -38,14 +39,18 @@ return ____exports"
3839
exports[`LuaTable removed warning property access length: diagnostics 1`] = `"main.ts(12,16): error TSTL: '@luaTable' has been removed and will no longer have any effect.See https://typescripttolua.github.io/docs/advanced/compiler-annotations#luatable for more information."`;
3940

4041
exports[`extension removed: code 1`] = `
41-
"require(\\"lualib_bundle\\");
42+
"local ____lualib = require(\\"lualib_bundle\\")
43+
local __TS__Class = ____lualib.__TS__Class
44+
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
4245
B = __TS__Class()
4346
B.name = \\"B\\"
4447
__TS__ClassExtends(B, A)"
4548
`;
4649

4750
exports[`extension removed: code 2`] = `
48-
"require(\\"lualib_bundle\\");
51+
"local ____lualib = require(\\"lualib_bundle\\")
52+
local __TS__Class = ____lualib.__TS__Class
53+
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
4954
B = __TS__Class()
5055
B.name = \\"B\\"
5156
__TS__ClassExtends(B, A)"
@@ -90,7 +95,9 @@ end"
9095
exports[`phantom removed: diagnostics 1`] = `"main.ts(3,9): error TSTL: '@phantom' has been removed and will no longer have any effect.See https://typescripttolua.github.io/docs/advanced/compiler-annotations#phantom for more information."`;
9196

9297
exports[`pureAbstract removed: code 1`] = `
93-
"require(\\"lualib_bundle\\");
98+
"local ____lualib = require(\\"lualib_bundle\\")
99+
local __TS__Class = ____lualib.__TS__Class
100+
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
94101
ClassB = __TS__Class()
95102
ClassB.name = \\"ClassB\\"
96103
__TS__ClassExtends(ClassB, ClassA)"

0 commit comments

Comments
 (0)