Skip to content

Commit 2a5c4a2

Browse files
authored
Merge pull request #47 from ruby-syntax-tree/not-without-content
Handle not()
2 parents b0b45cd + 31f6e31 commit 2a5c4a2

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

lib/syntax_tree/node.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -8168,7 +8168,7 @@ def deconstruct_keys(keys)
81688168
# not value
81698169
#
81708170
class Not < Node
8171-
# [untyped] the statement on which to operate
8171+
# [nil | untyped] the statement on which to operate
81728172
attr_reader :statement
81738173

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

82068206
def format(q)
82078207
q.text(parentheses ? "not(" : "not ")
8208-
q.format(statement)
8208+
q.format(statement) if statement
82098209
q.text(")") if parentheses
82108210
end
82118211
end

lib/syntax_tree/parser.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -2837,19 +2837,17 @@ def on_unary(operator, statement)
28372837
# parentheses they don't get reported as a paren node for some reason.
28382838

28392839
beginning = find_token(Kw, "not")
2840-
ending = statement
2840+
ending = statement || beginning
2841+
parentheses = source[beginning.location.end_char] == "("
28412842

2842-
range = beginning.location.end_char...statement.location.start_char
2843-
paren = source[range].include?("(")
2844-
2845-
if paren
2843+
if parentheses
28462844
find_token(LParen)
28472845
ending = find_token(RParen)
28482846
end
28492847

28502848
Not.new(
28512849
statement: statement,
2852-
parentheses: paren,
2850+
parentheses: parentheses,
28532851
location: beginning.location.to(ending.location)
28542852
)
28552853
else

test/fixtures/not.rb

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
%
2+
not()
3+
%
4+
not ()
5+
%
26
not foo
37
%
48
not(foo)
9+
%
10+
not (foo)

0 commit comments

Comments
 (0)