Skip to content

Commit e533ed7

Browse files
committed
Improve some diagnostic code spans
1 parent e23b1d6 commit e533ed7

File tree

20 files changed

+129
-35
lines changed

20 files changed

+129
-35
lines changed

src/transformation/builtins/array.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function transformArrayPrototypeCall(
7979
case "flatMap":
8080
return transformLuaLibFunction(context, LuaLibFeature.ArrayFlatMap, node, caller, ...params);
8181
default:
82-
context.diagnostics.push(unsupportedProperty(node, "array", expressionName));
82+
context.diagnostics.push(unsupportedProperty(expression.name, "array", expressionName));
8383
}
8484
}
8585

src/transformation/builtins/console.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ export function transformConsoleCall(
6161
);
6262
return lua.createCallExpression(lua.createIdentifier("print"), [debugTracebackCall]);
6363
default:
64-
context.diagnostics.push(unsupportedProperty(expression, "console", methodName));
64+
context.diagnostics.push(unsupportedProperty(method.name, "console", methodName));
6565
}
6666
}

src/transformation/builtins/function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ export function transformFunctionPrototypeCall(
2727
case "call":
2828
return transformLuaLibFunction(context, LuaLibFeature.FunctionCall, node, caller, ...params);
2929
default:
30-
context.diagnostics.push(unsupportedProperty(node, "function", expressionName));
30+
context.diagnostics.push(unsupportedProperty(expression.name, "function", expressionName));
3131
}
3232
}

src/transformation/builtins/math.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function transformMathProperty(
2626
return lua.createNumericLiteral(Math[name], node);
2727

2828
default:
29-
context.diagnostics.push(unsupportedProperty(node, "Math", name));
29+
context.diagnostics.push(unsupportedProperty(node.name, "Math", name));
3030
}
3131
}
3232

@@ -99,6 +99,6 @@ export function transformMathCall(
9999
}
100100

101101
default:
102-
context.diagnostics.push(unsupportedProperty(expression, "Math", expressionName));
102+
context.diagnostics.push(unsupportedProperty(expression.name, "Math", expressionName));
103103
}
104104
}

src/transformation/builtins/number.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function transformNumberPrototypeCall(
2020
? lua.createCallExpression(lua.createIdentifier("tostring"), [caller], node)
2121
: transformLuaLibFunction(context, LuaLibFeature.NumberToString, node, caller, ...params);
2222
default:
23-
context.diagnostics.push(unsupportedProperty(node, "number", expressionName));
23+
context.diagnostics.push(unsupportedProperty(expression.name, "number", expressionName));
2424
}
2525
}
2626

@@ -37,6 +37,6 @@ export function transformNumberConstructorCall(
3737
case "isFinite":
3838
return transformLuaLibFunction(context, LuaLibFeature.NumberIsFinite, expression, ...parameters);
3939
default:
40-
context.diagnostics.push(unsupportedProperty(expression, "Number", methodName));
40+
context.diagnostics.push(unsupportedProperty(method.name, "Number", methodName));
4141
}
4242
}

src/transformation/builtins/object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function transformObjectConstructorCall(
2424
case "values":
2525
return transformLuaLibFunction(context, LuaLibFeature.ObjectValues, expression, ...parameters);
2626
default:
27-
context.diagnostics.push(unsupportedProperty(expression, "Object", methodName));
27+
context.diagnostics.push(unsupportedProperty(method.name, "Object", methodName));
2828
}
2929
}
3030

src/transformation/builtins/string.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function transformStringPrototypeCall(
110110
case "padEnd":
111111
return transformLuaLibFunction(context, LuaLibFeature.StringPadEnd, node, caller, ...params);
112112
default:
113-
context.diagnostics.push(unsupportedProperty(node, "string", expressionName));
113+
context.diagnostics.push(unsupportedProperty(expression.name, "string", expressionName));
114114
}
115115
}
116116

@@ -132,7 +132,7 @@ export function transformStringConstructorCall(
132132
);
133133

134134
default:
135-
context.diagnostics.push(unsupportedProperty(node, "String", expressionName));
135+
context.diagnostics.push(unsupportedProperty(expression.name, "String", expressionName));
136136
}
137137
}
138138

