Skip to content

Commit 8aff305

Browse files
committed
Make UnsupportedForTarget error a diagnostic
1 parent 8d9a91b commit 8aff305

12 files changed

Lines changed: 239 additions & 47 deletions

File tree

src/transformation/utils/diagnostics.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ts from "typescript";
2+
import { LuaTarget } from "../../CompilerOptions";
23
import { AnnotationKind } from "./annotations";
34

45
const createDiagnosticFactory = <TArgs extends any[]>(
@@ -107,3 +108,9 @@ export const unsupportedAccessorInObjectLiteral = createDiagnosticFactory(
107108
export const unsupportedRightShiftOperator = createDiagnosticFactory(
108109
"Right shift operator is not supported. Use `>>>` instead."
109110
);
111+
112+
const getLuaTargetName = (version: LuaTarget) => (version === LuaTarget.LuaJIT ? "LuaJIT" : `Lua ${version}`);
113+
export const unsupportedForTarget = createDiagnosticFactory(
114+
(functionality: string, version: LuaTarget) =>
115+
`${functionality} is/are not supported for target ${getLuaTargetName(version)}.`
116+
);

src/transformation/utils/errors.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as ts from "typescript";
2-
import { LuaTarget } from "../../CompilerOptions";
32

43
export class TranspileError extends Error {
54
public name = "TranspileError";
@@ -8,8 +7,6 @@ export class TranspileError extends Error {
87
}
98
}
109

11-
const getLuaTargetName = (version: LuaTarget) => (version === LuaTarget.LuaJIT ? "LuaJIT" : `Lua ${version}`);
12-
1310
export const InvalidDecoratorContext = (node: ts.Node) =>
1411
new TranspileError(`Decorator function cannot have 'this: void'.`, node);
1512

@@ -24,9 +21,6 @@ export const UndefinedScope = () => new Error("Expected to pop a scope, but foun
2421
export const UnsupportedProperty = (parentName: string, property: string, node: ts.Node) =>
2522
new TranspileError(`Unsupported property on ${parentName}: ${property}`, node);
2623

27-
export const UnsupportedForTarget = (functionality: string, version: LuaTarget, node: ts.Node) =>
28-
new TranspileError(`${functionality} is/are not supported for target ${getLuaTargetName(version)}.`, node);
29-
3024
export const UnresolvableRequirePath = (node: ts.Node, reason: string, path?: string) =>
3125
new TranspileError(`${reason}. TypeScript path: ${path}.`, node);
3226

src/transformation/visitors/binary-expression/bit.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { LuaTarget } from "../../../CompilerOptions";
33
import * as lua from "../../../LuaAST";
44
import { assertNever } from "../../../utils";
55
import { TransformationContext } from "../../context";
6-
import { unsupportedRightShiftOperator } from "../../utils/diagnostics";
7-
import { UnsupportedForTarget } from "../../utils/errors";
6+
import { unsupportedForTarget, unsupportedRightShiftOperator } from "../../utils/diagnostics";
87

98
export type BitOperator = ts.ShiftOperator | ts.BitwiseOperator;
109
export const isBitOperator = (operator: ts.BinaryOperator): operator is BitOperator =>
@@ -64,14 +63,13 @@ export function transformBinaryBitOperation(
6463
): lua.Expression {
6564
switch (context.luaTarget) {
6665
case LuaTarget.Lua51:
67-
throw UnsupportedForTarget("Bitwise operations", LuaTarget.Lua51, node);
68-
69-
case LuaTarget.Lua52:
70-
return transformBinaryBitLibOperation(node, left, right, operator, "bit32");
66+
context.diagnostics.push(unsupportedForTarget(node, "Bitwise operations", LuaTarget.Lua51));
7167

7268
case LuaTarget.LuaJIT:
7369
return transformBinaryBitLibOperation(node, left, right, operator, "bit");
7470

71+
case LuaTarget.Lua52:
72+
return transformBinaryBitLibOperation(node, left, right, operator, "bit32");
7573
default:
7674
const luaOperator = transformBitOperatorToLuaOperator(context, node, operator);
7775
return lua.createBinaryExpression(left, right, luaOperator, node);
@@ -108,14 +106,14 @@ export function transformUnaryBitOperation(
108106
): lua.Expression {
109107
switch (context.luaTarget) {
110108
case LuaTarget.Lua51:
111-
throw UnsupportedForTarget("Bitwise operations", LuaTarget.Lua51, node);
112-
113-
case LuaTarget.Lua52:
114-
return transformUnaryBitLibOperation(node, expression, operator, "bit32");
109+
context.diagnostics.push(unsupportedForTarget(node, "Bitwise operations", LuaTarget.Lua51));
115110

116111
case LuaTarget.LuaJIT:
117112
return transformUnaryBitLibOperation(node, expression, operator, "bit");
118113

114+
case LuaTarget.Lua52:
115+
return transformUnaryBitLibOperation(node, expression, operator, "bit32");
116+
119117
default:
120118
return lua.createUnaryExpression(expression, operator, node);
121119
}

src/transformation/visitors/break-continue.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as ts from "typescript";
22
import { LuaTarget } from "../../CompilerOptions";
33
import * as lua from "../../LuaAST";
44
import { FunctionVisitor } from "../context";
5-
import { UndefinedScope, UnsupportedForTarget } from "../utils/errors";
5+
import { unsupportedForTarget } from "../utils/diagnostics";
6+
import { UndefinedScope } from "../utils/errors";
67
import { findScope, ScopeType } from "../utils/scope";
78

89
export const transformBreakStatement: FunctionVisitor<ts.BreakStatement> = (breakStatement, context) => {
@@ -20,7 +21,7 @@ export const transformBreakStatement: FunctionVisitor<ts.BreakStatement> = (brea
2021

2122
export const transformContinueStatement: FunctionVisitor<ts.ContinueStatement> = (statement, context) => {
2223
if (context.luaTarget === LuaTarget.Lua51) {
23-
throw UnsupportedForTarget("Continue statement", LuaTarget.Lua51, statement);
24+
context.diagnostics.push(unsupportedForTarget(statement, "Continue statement", LuaTarget.Lua51));
2425
}
2526

2627
const scope = findScope(context, ScopeType.Loop);

src/transformation/visitors/switch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import * as ts from "typescript";
22
import { LuaTarget } from "../../CompilerOptions";
33
import * as lua from "../../LuaAST";
44
import { FunctionVisitor } from "../context";
5-
import { UnsupportedForTarget } from "../utils/errors";
5+
import { unsupportedForTarget } from "../utils/diagnostics";
66
import { peekScope, performHoisting, popScope, pushScope, ScopeType } from "../utils/scope";
77

88
export const transformSwitchStatement: FunctionVisitor<ts.SwitchStatement> = (statement, context) => {
99
if (context.luaTarget === LuaTarget.Lua51) {
10-
throw UnsupportedForTarget("Switch statements", LuaTarget.Lua51, statement);
10+
context.diagnostics.push(unsupportedForTarget(statement, "Switch statements", LuaTarget.Lua51));
1111
}
1212

1313
pushScope(context, ScopeType.Switch);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`switch not allowed in 5.1: code 1`] = `
4+
"local ____exports = {}
5+
function ____exports.__main(self)
6+
local ____switch3 = \\"abc\\"
7+
goto ____switch3_end
8+
::____switch3_end::
9+
end
10+
return ____exports"
11+
`;
12+
13+
exports[`switch not allowed in 5.1: diagnostics 1`] = `"main.ts(2,9): error TSTL: Switch statements is/are not supported for target Lua 5.1."`;

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,128 @@ ____exports.__result = 10 - (4 + 5)
3636
return ____exports"
3737
`;
3838

39+
exports[`Bitop [5.1] ("~a"): code 1`] = `
40+
"local ____exports = {}
41+
____exports.__result = bit.bnot(a)
42+
return ____exports"
43+
`;
44+
45+
exports[`Bitop [5.1] ("~a"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
46+
47+
exports[`Bitop [5.1] ("a&=b"): code 1`] = `
48+
"local ____exports = {}
49+
____exports.__result = (function()
50+
a = bit.band(a, b)
51+
return a
52+
end)()
53+
return ____exports"
54+
`;
55+
56+
exports[`Bitop [5.1] ("a&=b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
57+
58+
exports[`Bitop [5.1] ("a&b"): code 1`] = `
59+
"local ____exports = {}
60+
____exports.__result = bit.band(a, b)
61+
return ____exports"
62+
`;
63+
64+
exports[`Bitop [5.1] ("a&b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
65+
66+
exports[`Bitop [5.1] ("a<<=b"): code 1`] = `
67+
"local ____exports = {}
68+
____exports.__result = (function()
69+
a = bit.lshift(a, b)
70+
return a
71+
end)()
72+
return ____exports"
73+
`;
74+
75+
exports[`Bitop [5.1] ("a<<=b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
76+
77+
exports[`Bitop [5.1] ("a<<b"): code 1`] = `
78+
"local ____exports = {}
79+
____exports.__result = bit.lshift(a, b)
80+
return ____exports"
81+
`;
82+
83+
exports[`Bitop [5.1] ("a<<b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
84+
85+
exports[`Bitop [5.1] ("a>>=b"): code 1`] = `
86+
"local ____exports = {}
87+
____exports.__result = (function()
88+
a = bit.arshift(a, b)
89+
return a
90+
end)()
91+
return ____exports"
92+
`;
93+
94+
exports[`Bitop [5.1] ("a>>=b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
95+
96+
exports[`Bitop [5.1] ("a>>>=b"): code 1`] = `
97+
"local ____exports = {}
98+
____exports.__result = (function()
99+
a = bit.rshift(a, b)
100+
return a
101+
end)()
102+
return ____exports"
103+
`;
104+
105+
exports[`Bitop [5.1] ("a>>>=b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
106+
107+
exports[`Bitop [5.1] ("a>>>b"): code 1`] = `
108+
"local ____exports = {}
109+
____exports.__result = bit.rshift(a, b)
110+
return ____exports"
111+
`;
112+
113+
exports[`Bitop [5.1] ("a>>>b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
114+
115+
exports[`Bitop [5.1] ("a>>b"): code 1`] = `
116+
"local ____exports = {}
117+
____exports.__result = bit.arshift(a, b)
118+
return ____exports"
119+
`;
120+
121+
exports[`Bitop [5.1] ("a>>b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
122+
123+
exports[`Bitop [5.1] ("a^=b"): code 1`] = `
124+
"local ____exports = {}
125+
____exports.__result = (function()
126+
a = bit.bxor(a, b)
127+
return a
128+
end)()
129+
return ____exports"
130+
`;
131+
132+
exports[`Bitop [5.1] ("a^=b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
133+
134+
exports[`Bitop [5.1] ("a^b"): code 1`] = `
135+
"local ____exports = {}
136+
____exports.__result = bit.bxor(a, b)
137+
return ____exports"
138+
`;
139+
140+
exports[`Bitop [5.1] ("a^b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
141+
142+
exports[`Bitop [5.1] ("a|=b"): code 1`] = `
143+
"local ____exports = {}
144+
____exports.__result = (function()
145+
a = bit.bor(a, b)
146+
return a
147+
end)()
148+
return ____exports"
149+
`;
150+
151+
exports[`Bitop [5.1] ("a|=b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
152+
153+
exports[`Bitop [5.1] ("a|b"): code 1`] = `
154+
"local ____exports = {}
155+
____exports.__result = bit.bor(a, b)
156+
return ____exports"
157+
`;
158+
159+
exports[`Bitop [5.1] ("a|b"): diagnostics 1`] = `"main.ts(1,25): error TSTL: Bitwise operations is/are not supported for target Lua 5.1."`;
160+
39161
exports[`Bitop [5.2] ("~a") 1`] = `
40162
"local ____exports = {}
41163
____exports.__result = bit32.bnot(a)

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,62 @@ return ____exports"
1111
`;
1212

1313
exports[`forin[Array]: diagnostics 1`] = `"main.ts(3,9): error TSTL: Iterating over arrays with 'for ... in' is not allowed."`;
14+
15+
exports[`loop continue (do { continue; } while (false)) [5.1]: code 1`] = `
16+
"repeat
17+
do
18+
do
19+
goto __continue2
20+
end
21+
::__continue2::
22+
end
23+
until not false"
24+
`;
25+
26+
exports[`loop continue (do { continue; } while (false)) [5.1]: diagnostics 1`] = `"main.ts(1,6): error TSTL: Continue statement is/are not supported for target Lua 5.1."`;
27+
28+
exports[`loop continue (for (;;) { continue; }) [5.1]: code 1`] = `
29+
"do
30+
while true do
31+
do
32+
goto __continue2
33+
end
34+
::__continue2::
35+
end
36+
end"
37+
`;
38+
39+
exports[`loop continue (for (;;) { continue; }) [5.1]: diagnostics 1`] = `"main.ts(1,12): error TSTL: Continue statement is/are not supported for target Lua 5.1."`;
40+
41+
exports[`loop continue (for (const a in {}) { continue; }) [5.1]: code 1`] = `
42+
"for a in pairs({}) do
43+
do
44+
goto __continue2
45+
end
46+
::__continue2::
47+
end"
48+
`;
49+
50+
exports[`loop continue (for (const a in {}) { continue; }) [5.1]: diagnostics 1`] = `"main.ts(1,23): error TSTL: Continue statement is/are not supported for target Lua 5.1."`;
51+
52+
exports[`loop continue (for (const a of []) { continue; }) [5.1]: code 1`] = `
53+
"for ____, a in ipairs({}) do
54+
do
55+
goto __continue2
56+
end
57+
::__continue2::
58+
end"
59+
`;
60+
61+
exports[`loop continue (for (const a of []) { continue; }) [5.1]: diagnostics 1`] = `"main.ts(1,23): error TSTL: Continue statement is/are not supported for target Lua 5.1."`;
62+
63+
exports[`loop continue (while (false) { continue; }) [5.1]: code 1`] = `
64+
"while false do
65+
do
66+
goto __continue2
67+
end
68+
::__continue2::
69+
end"
70+
`;
71+
72+
exports[`loop continue (while (false) { continue; }) [5.1]: diagnostics 1`] = `"main.ts(1,17): error TSTL: Continue statement is/are not supported for target Lua 5.1."`;

test/unit/conditionals.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as tstl from "../../src";
2-
import { UnsupportedForTarget } from "../../src/transformation/utils/errors";
32
import * as util from "../util";
43

54
test.each([0, 1])("if (%p)", inp => {
@@ -278,7 +277,7 @@ test("switch not allowed in 5.1", () => {
278277
switch ("abc") {}
279278
`
280279
.setOptions({ luaTarget: tstl.LuaTarget.Lua51 })
281-
.expectToHaveDiagnosticOfError(UnsupportedForTarget("Switch statements", tstl.LuaTarget.Lua51, util.nodeStub));
280+
.expectDiagnosticsToMatchSnapshot();
282281
});
283282

284283
test.each([

test/unit/expressions.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as tstl from "../../src";
2-
import { UnsupportedForTarget } from "../../src/transformation/utils/errors";
32
import * as util from "../util";
43

54
// TODO:
@@ -66,7 +65,7 @@ test.each(allBinaryOperators)("Bitop [5.1] (%p)", input => {
6665
util.testExpression(input)
6766
.setOptions({ luaTarget: tstl.LuaTarget.Lua51, luaLibImport: tstl.LuaLibImportKind.None })
6867
.disableSemanticCheck()
69-
.expectToHaveDiagnosticOfError(UnsupportedForTarget("Bitwise operations", tstl.LuaTarget.Lua51, util.nodeStub));
68+
.expectDiagnosticsToMatchSnapshot();
7069
});
7170

7271
test.each(allBinaryOperators)("Bitop [JIT] (%p)", input => {

0 commit comments

Comments
 (0)