File tree Expand file tree Collapse file tree 4 files changed +40
-1
lines changed
Expand file tree Collapse file tree 4 files changed +40
-1
lines changed Original file line number Diff line number Diff 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 [ ] ;
Original file line number Diff line number Diff 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.' ,
Original file line number Diff line number Diff 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 ) ) {
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments