Skip to content

Commit 94f5f6a

Browse files
committed
Fixed bug with object declaration indices
1 parent 8a31454 commit 94f5f6a

6 files changed

Lines changed: 23 additions & 15 deletions

File tree

dist/Transpiler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,12 +725,16 @@ var LuaTranspiler = /** @class */ (function () {
725725
node.properties.forEach(function (assignment) {
726726
var _a = TSHelper_1.TSHelper.getChildren(assignment), key = _a[0], value = _a[1];
727727
if (ts.isIdentifier(key)) {
728-
properties.push("[\"" + key.escapedText + "\"]=" + _this.transpileExpression(value));
728+
properties.push(key.escapedText + "=" + _this.transpileExpression(value));
729729
}
730-
else {
730+
else if (ts.isComputedPropertyName(key)) {
731731
var index = _this.transpileExpression(key);
732732
properties.push(index + "=" + _this.transpileExpression(value));
733733
}
734+
else {
735+
var index = _this.transpileExpression(key);
736+
properties.push("[" + index + "]=" + _this.transpileExpression(value));
737+
}
734738
});
735739
return "{" + properties.join(",") + "}";
736740
};

src/Transpiler.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,10 +807,13 @@ export class LuaTranspiler {
807807
node.properties.forEach(assignment => {
808808
const [key, value] = tsEx.getChildren(assignment);
809809
if (ts.isIdentifier(key)) {
810-
properties.push(`["${key.escapedText}"]=`+this.transpileExpression(value));
810+
properties.push(`${key.escapedText}=`+this.transpileExpression(value));
811+
} else if (ts.isComputedPropertyName(key)) {
812+
const index = this.transpileExpression(key);
813+
properties.push(`${index}=`+this.transpileExpression(value));
811814
} else {
812815
const index = this.transpileExpression(<ts.Expression>key);
813-
properties.push(`${index}=`+this.transpileExpression(value));
816+
properties.push(`[${index}]=`+this.transpileExpression(value));
814817
}
815818
});
816819

test/src/test.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ end
1414
function GameState.SetState(self,state)
1515
local testNil = nil
1616
self.state=state
17-
CustomGameEventManager.Send_ServerToAllClients(CustomGameEventManager,"game_state_update",{["state"]=self.state})
17+
CustomGameEventManager.Send_ServerToAllClients(CustomGameEventManager,"game_state_update",{state=self.state})
1818
for _, callback in ipairs(self.callbacks) do
1919
callback(self.state)
2020
end
@@ -30,5 +30,5 @@ function GameState.RegisterListenerWithContext(self,callback,context)
3030
end
3131
function GameState.OnStateRequest(self,userid,event)
3232
local player = PlayerResource.GetPlayer(PlayerResource,event.PlayerID)
33-
CustomGameEventManager.Send_ServerToPlayer(CustomGameEventManager,player,"game_state_response",{["state"]=self.state})
33+
CustomGameEventManager.Send_ServerToPlayer(CustomGameEventManager,player,"game_state_response",{state=self.state})
3434
end

test/src/test/test3.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local a = ""
22
local b = {1,2,3}
3-
local c = {["d"]=a,["e"]=b}
3+
local c = {d=a,e=b}
44
local d = #b
55
local e = #a
66
local f = #c.d

test/src/test2.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
local globalString = "glob"
22
local input = {1,2}
3-
local objTest = {["a"]=3,["B"]=true}
3+
local objTest = {a=3,["B"]=true,[input[0+1]]=5}
44
TestClass = TestClass or {}
55
TestClass.__index = TestClass
6-
function TestClass.new(...)
6+
function TestClass.new(construct, ...)
77
local instance = setmetatable({}, TestClass)
8-
if TestClass.constructor then TestClass.constructor(instance, ...) end
8+
if construct and TestClass.constructor then TestClass.constructor(instance, ...) end
99
return instance
1010
end
1111
function TestClass.constructor(self,tf)
@@ -26,15 +26,16 @@ end
2626
function Activate()
2727
local test = function()
2828
return ""
29-
end
29+
end
30+
3031
for i=0,10-1,1 do
3132
print(i)
3233
end
3334
for i=40,10+1,-1 do
3435
end
3536
for i=2,20,2 do
3637
end
37-
local i = ITE(false==false,function() return 0 end, function() return 4 end)
38+
local i = TS_ITE(false==false,function() return 0 end, function() return 4 end)
3839
i = i + 1
3940
i = i - 1
4041
not true
@@ -43,7 +44,7 @@ function Activate()
4344
i=i-1
4445
local a = bit.band(24,4)
4546
local list = {1,2,3}
46-
local obj = {["a"]=3}
47+
local obj = {a=3}
4748
for i=0,#list-1,1 do
4849
print(list[i+1])
4950
end
@@ -104,4 +105,4 @@ function Activate()
104105
::switchDone4::
105106
--------Switch statement end--------
106107
end
107-
local a = TestClass.new(3)
108+
local a = TestClass.new(true,3)

test/src/test2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var globalString = "glob";
44

55
let input = [1,2];
66

7-
let objTest = {a: 3, "B": true}
7+
let objTest = {a: 3, "B": true, [input[0]]: 5}
88

99
declare interface unit {
1010
GetParent(): unit;

0 commit comments

Comments
 (0)