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
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"pretest": "npm run lint && ts-node --transpile-only ./build_lualib.ts",
"test": "jest",
"lint": "npm run lint:tslint && npm run lint:prettier",
"lint:prettier": "prettier --check **/*.{js,ts,yml,json,md} || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",
"lint:prettier": "prettier --check \"**/*.{js,ts,yml,json,md}\" || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",
"lint:tslint": "tslint -p . && tslint -p test && tslint -p src/lualib",
"fix:prettier": "prettier --check --write **/*.{js,ts,yml,json,md}",
"fix:prettier": "prettier --check --write \"**/*.{js,ts,yml,json,md}\"",
"release-major": "npm version major",
"release-minor": "npm version minor",
"release-patch": "npm version patch",
Expand Down
18 changes: 9 additions & 9 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4876,14 +4876,6 @@ export class LuaTransformer {
}

public transformIdentifier(identifier: ts.Identifier): tstl.Identifier {
if (identifier.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
// TODO this is a hack that allows use to keep Identifier
// as return time as changing that would break a lot of stuff.
// But this should be changed to return tstl.createNilLiteral()
// at some point.
return tstl.createIdentifier("nil");
}

if (tsHelper.isForRangeType(identifier, this.checker)) {
const callExpression = tsHelper.findFirstNodeAbove(identifier, ts.isCallExpression);
if (!callExpression || !callExpression.parent || !ts.isForOfStatement(callExpression.parent)) {
Expand All @@ -4910,6 +4902,10 @@ export class LuaTransformer {
return this.createExportedIdentifier(identifier, exportScope);
}

if (expression.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
return tstl.createNilLiteral();
}

switch (this.getIdentifierText(expression)) {
case "NaN":
return tstl.createParenthesizedExpression(
Expand All @@ -4925,6 +4921,9 @@ export class LuaTransformer {
const math = tstl.createIdentifier("math");
const huge = tstl.createStringLiteral("huge");
return tstl.createTableIndexExpression(math, huge, expression);

case "globalThis":
return tstl.createIdentifier("_G", expression, this.getIdentifierSymbolId(expression));
}

return identifier;
Expand Down Expand Up @@ -5432,7 +5431,8 @@ export class LuaTransformer {
protected hasUnsafeSymbolName(symbol: ts.Symbol, tsOriginal?: ts.Identifier): boolean {
const isLuaKeyword = luaKeywords.has(symbol.name);
const isInvalidIdentifier = !tsHelper.isValidLuaIdentifier(symbol.name);
const isAmbient = symbol.declarations.some(d => tsHelper.isAmbientNode(d));
// TODO rework once microsoft/TypeScript#24706 is fixed and remove check for symbol.declarations
const isAmbient = symbol.declarations && symbol.declarations.some(d => tsHelper.isAmbientNode(d));
if ((isLuaKeyword || isInvalidIdentifier) && isAmbient) {
// Catch ambient declarations of identifiers with bad names
throw TSTLErrors.InvalidAmbientIdentifierName(tsOriginal || ts.createIdentifier(symbol.name));
Expand Down
63 changes: 63 additions & 0 deletions test/unit/identifiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,66 @@ test("exported variable with lua keyword as name is not renamed", () => {

expect(util.transpileExecuteAndReturnExport(code, "print")).toBe("foobar");
});

describe("globalThis translation", () => {
test("globalThis to _G (expression)", () => {
const code = `
var foo = "bar";
return globalThis.foo;`;

const lua = util.transpileString(code);

expect(util.executeLua(lua)).toBe("bar");
});

test("globalThis to _G (assign)", () => {
const code = `
globalThis.foo = "bar";
return globalThis.foo;`;

expect(util.transpileAndExecute(code)).toBe("bar");
});

test("globalThis to _G (reassign)", () => {
const code = `
globalThis.foo = "bar";
globalThis.foo = "baz";
return globalThis.foo;`;

expect(util.transpileAndExecute(code)).toBe("baz");
});

test("globalThis to _G (function)", () => {
const code = `
globalThis.foo = () => "bar";
return globalThis.foo();`;

expect(util.transpileAndExecute(code)).toBe("bar");
});

test("globalThis to _G (assign + noImplicitAny)", () => {
const code = `
(<any>globalThis).foo = "bar";
return (<any>globalThis).foo;`;

expect(util.transpileAndExecute(code)).toBe("bar");
});

test("globalThis to _G (var)", () => {
const code = `
var globalFoo = "bar";
return globalThis.globalFoo;`;

const lua = util.transpileString(code);

expect(util.executeLua(lua)).toBe("bar");
});

test("globalThis to _G (let)", () => {
const code = `
let NotAGlobalFoo = "bar";
return (<any>globalThis).NotAGlobalFoo;`;

expect(util.transpileAndExecute(code)).toBe(undefined);
});
});