@@ -145,6 +145,6 @@ export function transformStringProperty(
145145
const expression = context.transformExpression(node.expression);
146146
return lua.createUnaryExpression(expression, lua.SyntaxKind.LengthOperator, node);
147147
default:
148-
context.diagnostics.push(unsupportedProperty(node, "string", node.name.text));
148+
context.diagnostics.push(unsupportedProperty(node.name, "string", node.name.text));
149149
}
150150
}

src/transformation/builtins/symbol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ export function transformSymbolConstructorCall(
2020
const functionIdentifier = lua.createIdentifier(`__TS__SymbolRegistry${upperMethodName}`);
2121
return lua.createCallExpression(functionIdentifier, parameters, expression);
2222
default:
23-
context.diagnostics.push(unsupportedProperty(expression, "Symbol", methodName));
23+
context.diagnostics.push(unsupportedProperty(method.name, "Symbol", methodName));
2424
}
2525
}

src/transformation/utils/diagnostics.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ export const luaIteratorForbiddenUsage = createDiagnosticFactory(
9797
"the '@tupleReturn' annotation."
9898
);
9999

100-
export const unsupportedNullishCoalescing = createDiagnosticFactory("Nullish coalescing is not supported.");
101-
102100
export const unsupportedAccessorInObjectLiteral = createDiagnosticFactory(
103101
"Accessors in object literal are not supported."
104102
);
105103

104+
export const unsupportedNullishCoalescing = createDiagnosticFactory("Nullish coalescing is not supported.");
105+
106106
export const unsupportedRightShiftOperator = createDiagnosticFactory(
107107
"Right shift operator is not supported. Use `>>>` instead."
108108
);
@@ -117,10 +117,6 @@ export const unsupportedProperty = createDiagnosticFactory(
117117
(parentName: string, property: string) => `${parentName}.${property} is unsupported.`
118118
);
119119

120-
export const forOfUnsupportedObjectDestructuring = createDiagnosticFactory(
121-
`Unsupported object destructuring in for...of statement.`
122-
);
123-
124120
export const invalidAmbientIdentifierName = createDiagnosticFactory(
125121
(text: string) => `Invalid ambient identifier name '${text}'. Ambient identifiers must be valid lua identifiers.`
126122
);

src/transformation/utils/safe-names.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ const luaBuiltins: ReadonlySet<string> = new Set([
5555

5656
export const isUnsafeName = (name: string) => !isValidLuaIdentifier(name) || luaBuiltins.has(name);
5757

58+
function checkName(context: TransformationContext, name: string, node: ts.Node): boolean {
59+
const isInvalid = !isValidLuaIdentifier(name);
60+
61+
if (isInvalid) {
62+
// Empty identifier is a TypeScript error
63+
if (name !== "") {
64+
context.diagnostics.push(invalidAmbientIdentifierName(node, name));
65+
}
66+
}
67+
68+
return isInvalid;
69+
}
70+
5871
export function hasUnsafeSymbolName(
5972
context: TransformationContext,
6073
symbol: ts.Symbol,
@@ -63,8 +76,7 @@ export function hasUnsafeSymbolName(
6376
const isAmbient = symbol.declarations && symbol.declarations.some(d => isAmbientNode(d));
6477

6578
// Catch ambient declarations of identifiers with bad names
66-
if (!isValidLuaIdentifier(symbol.name) && isAmbient) {
67-
context.diagnostics.push(invalidAmbientIdentifierName(tsOriginal, symbol.name));
79+
if (isAmbient && checkName(context, symbol.name, tsOriginal)) {
6880
return true;
6981
}
7082

@@ -84,12 +96,7 @@ export function hasUnsafeIdentifierName(
8496
}
8597
}
8698

87-
if (!isValidLuaIdentifier(identifier.text)) {
88-
context.diagnostics.push(invalidAmbientIdentifierName(identifier, identifier.text));
89-
return true;
90-
}
91-
92-
return false;
99+
return checkName(context, identifier.text, identifier);
93100
}
94101

95102
const fixInvalidLuaIdentifier = (name: string) =>

0 commit comments

Comments
 (0)