Skip to content

Handle not() #47

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

Merged
merged 1 commit into from
Apr 15, 2022
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
4 changes: 2 additions & 2 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8168,7 +8168,7 @@ def deconstruct_keys(keys)
# not value
#
class Not < Node
# [untyped] the statement on which to operate
# [nil | untyped] the statement on which to operate
attr_reader :statement

# [boolean] whether or not parentheses were used
Expand Down Expand Up @@ -8205,7 +8205,7 @@ def deconstruct_keys(keys)

def format(q)
q.text(parentheses ? "not(" : "not ")
q.format(statement)
q.format(statement) if statement
q.text(")") if parentheses
end
end
Expand Down
10 changes: 4 additions & 6 deletions lib/syntax_tree/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2837,19 +2837,17 @@ def on_unary(operator, statement)
# parentheses they don't get reported as a paren node for some reason.

beginning = find_token(Kw, "not")
ending = statement
ending = statement || beginning
parentheses = source[beginning.location.end_char] == "("

range = beginning.location.end_char...statement.location.start_char
paren = source[range].include?("(")

if paren
if parentheses
find_token(LParen)
ending = find_token(RParen)
end

Not.new(
statement: statement,
parentheses: paren,
parentheses: parentheses,
location: beginning.location.to(ending.location)
)
else
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/not.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
%
not()
%
not ()
%
not foo
%
not(foo)
%
not (foo)