Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/transformation/utils/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,7 @@ export const unsupportedBuiltinOptionalCall = createErrorDiagnosticFactory(
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."
);
34 changes: 33 additions & 1 deletion src/transformation/visitors/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { assertNever } from "../../utils";
import { FunctionVisitor, TransformationContext, Visitors } from "../context";
import { invalidMultiFunctionUse, unsupportedAccessorInObjectLiteral } from "../utils/diagnostics";
import {
invalidMultiFunctionUse,
undefinedInArrayLiteral,
unsupportedAccessorInObjectLiteral,
} from "../utils/diagnostics";
import { createExportedIdentifier, getSymbolExportScope } from "../utils/export";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { createSafeName, hasUnsafeIdentifierName, hasUnsafeSymbolName } from "../utils/safe-names";
Expand Down Expand Up @@ -196,6 +200,9 @@ const transformObjectLiteralExpression: FunctionVisitor<ts.ObjectLiteralExpressi
};

const transformArrayLiteralExpression: FunctionVisitor<ts.ArrayLiteralExpression> = (expression, context) => {
// Disallow using undefined/null in array literals
checkForUndefinedOrNullInArrayLiteral(expression, context);

const filteredElements = expression.elements.map(e =>
ts.isOmittedExpression(e) ? ts.factory.createIdentifier("undefined") : e
);
Expand All @@ -204,6 +211,31 @@ const transformArrayLiteralExpression: FunctionVisitor<ts.ArrayLiteralExpression
return lua.createTableExpression(values, expression);
};

function checkForUndefinedOrNullInArrayLiteral(array: ts.ArrayLiteralExpression, context: TransformationContext) {
// Look for last non-nil element in literal
let lastNonUndefinedIndex = array.elements.length - 1;
for (; lastNonUndefinedIndex >= 0; lastNonUndefinedIndex--) {
if (!isUndefinedOrNull(array.elements[lastNonUndefinedIndex])) {
break;
}
}

// Add diagnostics for non-trailing nil elements in array literal
for (let i = 0; i < array.elements.length; i++) {
if (i < lastNonUndefinedIndex && isUndefinedOrNull(array.elements[i])) {
context.diagnostics.push(undefinedInArrayLiteral(array.elements[i]));
}
}
}

function isUndefinedOrNull(node: ts.Node) {
return (
node.kind === ts.SyntaxKind.UndefinedKeyword ||
node.kind === ts.SyntaxKind.NullKeyword ||
(ts.isIdentifier(node) && node.text === "undefined")
);
}

export const literalVisitors: Visitors = {
[ts.SyntaxKind.NullKeyword]: node => lua.createNilLiteral(node),
[ts.SyntaxKind.TrueKeyword]: node => lua.createBooleanLiteral(true, node),
Expand Down
26 changes: 26 additions & 0 deletions test/unit/builtins/array.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { undefinedInArrayLiteral } from "../../../src/transformation/utils/diagnostics";
import * as util from "../../util";

test("omitted expression", () => {
Expand Down Expand Up @@ -674,3 +675,28 @@ test("Array.isArray returns true for empty objects", () => {
test("array.prototype.concat issue #738", () => {
util.testExpression`([] as any[]).concat(13, 323, {x: 3}, [2, 3])`.expectToMatchJsResult();
});

test.each(["[1, undefined, 3]", "[1, null, 3]", "[1, undefined, 2, null]"])(
"not allowed to use null or undefined in array literals (%p)",
literal => {
util.testExpression(literal).expectToHaveDiagnostics([undefinedInArrayLiteral.code]);
}
);

test("not allowed to use null or undefined in array literals ([undefined, null, 1])", () => {
util.testExpression`[undefined, null, 1]`.expectToHaveDiagnostics([
undefinedInArrayLiteral.code,
undefinedInArrayLiteral.code,
]);
});

test.each([
"[]",
"[1, 2, undefined]",
"[1, 2, null]",
"[1, undefined, undefined, null]",
"[undefined]",
"[undefined, null]",
])("trailing undefined or null are allowed in array literal (%p)", literal => {
util.testExpression(literal).expectToHaveNoDiagnostics();
});