Skip to content

Commit 2b9c2a6

Browse files
committed
Make UnsupportedProperty error a diagnostic
1 parent 8aff305 commit 2b9c2a6

17 files changed

Lines changed: 138 additions & 89 deletions

File tree

src/transformation/builtins/array.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
4-
import { UnsupportedProperty } from "../utils/errors";
4+
import { unsupportedProperty } from "../utils/diagnostics";
55
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
66
import { isExplicitArrayType } from "../utils/typescript";
77
import { PropertyCallExpression, transformArguments } from "../visitors/call";
@@ -80,7 +80,7 @@ export function transformArrayPrototypeCall(
8080
return transformLuaLibFunction(context, LuaLibFeature.ArrayFlatMap, node, caller, ...params);
8181
default:
8282
if (isExplicitArrayType(context, ownerType)) {
83-
throw UnsupportedProperty("array", expressionName, node);
83+
context.diagnostics.push(unsupportedProperty(node, "array", expressionName));
8484
}
8585
}
8686
}

src/transformation/builtins/console.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
4-
import { UnsupportedProperty } from "../utils/errors";
4+
import { unsupportedProperty } from "../utils/diagnostics";
55
import { PropertyCallExpression, transformArguments } from "../visitors/call";
66

77
const isStringFormatTemplate = (node: ts.Expression) => ts.isStringLiteral(node) && node.text.includes("%");
88

99
export function transformConsoleCall(
1010
context: TransformationContext,
1111
expression: PropertyCallExpression
12-
): lua.Expression {
12+
): lua.Expression | undefined {
1313
const method = expression.expression;
1414
const methodName = method.name.text;
1515
const signature = context.checker.getResolvedSignature(expression);
@@ -61,6 +61,6 @@ export function transformConsoleCall(
6161
);
6262
return lua.createCallExpression(lua.createIdentifier("print"), [debugTracebackCall]);
6363
default:
64-
throw UnsupportedProperty("console", methodName, expression);
64+
context.diagnostics.push(unsupportedProperty(expression, "console", methodName));
6565
}
6666
}

src/transformation/builtins/function.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import * as lua from "../../LuaAST";
22
import { TransformationContext } from "../context";
3-
import { unsupportedSelfFunctionConversion } from "../utils/diagnostics";
4-
import { UnsupportedProperty } from "../utils/errors";
3+
import { unsupportedProperty, unsupportedSelfFunctionConversion } from "../utils/diagnostics";
54
import { ContextType, getFunctionContextType } from "../utils/function-context";
65
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
76
import { PropertyCallExpression, transformArguments } from "../visitors/call";
87

