Skip to content

Commit 3d056c8

Browse files
lollekoPerryvw
authored andcommitted
globalThis to _G (#631)
* globalThis to _G Closes #592 * moved globalThis check to transformIdentifierExpression * support for "typeof globalThis" WIP * Removed is standard lib test * globalThis transformation only for identifier name not identifier type. * fixed prettier * Fixed prettier script * Changed transpile -> execute to transpileAndExecute where possible * readd symbol.declaration check * Fixed prettier version inconsistency (1.17.x / 1.18.x)
1 parent 85050fd commit 3d056c8

File tree

4 files changed

+85
-41
lines changed

4 files changed

+85
-41
lines changed

package-lock.json

Lines changed: 11 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"pretest": "npm run lint && ts-node --transpile-only ./build_lualib.ts",
2424
"test": "jest",
2525
"lint": "npm run lint:tslint && npm run lint:prettier",
26-
"lint:prettier": "prettier --check **/*.{js,ts,yml,json,md} || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",
26+
"lint:prettier": "prettier --check \"**/*.{js,ts,yml,json,md}\" || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",
2727
"lint:tslint": "tslint -p . && tslint -p test && tslint -p src/lualib",
28-
"fix:prettier": "prettier --check --write **/*.{js,ts,yml,json,md}",
28+
"fix:prettier": "prettier --check --write \"**/*.{js,ts,yml,json,md}\"",
2929
"release-major": "npm version major",
3030
"release-minor": "npm version minor",
3131
"release-patch": "npm version patch",

src/LuaTransformer.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4876,14 +4876,6 @@ export class LuaTransformer {
48764876
}
48774877

48784878
public transformIdentifier(identifier: ts.Identifier): tstl.Identifier {
4879-
if (identifier.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
4880-
// TODO this is a hack that allows use to keep Identifier
4881-
// as return time as changing that would break a lot of stuff.
4882-
// But this should be changed to return tstl.createNilLiteral()
4883-
// at some point.
4884-
return tstl.createIdentifier("nil");
4885-
}
4886-
48874879
if (tsHelper.isForRangeType(identifier, this.checker)) {
48884880
const callExpression = tsHelper.findFirstNodeAbove(identifier, ts.isCallExpression);
48894881
if (!callExpression || !callExpression.parent || !ts.isForOfStatement(callExpression.parent)) {
@@ -4910,6 +4902,10 @@ export class LuaTransformer {
49104902
return this.createExportedIdentifier(identifier, exportScope);
49114903
}
49124904

4905+
if (expression.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
4906+
return tstl.createNilLiteral();
4907+
}
4908+
49134909
switch (this.getIdentifierText(expression)) {
49144910
case "NaN":
49154911
return tstl.createParenthesizedExpression(
@@ -4925,6 +4921,9 @@ export class LuaTransformer {
49254921
const math = tstl.createIdentifier("math");
49264922
const huge = tstl.createStringLiteral("huge");
49274923
return tstl.createTableIndexExpression(math, huge, expression);
4924+
4925+
case "globalThis":
4926+
return tstl.createIdentifier("_G", expression, this.getIdentifierSymbolId(expression));
49284927
}
49294928

49304929
return identifier;
@@ -5432,7 +5431,8 @@ export class LuaTransformer {
54325431
protected hasUnsafeSymbolName(symbol: ts.Symbol, tsOriginal?: ts.Identifier): boolean {
54335432
const isLuaKeyword = luaKeywords.has(symbol.name);
54345433
const isInvalidIdentifier = !tsHelper.isValidLuaIdentifier(symbol.name);
5435-
const isAmbient = symbol.declarations.some(d => tsHelper.isAmbientNode(d));
5434+
// TODO rework once microsoft/TypeScript#24706 is fixed and remove check for symbol.declarations
5435+
const isAmbient = symbol.declarations && symbol.declarations.some(d => tsHelper.isAmbientNode(d));
54365436
if ((isLuaKeyword || isInvalidIdentifier) && isAmbient) {
54375437
// Catch ambient declarations of identifiers with bad names
54385438
throw TSTLErrors.InvalidAmbientIdentifierName(tsOriginal || ts.createIdentifier(symbol.name));

test/unit/identifiers.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,3 +788,66 @@ test("exported variable with lua keyword as name is not renamed", () => {
788788

789789
expect(util.transpileExecuteAndReturnExport(code, "print")).toBe("foobar");
790790
});
791+
792+
describe("globalThis translation", () => {
793+
test("globalThis to _G (expression)", () => {
794+
const code = `
795+
var foo = "bar";
796+
return globalThis.foo;`;
797+
798+
const lua = util.transpileString(code);
799+
800+
expect(util.executeLua(lua)).toBe("bar");
801+
});
802+
803+
test("globalThis to _G (assign)", () => {
804+
const code = `
805+
globalThis.foo = "bar";
806+
return globalThis.foo;`;
807+
808+
expect(util.transpileAndExecute(code)).toBe("bar");
809+
});
810+
811+
test("globalThis to _G (reassign)", () => {
812+
const code = `
813+
globalThis.foo = "bar";
814+
globalThis.foo = "baz";
815+
return globalThis.foo;`;
816+
817+
expect(util.transpileAndExecute(code)).toBe("baz");
818+
});
819+
820+
test("globalThis to _G (function)", () => {
821+
const code = `
822+
globalThis.foo = () => "bar";
823+
return globalThis.foo();`;
824+
825+
expect(util.transpileAndExecute(code)).toBe("bar");
826+
});
827+
828+
test("globalThis to _G (assign + noImplicitAny)", () => {
829+
const code = `
830+
(<any>globalThis).foo = "bar";
831+
return (<any>globalThis).foo;`;
832+
833+
expect(util.transpileAndExecute(code)).toBe("bar");
834+
});
835+
836+
test("globalThis to _G (var)", () => {
837+
const code = `
838+
var globalFoo = "bar";
839+
return globalThis.globalFoo;`;
840+
841+
const lua = util.transpileString(code);
842+
843+
expect(util.executeLua(lua)).toBe("bar");
844+
});
845+
846+
test("globalThis to _G (let)", () => {
847+
const code = `
848+
let NotAGlobalFoo = "bar";
849+
return (<any>globalThis).NotAGlobalFoo;`;
850+
851+
expect(util.transpileAndExecute(code)).toBe(undefined);
852+
});
853+
});

0 commit comments

Comments
 (0)