Skip to content

bug: Unary plus should parse the same way as unary minus. #281

@samwightt

Description

@samwightt

Did you check existing issues?

  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues of tree-sitter-c

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

No response

Describe the bug

I'm working on an ast-grep version of the Rubocop ambiguous operator lint. One of the cases it needs to alert on is something +x and something -y.

When I do something -y I get an AST like this:

Image

But when I do something +x I get a binary in the AST instead of a unary as expected:

Image

Ruby alerts on both of these, showing that it parses both of these as a unary:

def do_something(x); end

do_something +42
do_something -42
# Running that gives us this:
 /Users/samwightt/Code/ruby/ast-grep-rubocop/demo_ambiguous_operators.rb:3: warning: ambiguous first argument; put parentheses or a space even after `+` operator
    /Users/samwightt/Code/ruby/ast-grep-rubocop/demo_ambiguous_operators.rb:4: warning: ambiguous first argument; put parentheses or a space even after `-` operator

Steps To Reproduce/Bad Parse Tree

See examples above.

Expected Behavior/Parse Tree

The parse tree for something +x and something -y should be the same structure, minus the operator and name difference:

  • something + x should give a binary
  • something +x should give a unary
  • something+x should give a binary

Repro

something +x
something -y

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions