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
1 change: 1 addition & 0 deletions src/CompilerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface TypeScriptToLuaOptions {
luaTarget?: LuaTarget;
luaLibImport?: LuaLibImportKind;
luaPlugins?: LuaPluginImport[];
noImplicitGlobalVariables?: boolean;
noImplicitSelf?: boolean;
noHeader?: boolean;
noResolvePaths?: string[];
Expand Down
6 changes: 6 additions & 0 deletions src/cli/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export const optionDeclarations: CommandLineOption[] = [
type: "enum",
choices: Object.values(LuaTarget),
},
{
name: "noImplicitGlobalVariables",
description:
'Specify to prevent implicitly turning "normal" variants into global variables in the transpiled output.',
type: "boolean",
},
{
name: "noImplicitSelf",
description: 'If "this" is implicitly considered an any type, do not generate a self parameter.',
Expand Down
4 changes: 3 additions & 1 deletion src/transformation/utils/lua-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ export function createLocalOrExportedOrGlobalDeclaration(
let declaration: lua.VariableDeclarationStatement | undefined;
let assignment: lua.AssignmentStatement | undefined;

const noImplicitGlobalVariables = context.options.noImplicitGlobalVariables === true;

const isFunctionDeclaration = tsOriginal !== undefined && ts.isFunctionDeclaration(tsOriginal);

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

if (context.isModule || !isTopLevelVariable) {
if (context.isModule || !isTopLevelVariable || noImplicitGlobalVariables) {
const isLuaFunctionExpression = rhs && !Array.isArray(rhs) && lua.isFunctionExpression(rhs);
const isSafeRecursiveFunctionDeclaration = isFunctionDeclaration && isLuaFunctionExpression;
if (!isSafeRecursiveFunctionDeclaration && hasMultipleReferences(scope, lhs)) {
Expand Down
30 changes: 30 additions & 0 deletions test/unit/functions/noImplicitGlobalVariables.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as util from "../../util";

test("normal TSTL creates global variables", () => {
const builder = util.testModule`
function foo() {}
const bar = 123;
`.expectToHaveNoDiagnostics();

const transpiledFile = builder.getLuaResult().transpiledFiles[0];
expect(transpiledFile).toBeDefined();
const { lua } = transpiledFile;
expect(lua).toBeDefined();
expect(lua).not.toContain("local");
});

test("noImplicitGlobalVariables does not create any global variables", () => {
const builder = util.testModule`
function foo() {}
const bar = 123;
`
.setOptions({ noImplicitGlobalVariables: true })
.expectToHaveNoDiagnostics();

const transpiledFile = builder.getLuaResult().transpiledFiles[0];
expect(transpiledFile).toBeDefined();
const { lua } = transpiledFile;
expect(lua).toBeDefined();
expect(lua).toContain("local function foo(");
expect(lua).toContain("local bar =");
});