From 6e8b42491da8d477e632e01d16bb23a7fa8a5d97 Mon Sep 17 00:00:00 2001 From: Reese Williams Date: Wed, 6 Dec 2023 00:19:55 +0000 Subject: [PATCH] Properly track line numbers for keyword binary operators --- fixtures/small/and_or_actual.rb | 12 ++++++++++++ fixtures/small/and_or_expected.rb | 10 ++++++++++ librubyfmt/rubyfmt_lib.rb | 9 +++++++++ 3 files changed, 31 insertions(+) create mode 100644 fixtures/small/and_or_actual.rb create mode 100644 fixtures/small/and_or_expected.rb diff --git a/fixtures/small/and_or_actual.rb b/fixtures/small/and_or_actual.rb new file mode 100644 index 00000000..83b45f26 --- /dev/null +++ b/fixtures/small/and_or_actual.rb @@ -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? \ No newline at end of file diff --git a/fixtures/small/and_or_expected.rb b/fixtures/small/and_or_expected.rb new file mode 100644 index 00000000..dce7fe0c --- /dev/null +++ b/fixtures/small/and_or_expected.rb @@ -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? diff --git a/librubyfmt/rubyfmt_lib.rb b/librubyfmt/rubyfmt_lib.rb index 62c8cae5..0b0f8b4f 100644 --- a/librubyfmt/rubyfmt_lib.rb +++ b/librubyfmt/rubyfmt_lib.rb @@ -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? @@ -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