98
export function transformFunctionPrototypeCall(
109
context: TransformationContext,
1110
node: PropertyCallExpression
12-
): lua.CallExpression {
11+
): lua.CallExpression | undefined {
1312
const expression = node.expression;
1413
const callerType = context.checker.getTypeAtLocation(expression.expression);
1514
if (getFunctionContextType(context, callerType) === ContextType.Void) {
@@ -28,6 +27,6 @@ export function transformFunctionPrototypeCall(
2827
case "call":
2928
return transformLuaLibFunction(context, LuaLibFeature.FunctionCall, node, caller, ...params);
3029
default:
31-
throw UnsupportedProperty("function", expressionName, node);
30+
context.diagnostics.push(unsupportedProperty(node, "function", expressionName));
3231
}
3332
}

src/transformation/builtins/index.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,22 @@ export function transformBuiltinPropertyAccessExpression(
2121
context: TransformationContext,
2222
node: ts.PropertyAccessExpression
2323
): lua.Expression | undefined {
24-
const type = context.checker.getTypeAtLocation(node.expression);
25-
if (isStringType(context, type)) {
24+
const ownerType = context.checker.getTypeAtLocation(node.expression);
25+
26+
if (isStringType(context, ownerType)) {
2627
return transformStringProperty(context, node);
27-
} else if (isArrayType(context, type)) {
28-
const arrayPropertyAccess = transformArrayProperty(context, node);
29-
if (arrayPropertyAccess) {
30-
return arrayPropertyAccess;
31-
}
3228
}
3329

34-
if (ts.isIdentifier(node.expression)) {
35-
const ownerType = context.checker.getTypeAtLocation(node.expression);
30+
if (isArrayType(context, ownerType)) {
31+
return transformArrayProperty(context, node);
32+
}
3633

37-
if (isStandardLibraryType(context, ownerType, "Math")) {
38-
return transformMathProperty(node);
39-
} else if (isStandardLibraryType(context, ownerType, "Symbol")) {
40-
// Pull in Symbol lib
41-
importLuaLibFeature(context, LuaLibFeature.Symbol);
34+
if (ts.isIdentifier(node.expression) && isStandardLibraryType(context, ownerType, undefined)) {
35+
switch (node.expression.text) {
36+
case "Math":
37+
return transformMathProperty(context, node);
38+
case "Symbol":
39+
importLuaLibFeature(context, LuaLibFeature.Symbol);
4240
}
4341
}
4442
}

src/transformation/builtins/math.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
4-
import { UnsupportedProperty } from "../utils/errors";
4+
import { unsupportedProperty } from "../utils/diagnostics";
55
import { PropertyCallExpression, transformArguments } from "../visitors/call";
66

7-
export function transformMathProperty(node: ts.PropertyAccessExpression): lua.Expression {
7+
export function transformMathProperty(
8+
context: TransformationContext,
9+
node: ts.PropertyAccessExpression
10+
): lua.Expression | undefined {
811
const name = node.name.text;
912
switch (name) {
1013
case "PI":
@@ -22,11 +25,14 @@ export function transformMathProperty(node: ts.PropertyAccessExpression): lua.Ex
2225
return lua.createNumericLiteral(Math[name], node);
2326

2427
default:
25-
throw UnsupportedProperty("Math", name, node);
28+
context.diagnostics.push(unsupportedProperty(node, "Math", name));
2629
}
2730
}
2831

29-
export function transformMathCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression {
32+
export function transformMathCall(
33+
context: TransformationContext,
34+
node: PropertyCallExpression
35+
): lua.Expression | undefined {
3036
const expression = node.expression;
3137
const signature = context.checker.getResolvedSignature(node);
3238
const params = transformArguments(context, node.arguments, signature);
@@ -92,6 +98,6 @@ export function transformMathCall(context: TransformationContext, node: Property
9298
}
9399

94100
default:
95-
throw UnsupportedProperty("Math", expressionName, expression);
101+
context.diagnostics.push(unsupportedProperty(expression, "Math", expressionName));
96102
}
97103
}

src/transformation/builtins/number.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as lua from "../../LuaAST";
22
import { TransformationContext } from "../context";
3-
import { UnsupportedProperty } from "../utils/errors";
3+
import { unsupportedProperty } from "../utils/diagnostics";
44
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
55
import { PropertyCallExpression, transformArguments } from "../visitors/call";
66

77
export function transformNumberPrototypeCall(
88
context: TransformationContext,
99
node: PropertyCallExpression
10-
): lua.Expression {
10+
): lua.Expression | undefined {
1111
const expression = node.expression;
1212
const signature = context.checker.getResolvedSignature(node);
1313
const params = transformArguments(context, node.arguments, signature);
@@ -20,14 +20,14 @@ export function transformNumberPrototypeCall(
2020
? lua.createCallExpression(lua.createIdentifier("tostring"), [caller], node)
2121
: transformLuaLibFunction(context, LuaLibFeature.NumberToString, node, caller, ...params);
2222
default:
23-
throw UnsupportedProperty("number", expressionName, node);
23+
context.diagnostics.push(unsupportedProperty(node, "number", expressionName));
2424
}
2525
}
2626

2727
export function transformNumberConstructorCall(
2828
context: TransformationContext,
2929
expression: PropertyCallExpression
30-
): lua.CallExpression {
30+
): lua.CallExpression | undefined {
3131
const method = expression.expression;
3232
const parameters = transformArguments(context, expression.arguments);
3333
const methodName = method.name.text;
@@ -37,6 +37,6 @@ export function transformNumberConstructorCall(
3737
case "isFinite":
3838
return transformLuaLibFunction(context, LuaLibFeature.NumberIsFinite, expression, ...parameters);
3939
default:
40-
throw UnsupportedProperty("Number", methodName, expression);
40+
context.diagnostics.push(unsupportedProperty(expression, "Number", methodName));
4141
}
4242
}

src/transformation/builtins/object.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as lua from "../../LuaAST";
22
import { TransformationContext } from "../context";
3-
import { UnsupportedProperty } from "../utils/errors";
3+
import { unsupportedProperty } from "../utils/diagnostics";
44
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
55
import { PropertyCallExpression, transformArguments } from "../visitors/call";
66

77
export function transformObjectConstructorCall(
88
context: TransformationContext,
99
expression: PropertyCallExpression
10-
): lua.Expression {
10+
): lua.Expression | undefined {
1111
const method = expression.expression;
1212
const parameters = transformArguments(context, expression.arguments);
1313
const methodName = method.name.text;
@@ -24,7 +24,7 @@ export function transformObjectConstructorCall(
2424
case "values":
2525
return transformLuaLibFunction(context, LuaLibFeature.ObjectValues, expression, ...parameters);
2626
default:
27-
throw UnsupportedProperty("Object", methodName, expression);
27+
context.diagnostics.push(unsupportedProperty(expression, "Object", methodName));
2828
}
2929
}
3030

src/transformation/builtins/string.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
4-
import { UnsupportedProperty } from "../utils/errors";
4+
import { unsupportedProperty } from "../utils/diagnostics";
55
import { createExpressionPlusOne } from "../utils/lua-ast";
66
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
77
import { PropertyCallExpression, transformArguments } from "../visitors/call";
@@ -19,7 +19,7 @@ function createStringCall(methodName: string, tsOriginal: ts.Node, ...params: lu
1919
export function transformStringPrototypeCall(
2020
context: TransformationContext,
2121
node: PropertyCallExpression
22-
): lua.Expression {
22+
): lua.Expression | undefined {
2323
const expression = node.expression;
2424
const signature = context.checker.getResolvedSignature(node);
2525
const params = transformArguments(context, node.arguments, signature);
@@ -148,14 +148,14 @@ export function transformStringPrototypeCall(
148148
node
149149
);
150150
default:
151-
throw UnsupportedProperty("string", expressionName, node);
151+
context.diagnostics.push(unsupportedProperty(node, "string", expressionName));
152152
}
153153
}
154154

155155
export function transformStringConstructorCall(
156156
context: TransformationContext,
157157
node: PropertyCallExpression
158-
): lua.Expression {
158+
): lua.Expression | undefined {
159159
const expression = node.expression;
160160
const signature = context.checker.getResolvedSignature(node);
161161
const params = transformArguments(context, node.arguments, signature);
@@ -170,14 +170,14 @@ export function transformStringConstructorCall(
170170
);
171171

172172
default:
173-
throw UnsupportedProperty("String", expressionName, node);
173+
context.diagnostics.push(unsupportedProperty(node, "String", expressionName));
174174
}
175175
}
176176

177177
export function transformStringProperty(
178178
context: TransformationContext,
179179
node: ts.PropertyAccessExpression
180-
): lua.UnaryExpression {
180+
): lua.UnaryExpression | undefined {
181181
switch (node.name.text) {
182182
case "length":
183183
let expression = context.transformExpression(node.expression);
@@ -186,6 +186,6 @@ export function transformStringProperty(
186186
}
187187
return lua.createUnaryExpression(expression, lua.SyntaxKind.LengthOperator, node);
188188
default:
189-
throw UnsupportedProperty("string", node.name.text, node);
189+
context.diagnostics.push(unsupportedProperty(node, "string", node.name.text));
190190
}
191191
}

src/transformation/builtins/symbol.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import * as lua from "../../LuaAST";
22
import { TransformationContext } from "../context";
3-
import { UnsupportedProperty } from "../utils/errors";
3+
import { unsupportedProperty } from "../utils/diagnostics";
44
import { importLuaLibFeature, LuaLibFeature } from "../utils/lualib";
55
import { PropertyCallExpression, transformArguments } from "../visitors/call";
66

7-
// Transpile a Symbol._ property
87
export function transformSymbolConstructorCall(
98
context: TransformationContext,
109
expression: PropertyCallExpression
11-
): lua.CallExpression {
10+
): lua.CallExpression | undefined {
1211
const method = expression.expression;
1312
const signature = context.checker.getResolvedSignature(expression);
1413
const parameters = transformArguments(context, expression.arguments, signature);
@@ -21,6 +20,6 @@ export function transformSymbolConstructorCall(
2120
const functionIdentifier = lua.createIdentifier(`__TS__SymbolRegistry${upperMethodName}`);
2221
return lua.createCallExpression(functionIdentifier, parameters, expression);
2322
default:
24-
throw UnsupportedProperty("Symbol", methodName, expression);
23+
context.diagnostics.push(unsupportedProperty(expression, "Symbol", methodName));
2524
}
2625
}

src/transformation/utils/diagnostics.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,7 @@ export const unsupportedForTarget = createDiagnosticFactory(
114114
(functionality: string, version: LuaTarget) =>
115115
`${functionality} is/are not supported for target ${getLuaTargetName(version)}.`
116116
);
117+
118+
export const unsupportedProperty = createDiagnosticFactory(
119+
(parentName: string, property: string) => `${parentName}.${property} is unsupported.`
120+
);

0 commit comments

Comments
 (0)