forked from MCJack123/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostics.ts
More file actions
170 lines (134 loc) · 7.55 KB
/
Copy pathdiagnostics.ts
File metadata and controls
170 lines (134 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import * as ts from "typescript";
import { LuaTarget, TypeScriptToLuaOptions } from "../../CompilerOptions";
import { createSerialDiagnosticFactory } from "../../utils";
import { AnnotationKind } from "./annotations";
type MessageProvider<TArgs extends any[]> = string | ((...args: TArgs) => string);
const createDiagnosticFactory = <TArgs extends any[]>(
category: ts.DiagnosticCategory,
message: MessageProvider<TArgs>
) =>
createSerialDiagnosticFactory((node: ts.Node, ...args: TArgs) => ({
file: ts.getOriginalNode(node).getSourceFile(),
start: ts.getOriginalNode(node).getStart(),
length: ts.getOriginalNode(node).getWidth(),
messageText: typeof message === "string" ? message : message(...args),
category,
}));
const createErrorDiagnosticFactory = <TArgs extends any[]>(message: MessageProvider<TArgs>) =>
createDiagnosticFactory(ts.DiagnosticCategory.Error, message);
const createWarningDiagnosticFactory = <TArgs extends any[]>(message: MessageProvider<TArgs>) =>
createDiagnosticFactory(ts.DiagnosticCategory.Warning, message);
export const unsupportedNodeKind = createErrorDiagnosticFactory(
(kind: ts.SyntaxKind) => `Unsupported node kind ${ts.SyntaxKind[kind]}`
);
export const forbiddenForIn = createErrorDiagnosticFactory("Iterating over arrays with 'for ... in' is not allowed.");
export const unsupportedNoSelfFunctionConversion = createErrorDiagnosticFactory((name?: string) => {
const nameReference = name ? ` '${name}'` : "";
return (
`Unable to convert function with a 'this' parameter to function${nameReference} with no 'this'. ` +
"To fix, wrap in an arrow function, or declare with 'this: void'."
);
});
export const unsupportedSelfFunctionConversion = createErrorDiagnosticFactory((name?: string) => {
const nameReference = name ? ` '${name}'` : "";
return (
`Unable to convert function with no 'this' parameter to function${nameReference} with 'this'. ` +
"To fix, wrap in an arrow function, or declare with 'this: any'."
);
});
export const unsupportedOverloadAssignment = createErrorDiagnosticFactory((name?: string) => {
const nameReference = name ? ` to '${name}'` : "";
return (
`Unsupported assignment of function with different overloaded types for 'this'${nameReference}. ` +
"Overloads should all have the same type for 'this'."
);
});
export const decoratorInvalidContext = createErrorDiagnosticFactory("Decorator function cannot have 'this: void'.");
export const annotationInvalidArgumentCount = createErrorDiagnosticFactory(
(kind: AnnotationKind, got: number, expected: number) => `'@${kind}' expects ${expected} arguments, but got ${got}.`
);
export const invalidRangeUse = createErrorDiagnosticFactory("$range can only be used in a for...of loop.");
export const invalidVarargUse = createErrorDiagnosticFactory(
"$vararg can only be used in a spread element ('...$vararg') in global scope."
);
export const invalidRangeControlVariable = createErrorDiagnosticFactory(
"For loop using $range must declare a single control variable."
);
export const invalidMultiIterableWithoutDestructuring = createErrorDiagnosticFactory(
"LuaIterable with a LuaMultiReturn return value type must be destructured."
);
export const invalidPairsIterableWithoutDestructuring = createErrorDiagnosticFactory(
"LuaPairsIterable type must be destructured in a for...of statement."
);
export const unsupportedAccessorInObjectLiteral = createErrorDiagnosticFactory(
"Accessors in object literal are not supported."
);
export const unsupportedRightShiftOperator = createErrorDiagnosticFactory(
"Right shift operator is not supported for target Lua 5.3. Use `>>>` instead."
);
const getLuaTargetName = (version: LuaTarget) => (version === LuaTarget.LuaJIT ? "LuaJIT" : (version === LuaTarget.Cobalt ? "Cobalt" : `Lua ${version}`));
export const unsupportedForTarget = createErrorDiagnosticFactory(
(functionality: string, version: LuaTarget) =>
`${functionality} is/are not supported for target ${getLuaTargetName(version)}.`
);
export const unsupportedForTargetButOverrideAvailable = createErrorDiagnosticFactory(
(functionality: string, version: LuaTarget, optionName: keyof TypeScriptToLuaOptions) =>
`As a precaution, ${functionality} is/are not supported for target ${getLuaTargetName(
version
)} due to language features/limitations. ` +
`However "--${optionName}" can be used to bypass this precaution. ` +
"See https://typescripttolua.github.io/docs/configuration for more information."
);
export const unsupportedProperty = createErrorDiagnosticFactory(
(parentName: string, property: string) => `${parentName}.${property} is unsupported.`
);
export const invalidAmbientIdentifierName = createErrorDiagnosticFactory(
(text: string) => `Invalid ambient identifier name '${text}'. Ambient identifiers must be valid lua identifiers.`
);
export const unsupportedVarDeclaration = createErrorDiagnosticFactory(
"`var` declarations are not supported. Use `let` or `const` instead."
);
export const invalidMultiFunctionUse = createErrorDiagnosticFactory(
"The $multi function must be called in a return statement."
);
export const invalidMultiFunctionReturnType = createErrorDiagnosticFactory(
"The $multi function cannot be cast to a non-LuaMultiReturn type."
);
export const invalidMultiTypeToNonArrayLiteral = createErrorDiagnosticFactory("Expected an array literal.");
export const invalidMultiTypeToEmptyPatternOrArrayLiteral = createErrorDiagnosticFactory(
"There must be one or more elements specified here."
);
export const invalidMultiReturnAccess = createErrorDiagnosticFactory(
"The LuaMultiReturn type can only be accessed via an element access expression of a numeric type."
);
export const invalidCallExtensionUse = createErrorDiagnosticFactory(
"This function must be called directly and cannot be referred to."
);
export const annotationRemoved = createErrorDiagnosticFactory(
(kind: AnnotationKind) =>
`'@${kind}' has been removed and will no longer have any effect.` +
`See https://typescripttolua.github.io/docs/advanced/compiler-annotations#${kind.toLowerCase()} for more information.`
);
export const annotationDeprecated = createWarningDiagnosticFactory(
(kind: AnnotationKind) =>
`'@${kind}' is deprecated and will be removed in a future update. Please update your code before upgrading to the next release, otherwise your project will no longer compile. ` +
`See https://typescripttolua.github.io/docs/advanced/compiler-annotations#${kind.toLowerCase()} for more information.`
);
export const truthyOnlyConditionalValue = createWarningDiagnosticFactory(
"Only false and nil evaluate to 'false' in Lua, everything else is considered 'true'. Explicitly compare the value with ===."
);
export const notAllowedOptionalAssignment = createErrorDiagnosticFactory(
"The left-hand side of an assignment expression may not be an optional property access."
);
export const awaitMustBeInAsyncFunction = createErrorDiagnosticFactory(
"Await can only be used inside async functions."
);
export const unsupportedBuiltinOptionalCall = createErrorDiagnosticFactory(
"Optional calls are not supported for builtin or language extension functions."
);
export const unsupportedOptionalCompileMembersOnly = createErrorDiagnosticFactory(
"Optional calls are not supported on enums marked with @compileMembersOnly."
);
export const undefinedInArrayLiteral = createErrorDiagnosticFactory(
"Array literals may not contain undefined or null."
);