@@ -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 , " (" , " " )
0 commit comments