Skip to content

Commit 2af391e

Browse files
committed
Make function assignment errors diagnostics
1 parent a08e239 commit 2af391e

File tree

8 files changed

+1863
-217
lines changed

8 files changed

+1863
-217
lines changed

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

test/unit/functions/validation/__snapshots__/invalidFunctionAssignments.spec.ts.snap

Lines changed: 1723 additions & 0 deletions
Large diffs are not rendered by default.

test/unit/functions/validation/functionPermutations.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,7 @@ export const anonTestFunctionType = "(s: string) => string";
348348
export const selfTestFunctionType = "(this: any, s: string) => string";
349349
export const noSelfTestFunctionType = "(this: void, s: string) => string";
350350

351-
type TestFunctionCast = [
352-
/*testFunction: */ TestFunction,
353-
/*castedFunction: */ string,
354-
/*isSelfConversion?: */ boolean?
355-
];
351+
type TestFunctionCast = [TestFunction, string];
356352
export const validTestFunctionCasts: TestFunctionCast[] = [
357353
[selfTestFunctions[0], `<${anonTestFunctionType}>(${selfTestFunctions[0].value})`],
358354
[selfTestFunctions[0], `(${selfTestFunctions[0].value}) as (${anonTestFunctionType})`],
@@ -366,21 +362,17 @@ export const validTestFunctionCasts: TestFunctionCast[] = [
366362
[noSelfInFileTestFunctions[0], `(${noSelfInFileTestFunctions[0].value}) as (${noSelfTestFunctionType})`],
367363
];
368364
export const invalidTestFunctionCasts: TestFunctionCast[] = [
369-
[noSelfTestFunctions[0], `<${anonTestFunctionType}>(${noSelfTestFunctions[0].value})`, false],
370-
[noSelfTestFunctions[0], `(${noSelfTestFunctions[0].value}) as (${anonTestFunctionType})`, false],
371-
[noSelfTestFunctions[0], `<${selfTestFunctionType}>(${noSelfTestFunctions[0].value})`, false],
372-
[noSelfTestFunctions[0], `(${noSelfTestFunctions[0].value}) as (${selfTestFunctionType})`, false],
373-
[noSelfInFileTestFunctions[0], `<${selfTestFunctionType}>(${noSelfInFileTestFunctions[0].value})`, false],
374-
[noSelfInFileTestFunctions[0], `(${noSelfInFileTestFunctions[0].value}) as (${selfTestFunctionType})`, false],
375-
[selfTestFunctions[0], `<${noSelfTestFunctionType}>(${selfTestFunctions[0].value})`, true],
376-
[selfTestFunctions[0], `(${selfTestFunctions[0].value}) as (${noSelfTestFunctionType})`, true],
365+
[noSelfTestFunctions[0], `<${anonTestFunctionType}>(${noSelfTestFunctions[0].value})`],
366+
[noSelfTestFunctions[0], `(${noSelfTestFunctions[0].value}) as (${anonTestFunctionType})`],
367+
[noSelfTestFunctions[0], `<${selfTestFunctionType}>(${noSelfTestFunctions[0].value})`],
368+
[noSelfTestFunctions[0], `(${noSelfTestFunctions[0].value}) as (${selfTestFunctionType})`],
369+
[noSelfInFileTestFunctions[0], `<${selfTestFunctionType}>(${noSelfInFileTestFunctions[0].value})`],
370+
[noSelfInFileTestFunctions[0], `(${noSelfInFileTestFunctions[0].value}) as (${selfTestFunctionType})`],
371+
[selfTestFunctions[0], `<${noSelfTestFunctionType}>(${selfTestFunctions[0].value})`],
372+
[selfTestFunctions[0], `(${selfTestFunctions[0].value}) as (${noSelfTestFunctionType})`],
377373
];
378374

379-
export type TestFunctionAssignment = [
380-
/*testFunction: */ TestFunction,
381-
/*functionType: */ string,
382-
/*isSelfConversion?: */ boolean?
383-
];
375+
export type TestFunctionAssignment = [TestFunction, string];
384376
export const validTestFunctionAssignments: TestFunctionAssignment[] = [
385377
...selfTestFunctions.map((f): TestFunctionAssignment => [f, anonTestFunctionType]),
386378
...selfTestFunctions.map((f): TestFunctionAssignment => [f, selfTestFunctionType]),
@@ -395,11 +387,11 @@ export const validTestFunctionAssignments: TestFunctionAssignment[] = [
395387
...noSelfTestFunctionExpressions.map((f): TestFunctionAssignment => [f, noSelfTestFunctionType]),
396388
];
397389
export const invalidTestFunctionAssignments: TestFunctionAssignment[] = [
398-
...selfTestFunctions.map((f): TestFunctionAssignment => [f, noSelfTestFunctionType, false]),
399-
...noSelfTestFunctions.map((f): TestFunctionAssignment => [f, anonTestFunctionType, true]),
400-
...noSelfTestFunctions.map((f): TestFunctionAssignment => [f, selfTestFunctionType, true]),
401-
...noSelfInFileTestFunctions.map((f): TestFunctionAssignment => [f, selfTestFunctionType, true]),
402-
...selfTestFunctionExpressions.map((f): TestFunctionAssignment => [f, noSelfTestFunctionType, false]),
403-
...noSelfTestFunctionExpressions.map((f): TestFunctionAssignment => [f, anonTestFunctionType, true]),
404-
...noSelfTestFunctionExpressions.map((f): TestFunctionAssignment => [f, selfTestFunctionType, true]),
390+
...selfTestFunctions.map((f): TestFunctionAssignment => [f, noSelfTestFunctionType]),
391+
...noSelfTestFunctions.map((f): TestFunctionAssignment => [f, anonTestFunctionType]),
392+
...noSelfTestFunctions.map((f): TestFunctionAssignment => [f, selfTestFunctionType]),
393+
...noSelfInFileTestFunctions.map((f): TestFunctionAssignment => [f, selfTestFunctionType]),
394+
...selfTestFunctionExpressions.map((f): TestFunctionAssignment => [f, noSelfTestFunctionType]),
395+
...noSelfTestFunctionExpressions.map((f): TestFunctionAssignment => [f, anonTestFunctionType]),
396+
...noSelfTestFunctionExpressions.map((f): TestFunctionAssignment => [f, selfTestFunctionType]),
405397
];

0 commit comments

Comments
 (0)