Skip to content
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

Properly track line numbers for keyword binary operators #449

Merged
merged 1 commit into from
Jan 8, 2024
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
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"].freeze


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
Loading