Skip to content

Commit 8a31454

Browse files
committed
Check for variable declarations without initializer
1 parent 85bd764 commit 8a31454

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

dist/Transpiler.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,13 @@ var LuaTranspiler = /** @class */ (function () {
575575
LuaTranspiler.prototype.transpileVariableDeclaration = function (node) {
576576
// Find variable identifier
577577
var identifier = node.name;
578-
var value = this.transpileExpression(node.initializer);
579-
return "local " + identifier.escapedText + " = " + value;
578+
if (node.initializer) {
579+
var value = this.transpileExpression(node.initializer);
580+
return "local " + identifier.escapedText + " = " + value;
581+
}
582+
else {
583+
return "local " + identifier.escapedText + " = nil";
584+
}
580585
};
581586
LuaTranspiler.prototype.transpileFunctionDeclaration = function (node) {
582587
var result = "";

src/Transpiler.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,12 @@ export class LuaTranspiler {
635635
transpileVariableDeclaration(node: ts.VariableDeclaration): string {
636636
// Find variable identifier
637637
const identifier = <ts.Identifier>node.name;
638-
const value = this.transpileExpression(node.initializer);
639-
640-
return `local ${identifier.escapedText} = ${value}`;
638+
if (node.initializer) {
639+
const value = this.transpileExpression(node.initializer);
640+
return `local ${identifier.escapedText} = ${value}`;
641+
} else {
642+
return `local ${identifier.escapedText} = nil`;
643+
}
641644
}
642645

643646
transpileFunctionDeclaration(node: ts.FunctionDeclaration): string {

test/src/test.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
require("./test2"){$imports.name.escapedText} = require("./test2")local a = TestClass.new(0)
1+
require("./test2"){$imports.name.escapedText} = require("./test2")local a = TestClass.new(true,0)
22
GameState = GameState or {}
33
GameState.__index = GameState
4-
function GameState.new(...)
4+
function GameState.new(construct, ...)
55
local instance = setmetatable({}, GameState)
6-
if GameState.constructor then GameState.constructor(instance, ...) end
6+
if construct and GameState.constructor then GameState.constructor(instance, ...) end
77
return instance
88
end
99
function GameState.Init(self)
@@ -12,6 +12,7 @@ function GameState.Init(self)
1212
CustomGameEventManager.RegisterListener(CustomGameEventManager,"game_state_request",self.OnStateRequest)
1313
end
1414
function GameState.SetState(self,state)
15+
local testNil = nil
1516
self.state=state
1617
CustomGameEventManager.Send_ServerToAllClients(CustomGameEventManager,"game_state_update",{["state"]=self.state})
1718
for _, callback in ipairs(self.callbacks) do
@@ -24,7 +25,8 @@ end
2425
function GameState.RegisterListenerWithContext(self,callback,context)
2526
table.insert(self.callbacks, function(state)
2627
callback(context,state)
27-
end )
28+
end
29+
)
2830
end
2931
function GameState.OnStateRequest(self,userid,event)
3032
local player = PlayerResource.GetPlayer(PlayerResource,event.PlayerID)

test/src/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class GameState{
3333
}
3434

3535
SetState(state: GameRulesState) {
36+
let testNil;
37+
3638
this.state = state;
3739

3840
CustomGameEventManager.Send_ServerToAllClients("game_state_update", {state : this.state});

0 commit comments

Comments
 (0)