Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix immediately called (op) in pipeline context #1677

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions source/parser/lib.civet
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions test/function-block-shorthand.civet
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,14 @@ describe "(op) shorthand", ->
(1)
"""

testCase """
apply binary op with one argument in pipeline
---
foo |> (+) x
---
(x)(foo)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that the desired result?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, (+) x instantly unwraps, it’s not a section

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's perfect, but the current behavior is that (+) can be followed by any number of arguments, and they get added together. In the special case of 1, it's not very useful, but maybe helpful in a setting where you're adding/removing arguments like so:

(+)
  part1
  part2
//edit from/to:
(+)
  part1
  //part2

(similar to how we support empty blocks in if etc.)

"""

testCase """
apply binary op with no arguments
---
Expand Down
Loading