Skip to content

Commit 43d31d0

Browse files
authored
noImplicitGlobalVariables compiler option (#1269)
* adding option + test * adding noImplicitGlobalVariables option * Update noImplicitGlobalVariables.spec.ts * prettier fix * fix tests
1 parent 5756807 commit 43d31d0

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/CompilerOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface TypeScriptToLuaOptions {
2929
luaTarget?: LuaTarget;
3030
luaLibImport?: LuaLibImportKind;
3131
luaPlugins?: LuaPluginImport[];
32+
noImplicitGlobalVariables?: boolean;
3233
noImplicitSelf?: boolean;
3334
noHeader?: boolean;
3435
noResolvePaths?: string[];

src/cli/parse.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ export const optionDeclarations: CommandLineOption[] = [
5858
type: "enum",
5959
choices: Object.values(LuaTarget),
6060
},
61+
{
62+
name: "noImplicitGlobalVariables",
63+
description:
64+
'Specify to prevent implicitly turning "normal" variants into global variables in the transpiled output.',
65+
type: "boolean",
66+
},
6167
{
6268
name: "noImplicitSelf",
6369
description: 'If "this" is implicitly considered an any type, do not generate a self parameter.',

src/transformation/utils/lua-ast.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ export function createLocalOrExportedOrGlobalDeclaration(
137137
let declaration: lua.VariableDeclarationStatement | undefined;
138138
let assignment: lua.AssignmentStatement | undefined;
139139

140+
const noImplicitGlobalVariables = context.options.noImplicitGlobalVariables === true;
141+
140142
const isFunctionDeclaration = tsOriginal !== undefined && ts.isFunctionDeclaration(tsOriginal);
141143

142144
const identifiers = castArray(lhs);
@@ -160,7 +162,7 @@ export function createLocalOrExportedOrGlobalDeclaration(
160162
const scope = peekScope(context);
161163
const isTopLevelVariable = scope.type === ScopeType.File;
162164

163-
if (context.isModule || !isTopLevelVariable) {
165+
if (context.isModule || !isTopLevelVariable || noImplicitGlobalVariables) {
164166
const isLuaFunctionExpression = rhs && !Array.isArray(rhs) && lua.isFunctionExpression(rhs);
165167
const isSafeRecursiveFunctionDeclaration = isFunctionDeclaration && isLuaFunctionExpression;
166168
if (!isSafeRecursiveFunctionDeclaration && hasMultipleReferences(scope, lhs)) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as util from "../../util";
2+
3+
test("normal TSTL creates global variables", () => {
4+
const builder = util.testModule`
5+
function foo() {}
6+
const bar = 123;
7+
`.expectToHaveNoDiagnostics();
8+
9+
const transpiledFile = builder.getLuaResult().transpiledFiles[0];
10+
expect(transpiledFile).toBeDefined();
11+
const { lua } = transpiledFile;
12+
expect(lua).toBeDefined();
13+
expect(lua).not.toContain("local");
14+
});
15+
16+
test("noImplicitGlobalVariables does not create any global variables", () => {
17+
const builder = util.testModule`
18+
function foo() {}
19+
const bar = 123;
20+
`
21+
.setOptions({ noImplicitGlobalVariables: true })
22+
.expectToHaveNoDiagnostics();
23+
24+
const transpiledFile = builder.getLuaResult().transpiledFiles[0];
25+
expect(transpiledFile).toBeDefined();
26+
const { lua } = transpiledFile;
27+
expect(lua).toBeDefined();
28+
expect(lua).toContain("local function foo(");
29+
expect(lua).toContain("local bar =");
30+
});

0 commit comments

Comments
 (0)