Skip to content

Commit 0306f28

Browse files
committed
Make function assignment errors diagnostics
1 parent a08e239 commit 0306f28

File tree

9 files changed

+1866
-219
lines changed

9 files changed

+1866
-219
lines changed

src/transformation/builtins/function.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as lua from "../../LuaAST";
22
import { TransformationContext } from "../context";
3-
import { UnsupportedProperty, UnsupportedSelfFunctionConversion } from "../utils/errors";
3+
import { unsupportedSelfFunctionConversion } from "../utils/diagnostics";
4+
import { UnsupportedProperty } from "../utils/errors";
45
import { ContextType, getFunctionContextType } from "../utils/function-context";
56
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
67
import { PropertyCallExpression, transformArguments } from "../visitors/call";
@@ -12,7 +13,7 @@ export function transformFunctionPrototypeCall(
1213
const expression = node.expression;
1314
const callerType = context.checker.getTypeAtLocation(expression.expression);
1415
if (getFunctionContextType(context, callerType) === ContextType.Void) {
15-
throw UnsupportedSelfFunctionConversion(node);
16+
context.diagnostics.push(unsupportedSelfFunctionConversion(node));
1617
}
1718

1819
const signature = context.checker.getResolvedSignature(node);

src/transformation/utils/assignment-validation.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import * as ts from "typescript";
22
import { getOrUpdate } from "../../utils";
33
import { TransformationContext } from "../context";
44
import {
5-
UnsupportedNoSelfFunctionConversion,
6-
UnsupportedOverloadAssignment,
7-
UnsupportedSelfFunctionConversion,
8-
} from "./errors";
5+
unsupportedNoSelfFunctionConversion,
6+
unsupportedOverloadAssignment,
7+
unsupportedSelfFunctionConversion,
8+
} from "./diagnostics";
99
import { ContextType, getFunctionContextType } from "./function-context";
1010

1111
// TODO: Clear if types are reused between compilations
@@ -97,12 +97,12 @@ function validateFunctionAssignment(
9797
const toContext = getFunctionContextType(context, toType);
9898

9999
if (fromContext === ContextType.Mixed || toContext === ContextType.Mixed) {
100-
throw UnsupportedOverloadAssignment(node, toName);
100+
context.diagnostics.push(unsupportedOverloadAssignment(node, toName));
101101
} else if (fromContext !== toContext && fromContext !== ContextType.None && toContext !== ContextType.None) {
102102
if (toContext === ContextType.Void) {
103-
throw UnsupportedNoSelfFunctionConversion(node, toName);
103+
context.diagnostics.push(unsupportedNoSelfFunctionConversion(node, toName));
104104
} else {
105-
throw UnsupportedSelfFunctionConversion(node, toName);
105+
context.diagnostics.push(unsupportedSelfFunctionConversion(node, toName));
106106
}
107107
}
108108
}

src/transformation/utils/diagnostics.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ const createDiagnosticFactory = <TArgs extends any[]>(
1616

1717
export const forbiddenForIn = createDiagnosticFactory(`Iterating over arrays with 'for ... in' is not allowed.`);
1818

19+
export const unsupportedNoSelfFunctionConversion = createDiagnosticFactory((name?: string) => {
20+
const nameReference = name ? ` '${name}'` : "";
21+
return (
22+
`Unable to convert function with a 'this' parameter to function${nameReference} with no 'this'. ` +
23+
`To fix, wrap in an arrow function, or declare with 'this: void'.`
24+
);
25+
});
26+
27+
export const unsupportedSelfFunctionConversion = createDiagnosticFactory((name?: string) => {
28+
const nameReference = name ? ` '${name}'` : "";
29+
return (
30+
`Unable to convert function with no 'this' parameter to function${nameReference} with 'this'. ` +
31+
`To fix, wrap in an arrow function, or declare with 'this: any'.`
32+
);
33+
});
34+
35+
export const unsupportedOverloadAssignment = createDiagnosticFactory((name?: string) => {
36+
const nameReference = name ? ` to '${name}'` : "";
37+
return (
38+
`Unsupported assignment of function with different overloaded types for 'this'${nameReference}. ` +
39+
"Overloads should all have the same type for 'this'."
40+
);
41+
});
42+
1943
export const annotationInvalidArgumentCount = createDiagnosticFactory(
2044
(kind: AnnotationKind, got: number, expected: number) => `'@${kind}' expects ${expected} arguments, but got ${got}.`
2145
);

src/transformation/utils/errors.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,6 @@ export const UnsupportedProperty = (parentName: string, property: string, node:
3030
export const UnsupportedForTarget = (functionality: string, version: LuaTarget, node: ts.Node) =>
3131
new TranspileError(`${functionality} is/are not supported for target ${getLuaTargetName(version)}.`, node);
3232

33-
export const UnsupportedNoSelfFunctionConversion = (node: ts.Node, name?: string) => {
34-
const nameReference = name ? ` '${name}'` : "";
35-
return new TranspileError(
36-
`Unable to convert function with a 'this' parameter to function${nameReference} with no 'this'. ` +
37-
`To fix, wrap in an arrow function, or declare with 'this: void'.`,
38-
node
39-
);
40-
};
41-
42-
export const UnsupportedSelfFunctionConversion = (node: ts.Node, name?: string) => {
43-
const nameReference = name ? ` '${name}'` : "";
44-
return new TranspileError(
45-
`Unable to convert function with no 'this' parameter to function${nameReference} with 'this'. ` +
46-
`To fix, wrap in an arrow function or declare with 'this: any'.`,
47-
node
48-
);
49-
};
50-
51-
export const UnsupportedOverloadAssignment = (node: ts.Node, name?: string) => {
52-
const nameReference = name ? ` to '${name}'` : "";
53-
return new TranspileError(
54-
`Unsupported assignment of function with different overloaded types for 'this'${nameReference}. ` +
55-
"Overloads should all have the same type for 'this'.",
56-
node
57-
);
58-
};
59-
6033
export const UnresolvableRequirePath = (node: ts.Node, reason: string, path?: string) =>
6134
new TranspileError(`${reason}. TypeScript path: ${path}.`, node);
6235

0 commit comments

Comments
 (0)