Skip to content

Commit

Permalink
Properly track line numbers for keyword binary operators
Browse files Browse the repository at this point in the history
  • Loading branch information
reese authored Dec 6, 2023
1 parent e00d2ab commit 6e8b424
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions fixtures/small/and_or_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
true and false
true or false
false or
true

true and
false or
maybe? and
some_other_things!


do_stuff! if should_do_thing_one? and should_do_thing_two?
10 changes: 10 additions & 0 deletions fixtures/small/and_or_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
true and false
true or false
false or true

true and
false or
maybe? and
some_other_things!

do_stuff! if should_do_thing_one? and should_do_thing_two?
9 changes: 9 additions & 0 deletions librubyfmt/rubyfmt_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module TrackAllScannerEvents

class Parser < Ripper::SexpBuilderPP
ARRAY_SYMBOLS = {qsymbols: "%i", qwords: "%w", symbols: "%I", words: "%W"}.freeze
OPERATOR_KEYWORDS = ["and", "or"]


def self.is_percent_array?(rest)
return false if rest.nil?
Expand Down Expand Up @@ -355,6 +357,13 @@ def on_kw(kw)
if stack = @kw_stacks[kw]
stack << lineno
end

# `or` and `and` should register their locations like other binary operators
if OPERATOR_KEYWORDS.include?(kw)
@op_locations << lineno
return super + [[lineno, lineno]]
end

super
end

Expand Down

0 comments on commit 6e8b424

Please sign in to comment.