diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index fa4e0829..9e96bd96 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -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 @@ -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 diff --git a/lib/syntax_tree/parser.rb b/lib/syntax_tree/parser.rb index 77660f48..d5b82507 100644 --- a/lib/syntax_tree/parser.rb +++ b/lib/syntax_tree/parser.rb @@ -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 diff --git a/test/fixtures/not.rb b/test/fixtures/not.rb index eaa456f1..0204e345 100644 --- a/test/fixtures/not.rb +++ b/test/fixtures/not.rb @@ -1,4 +1,10 @@ % +not() +% +not () +% not foo % not(foo) +% +not (foo)