X Tutup
Skip to content

Commit e8ea8f3

Browse files
committed
style fixes
1 parent 92b680d commit e8ea8f3

File tree

4 files changed

+32
-44
lines changed

4 files changed

+32
-44
lines changed

src/LuaTransformer.ts

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5503,7 +5503,8 @@ export class LuaTransformer {
55035503

55045504
protected checkForLuaLibType(type: ts.Type): void {
55055505
if (type.symbol) {
5506-
switch (this.checker.getFullyQualifiedName(type.symbol)) {
5506+
const name = this.checker.getFullyQualifiedName(type.symbol);
5507+
switch (name) {
55075508
case "Map":
55085509
this.importLuaLibFeature(LuaLibFeature.Map);
55095510
return;
@@ -5516,42 +5517,9 @@ export class LuaTransformer {
55165517
case "WeakSet":
55175518
this.importLuaLibFeature(LuaLibFeature.WeakSet);
55185519
return;
5519-
case "Error":
5520-
this.importLuaLibFeature(LuaLibFeature.Error);
5521-
return;
5522-
case "ErrorConstructor":
5523-
this.importLuaLibFeature(LuaLibFeature.Error);
5524-
return;
5525-
case "RangeError":
5526-
this.importLuaLibFeature(LuaLibFeature.Error);
5527-
return;
5528-
case "RangeErrorConstructor":
5529-
this.importLuaLibFeature(LuaLibFeature.Error);
5530-
return;
5531-
case "ReferenceError":
5532-
this.importLuaLibFeature(LuaLibFeature.Error);
5533-
return;
5534-
case "ReferenceErrorConstructor":
5535-
this.importLuaLibFeature(LuaLibFeature.Error);
5536-
return;
5537-
case "SyntaxError":
5538-
this.importLuaLibFeature(LuaLibFeature.Error);
5539-
return;
5540-
case "SyntaxErrorConstructor":
5541-
this.importLuaLibFeature(LuaLibFeature.Error);
5542-
return;
5543-
case "TypeError":
5544-
this.importLuaLibFeature(LuaLibFeature.Error);
5545-
return;
5546-
case "TypeErrorConstructor":
5547-
this.importLuaLibFeature(LuaLibFeature.Error);
5548-
return;
5549-
case "URIError":
5550-
this.importLuaLibFeature(LuaLibFeature.Error);
5551-
return;
5552-
case "URIErrorConstructor":
5553-
this.importLuaLibFeature(LuaLibFeature.Error);
5554-
return;
5520+
}
5521+
if (tsHelper.isBuiltInErrorTypeName(name)) {
5522+
this.importLuaLibFeature(LuaLibFeature.Error);
55555523
}
55565524
}
55575525
}

src/TSHelper.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ const defaultArrayCallMethodNames = new Set<string>([
3333
"flatMap",
3434
]);
3535

36+
const builtinErrorTypeNames = new Set([
37+
"Error",
38+
"ErrorConstructor",
39+
"RangeError",
40+
"RangeErrorConstructor",
41+
"ReferenceError",
42+
"ReferenceErrorConstructor",
43+
"SyntaxError",
44+
"SyntaxErrorConstructor",
45+
"TypeError",
46+
"TypeErrorConstructor",
47+
"URIError",
48+
"URIErrorConstructor",
49+
]);
50+
3651
export function getExtendedTypeNode(
3752
node: ts.ClassLikeDeclarationBase,
3853
checker: ts.TypeChecker
@@ -1041,3 +1056,7 @@ export function formatPathToLuaPath(filePath: string): string {
10411056
}
10421057
return filePath.replace(/\.\//g, "").replace(/\//g, ".");
10431058
}
1059+
1060+
export function isBuiltInErrorTypeName(name: string): boolean {
1061+
return builtinErrorTypeNames.has(name);
1062+
}

src/lualib/Error.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ type TSTLCapturedErrorStack = Array<{
66
currentline: number;
77
}>;
88

9+
interface ErrorType<T> extends Function {
10+
name: string;
11+
new (...args: any[]): T;
12+
}
13+
914
function __TS__GetErrorStack(constructor: Function): TSTLCapturedErrorStack {
1015
const functionFrames = [];
1116
let level = 1;
@@ -49,7 +54,7 @@ function __TS__GetErrorString(this: void, error: Error): string {
4954
return error.message !== "" ? `${error.name}: ${error.message}` : error.name;
5055
}
5156

52-
function __TS__InitErrorClass(Type: any, name?: string): any {
57+
function __TS__InitErrorClass<T>(Type: ErrorType<T>, name?: string): any {
5358
if (name) {
5459
Type.name = name;
5560
}

test/unit/error.spec.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,10 @@ test("throw and catch custom error object", () => {
296296
`.expectToMatchJsResult();
297297
});
298298

299-
test.each(["Error", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError", "MyError"])(
299+
test.each(["Error", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError"])(
300300
"throw builtin Errors as classes",
301301
errorType => {
302302
util.testFunction`
303-
class MyError extends Error {
304-
name: "MyError"
305-
}
306-
307303
try {
308304
throw new ${errorType}("message")
309305
} catch (error) {
@@ -348,7 +344,7 @@ test("subclass Error", () => {
348344
throw new MyError();
349345
} catch (error) {
350346
if (error instanceof Error) {
351-
return error.name;
347+
return \'\$error\';
352348
} else {
353349
throw TypeError();
354350
}

0 commit comments

Comments
 (0)
X Tutup