Skip to content

Commit 867989b

Browse files
committed
escape dot chains that are lua keywords
1 parent b7bf9b7 commit 867989b

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

moonscript/transform.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,19 @@ Value = Transformer({
12711271
end,
12721272
chain = function(self, node)
12731273
local stub = node[#node]
1274+
for i = 3, #node do
1275+
local part = node[i]
1276+
if ntype(part) == "dot" and data.lua_keywords[part[2]] then
1277+
node[i] = {
1278+
"index",
1279+
{
1280+
"string",
1281+
'"',
1282+
part[2]
1283+
}
1284+
}
1285+
end
1286+
end
12741287
if ntype(node[2]) == "string" then
12751288
node[2] = {
12761289
"parens",

moonscript/transform.moon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,12 @@ Value = Transformer {
680680
chain: (node) =>
681681
stub = node[#node]
682682

683+
-- escape lua keywords used in dot accessors
684+
for i=3,#node
685+
part = node[i]
686+
if ntype(part) == "dot" and data.lua_keywords[part[2]]
687+
node[i] = { "index", {"string", '"', part[2]} }
688+
683689
if ntype(node[2]) == "string"
684690
-- add parens if callee is raw string
685691
node[2] = {"parens", node[2] }

tests/inputs/syntax.moon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,9 @@ a *= 3 + 5
227227
a *= 3
228228
a /= func "cool"
229229

230+
---
231+
232+
x.then = "hello"
233+
x.while.true = "hello"
234+
230235
-- cooool

tests/outputs/syntax.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,6 @@ another(hello, one, two, three, four, {
226226
a = a + (3 - 5)
227227
a = a * (3 + 5)
228228
a = a * 3
229-
a = a / func("cool")
229+
a = a / func("cool")
230+
x["then"] = "hello"
231+
x["while"]["true"] = "hello"

0 commit comments

Comments
 (0)