From 66d8440bf27854345fb29f67634d92e8d3ee9ef2 Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Sun, 5 Jan 2025 12:15:15 -0500 Subject: [PATCH] Fix immediately called `(op)` in pipeline context Fixes #1672 --- source/parser/lib.civet | 16 +++++++++++----- test/function-block-shorthand.civet | 8 ++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/source/parser/lib.civet b/source/parser/lib.civet index 21e2c49d..806be8ee 100644 --- a/source/parser/lib.civet +++ b/source/parser/lib.civet @@ -562,12 +562,18 @@ function processCallMemberExpression(node: CallExpression | MemberExpression): A (isComma(arg) as ASTLeaf).token = `)${op.token}(` commaCount++ // Don't mess with (+)() - if args.length - children.splice 0, 2, - commaCount ? + if args# + if commaCount + children.splice 0, 2, type: "ParenthesizedExpression" - children: ["(", ...call.children, ")"] - : { ...call, type: "ParenthesizedExpression" } + children: ["(", call.children, ")"] + expression: call.children + else // already parenthesized by argument parens + middle := call.children[<..<] + children.splice 0, 2, + type: "ParenthesizedExpression" + expression: middle + children: [ call.children.0, middle, call.children.-1 ] // If nothing left to this CallExpression, remove the wrapper if children# is 1 return children[0] diff --git a/test/function-block-shorthand.civet b/test/function-block-shorthand.civet index 82ad2a24..c0fc7ac6 100644 --- a/test/function-block-shorthand.civet +++ b/test/function-block-shorthand.civet @@ -920,6 +920,14 @@ describe "(op) shorthand", -> (1) """ + testCase """ + apply binary op with one argument in pipeline + --- + foo |> (+) x + --- + (x)(foo) + """ + testCase """ apply binary op with no arguments ---