Skip to content

Commit c3b7fe4

Browse files
committed
Fixed output of immediately invoked function expressions and similar.
1 parent 0fe9b2d commit c3b7fe4

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

dumbParser.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5082,15 +5082,21 @@ do
50825082
return true, lastOutput
50835083
end
50845084

5085+
local function doesExpressionNeedParenthesisIfOnTheLeftSide(expr)
5086+
local nodeType = expr.type
5087+
-- Some things, like "binary" or "vararg", are not here because those expressions add their own parentheses.
5088+
return nodeType == "literal" or nodeType == "table" or nodeType == "function"
5089+
end
5090+
50855091
-- Returns nil and a message or error.
50865092
local function writeLookup(buffer, pretty, indent, lastOutput, lookup, forMethodCall, nodeCb)
5087-
local objIsLiteral = (lookup.object.type == "literal")
5088-
if objIsLiteral then lastOutput = writeLua(buffer, "(", "") end
5093+
local objNeedParens = doesExpressionNeedParenthesisIfOnTheLeftSide(lookup.object)
5094+
if objNeedParens then lastOutput = writeLua(buffer, "(", "") end
50895095

50905096
local ok;ok, lastOutput = writeNode(buffer, pretty, indent, lastOutput, lookup.object, false, nodeCb)
50915097
if not ok then return nil, lastOutput end
50925098

5093-
if objIsLiteral then lastOutput = writeLua(buffer, ")", "") end
5099+
if objNeedParens then lastOutput = writeLua(buffer, ")", "") end
50945100

50955101
if canNodeBeName(lookup.member) then
50965102
lastOutput = writeLua(buffer, (forMethodCall and ":" or "."), "")
@@ -5361,8 +5367,13 @@ do
53615367
if not ok then return nil, lastOutput end
53625368

53635369
else
5370+
local needParens = doesExpressionNeedParenthesisIfOnTheLeftSide(call.callee)
5371+
if needParens then lastOutput = writeLua(buffer, "(", "") end
5372+
53645373
local ok;ok, lastOutput = writeNode(buffer, pretty, indent, lastOutput, call.callee, false, nodeCb)
53655374
if not ok then return nil, lastOutput end
5375+
5376+
if needParens then lastOutput = writeLua(buffer, ")", "") end
53665377
end
53675378

53685379
lastOutput = writeLua(buffer, "(", "")

test.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ do
7272
local s1 = "\"''a''b''c''\""
7373
local s2 = '\'""a""b""c""\''
7474

75+
local s = (""):rep(9)
76+
local n = ({9})[1]
77+
local v = (...)[1]
78+
local b = (function(n) return n>1 end)(9)
79+
7580
;;;;
7681

7782
-- Special number notations.

0 commit comments

Comments
 (0)