Skip to content

Commit a4c9c9a

Browse files
committed
Merge remote-tracking branch 'upstream/master' into load-custom-transformers
2 parents 6da44e9 + 3d7833e commit a4c9c9a

17 files changed

+428
-138
lines changed

src/LuaKeywords.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const luaKeywords: Set<string> = new Set([
2+
"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto", "if", "in", "local", "nil",
3+
"not", "or", "repeat", "return", "then", "until", "while",
4+
]);
5+
6+
export const luaBuiltins: Set<string> = new Set([
7+
"_G", "assert", "coroutine", "debug", "error", "ipairs", "math", "pairs", "pcall", "print", "rawget", "rawset",
8+
"repeat", "require", "self", "string", "table", "tostring", "type", "unpack",
9+
]);

src/LuaPrinter.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as tstl from "./LuaAST";
66
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
77
import { LuaLib, LuaLibFeature } from "./LuaLib";
88
import { TSHelper as tsHelper } from "./TSHelper";
9+
import { luaKeywords } from "./LuaKeywords";
910

1011
type SourceChunk = string | SourceNode;
1112

@@ -559,7 +560,10 @@ export class LuaPrinter {
559560
const value = this.printExpression(expression.value);
560561

561562
if (expression.key) {
562-
if (tstl.isStringLiteral(expression.key) && tsHelper.isValidLuaIdentifier(expression.key.value)) {
563+
if (tstl.isStringLiteral(expression.key)
564+
&& tsHelper.isValidLuaIdentifier(expression.key.value)
565+
&& !luaKeywords.has(expression.key.value))
566+
{
563567
chunks.push(expression.key.value, " = ", value);
564568
} else {
565569
chunks.push("[", this.printExpression(expression.key), "] = ", value);
@@ -653,7 +657,10 @@ export class LuaPrinter {
653657
const chunks: SourceChunk[] = [];
654658

655659
chunks.push(this.printExpression(expression.table));
656-
if (tstl.isStringLiteral(expression.index) && tsHelper.isValidLuaIdentifier(expression.index.value)) {
660+
if (tstl.isStringLiteral(expression.index)
661+
&& tsHelper.isValidLuaIdentifier(expression.index.value)
662+
&& !luaKeywords.has(expression.index.value))
663+
{
657664
chunks.push(".", this.createSourceNode(expression.index, expression.index.value));
658665
} else {
659666
chunks.push("[", this.printExpression(expression.index), "]");

src/LuaTransformer.ts

Lines changed: 132 additions & 80 deletions
Large diffs are not rendered by default.

src/TSTLErrors.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,12 @@ export class TSTLErrors {
189189
"must be moved before the identifier's use, or hoisting must be enabled.",
190190
node
191191
);
192-
}
192+
};
193+
194+
public static InvalidAmbientLuaKeywordIdentifier = (node: ts.Identifier) => {
195+
return new TranspileError(
196+
`Invalid use of lua keyword "${node.text}" as ambient identifier name.`,
197+
node
198+
);
199+
};
193200
}

test/translation/__snapshots__/transformation.spec.ts.snap

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -383,42 +383,57 @@ exports[`Transformation (modulesFunctionNoExport) 1`] = `
383383
end"
384384
`;
385385

386-
exports[`Transformation (modulesImportAll) 1`] = `"local Test = require(\\"test\\")"`;
386+
exports[`Transformation (modulesImportAll) 1`] = `
387+
"local Test = require(\\"test\\")
388+
local ____ = Test"
389+
`;
387390

388391
exports[`Transformation (modulesImportNamed) 1`] = `
389392
"local __TSTL_test = require(\\"test\\")
390-
local TestClass = __TSTL_test.TestClass"
393+
local TestClass = __TSTL_test.TestClass
394+
local ____ = TestClass"
391395
`;
392396

393397
exports[`Transformation (modulesImportNamedSpecialChars) 1`] = `
394398
"local __TSTL_kebab_2Dmodule = require(\\"kebab-module\\")
395-
local TestClass = __TSTL_kebab_2Dmodule.TestClass
399+
local TestClass1 = __TSTL_kebab_2Dmodule.TestClass1
396400
local __TSTL_dollar_24module = require(\\"dollar$module\\")
397-
local TestClass = __TSTL_dollar_24module.TestClass
401+
local TestClass2 = __TSTL_dollar_24module.TestClass2
398402
local __TSTL_singlequote_27module = require(\\"singlequote'module\\")
399-
local TestClass = __TSTL_singlequote_27module.TestClass
403+
local TestClass3 = __TSTL_singlequote_27module.TestClass3
400404
local __TSTL_hash_23module = require(\\"hash#module\\")
401-
local TestClass = __TSTL_hash_23module.TestClass
405+
local TestClass4 = __TSTL_hash_23module.TestClass4
402406
local __TSTL_space_20module = require(\\"space module\\")
403-
local TestClass = __TSTL_space_20module.TestClass"
407+
local TestClass5 = __TSTL_space_20module.TestClass5
408+
local ____ = TestClass1
409+
local ____ = TestClass2
410+
local ____ = TestClass3
411+
local ____ = TestClass4
412+
local ____ = TestClass5"
404413
`;
405414

406415
exports[`Transformation (modulesImportRenamed) 1`] = `
407416
"local __TSTL_test = require(\\"test\\")
408-
local RenamedClass = __TSTL_test.TestClass"
417+
local RenamedClass = __TSTL_test.TestClass
418+
local ____ = RenamedClass"
409419
`;
410420

411421
exports[`Transformation (modulesImportRenamedSpecialChars) 1`] = `
412422
"local __TSTL_kebab_2Dmodule = require(\\"kebab-module\\")
413-
local RenamedClass = __TSTL_kebab_2Dmodule.TestClass
423+
local RenamedClass1 = __TSTL_kebab_2Dmodule.TestClass
414424
local __TSTL_dollar_24module = require(\\"dollar$module\\")
415-
local RenamedClass = __TSTL_dollar_24module.TestClass
425+
local RenamedClass2 = __TSTL_dollar_24module.TestClass
416426
local __TSTL_singlequote_27module = require(\\"singlequote'module\\")
417-
local RenamedClass = __TSTL_singlequote_27module.TestClass
427+
local RenamedClass3 = __TSTL_singlequote_27module.TestClass
418428
local __TSTL_hash_23module = require(\\"hash#module\\")
419-
local RenamedClass = __TSTL_hash_23module.TestClass
429+
local RenamedClass4 = __TSTL_hash_23module.TestClass
420430
local __TSTL_space_20module = require(\\"space module\\")
421-
local RenamedClass = __TSTL_space_20module.TestClass"
431+
local RenamedClass5 = __TSTL_space_20module.TestClass
432+
local ____ = RenamedClass1
433+
local ____ = RenamedClass2
434+
local ____ = RenamedClass3
435+
local ____ = RenamedClass4
436+
local ____ = RenamedClass5"
422437
`;
423438

424439
exports[`Transformation (modulesImportWithoutFromClause) 1`] = `"require(\\"test\\")"`;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
import * as Test from "test";
2+
3+
Test;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
import { TestClass } from "test";
2+
3+
TestClass;
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { TestClass } from "kebab-module";
2-
import { TestClass } from "dollar$module";
3-
import { TestClass } from "singlequote'module";
4-
import { TestClass } from "hash#module";
5-
import { TestClass } from "space module";
1+
import { TestClass1 } from "kebab-module";
2+
import { TestClass2 } from "dollar$module";
3+
import { TestClass3 } from "singlequote'module";
4+
import { TestClass4 } from "hash#module";
5+
import { TestClass5 } from "space module";
6+
7+
TestClass1;
8+
TestClass2;
9+
TestClass3;
10+
TestClass4;
11+
TestClass5;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
import { TestClass as RenamedClass } from "test";
2+
3+
RenamedClass;
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { TestClass as RenamedClass } from "kebab-module";
2-
import { TestClass as RenamedClass } from "dollar$module";
3-
import { TestClass as RenamedClass } from "singlequote'module";
4-
import { TestClass as RenamedClass } from "hash#module";
5-
import { TestClass as RenamedClass } from "space module";
1+
import { TestClass as RenamedClass1 } from "kebab-module";
2+
import { TestClass as RenamedClass2 } from "dollar$module";
3+
import { TestClass as RenamedClass3 } from "singlequote'module";
4+
import { TestClass as RenamedClass4 } from "hash#module";
5+
import { TestClass as RenamedClass5 } from "space module";
6+
7+
RenamedClass1;
8+
RenamedClass2;
9+
RenamedClass3;
10+
RenamedClass4;
11+
RenamedClass5;

0 commit comments

Comments
 (0)