From 8014b6bc3c92f789ca1b989f7dd050403e74f934 Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Mon, 13 Jul 2020 21:13:50 +0200 Subject: [PATCH 1/4] Use subject and subject! in message Also, add "an" to error message. --- lib/rubocop/cop/rspec/empty_line_after_subject.rb | 5 +++-- spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/rubocop/cop/rspec/empty_line_after_subject.rb b/lib/rubocop/cop/rspec/empty_line_after_subject.rb index 2b95671fa..1bd80b3ac 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_subject.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_subject.rb @@ -18,14 +18,15 @@ class EmptyLineAfterSubject < Cop extend AutoCorrector include RuboCop::RSpec::BlankLineSeparation - MSG = 'Add empty line after `subject`.' + MSG = 'Add an empty line after `%s`.' def on_block(node) return unless subject?(node) && !in_spec_block?(node) return if last_child?(node) missing_separating_line(node) do |location| - add_offense(location) do |corrector| + msg = format(MSG, subject: node.method_name) + add_offense(location, message: msg) do |corrector| corrector.insert_after(location.end, "\n") end end diff --git a/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb b/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb index c4b4132aa..06e7d0cca 100644 --- a/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb +++ b/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb @@ -7,7 +7,7 @@ expect_offense(<<-RUBY) RSpec.describe User do subject { described_class.new } - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line after `subject`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after `subject`. let(:params) { foo } end RUBY @@ -25,7 +25,7 @@ expect_offense(<<-RUBY) RSpec.describe User do subject! { described_class.new } - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line after `subject`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after `subject!`. let(:params) { foo } end RUBY From 88d2b7c844e03c6a71d30d50634a3d9d83e3d185 Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Mon, 13 Jul 2020 21:13:53 +0200 Subject: [PATCH 2/4] Use let and let! in message Also, removed redundant "block" from the message. --- .../cop/rspec/empty_line_after_final_let.rb | 5 +++-- .../cop/rspec/empty_line_after_final_let_spec.rb | 14 +++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/rubocop/cop/rspec/empty_line_after_final_let.rb b/lib/rubocop/cop/rspec/empty_line_after_final_let.rb index 04a9537ef..2d4a4c9da 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_final_let.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_final_let.rb @@ -20,7 +20,7 @@ class EmptyLineAfterFinalLet < Cop extend AutoCorrector include RuboCop::RSpec::BlankLineSeparation - MSG = 'Add an empty line after the last `let` block.' + MSG = 'Add an empty line after the last `%s`.' def on_block(node) return unless example_group_with_body?(node) @@ -31,7 +31,8 @@ def on_block(node) return if last_child?(latest_let) missing_separating_line(latest_let) do |location| - add_offense(location) do |corrector| + msg = format(MSG, let: latest_let.method_name) + add_offense(location, message: msg) do |corrector| corrector.insert_after(location.end, "\n") end end diff --git a/spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb b/spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb index 777b6f510..9d1359892 100644 --- a/spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb +++ b/spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb @@ -8,7 +8,7 @@ RSpec.describe User do let(:a) { a } let(:b) { b } - ^^^^^^^^^^^^^ Add an empty line after the last `let` block. + ^^^^^^^^^^^^^ Add an empty line after the last `let`. it { expect(a).to eq(b) } end RUBY @@ -30,7 +30,7 @@ let!(:b) do b end - ^^^ Add an empty line after the last `let` block. + ^^^ Add an empty line after the last `let!`. it { expect(a).to eq(b) } end RUBY @@ -52,7 +52,7 @@ RSpec.describe User do let(:a) { a } let(:user, &args[:build_user]) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let` block. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let`. it { expect(a).to eq(b) } end RUBY @@ -96,7 +96,7 @@ let(:a) { a } let(:b) { b } # end of setup - ^^^^^^^^^^^^^^ Add an empty line after the last `let` block. + ^^^^^^^^^^^^^^ Add an empty line after the last `let`. it { expect(a).to eq(b) } end RUBY @@ -119,7 +119,7 @@ let(:b) { b } # a multiline comment marking # the end of setup - ^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let` block. + ^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let`. it { expect(a).to eq(b) } end RUBY @@ -144,7 +144,7 @@ subject { described_class } let!(:b) { b } - ^^^^^^^^^^^^^^ Add an empty line after the last `let` block. + ^^^^^^^^^^^^^^ Add an empty line after the last `let!`. it { expect(a).to eq(b) } end RUBY @@ -236,7 +236,7 @@ hello world BAR - ^^^ Add an empty line after the last `let` block. + ^^^ Add an empty line after the last `let`. it 'has tricky syntax' do expect(foo).to eql(" hello\n world\n") end From ee6da003ec221ce7f76cc60832a9253e94583ce3 Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Mon, 13 Jul 2020 21:13:56 +0200 Subject: [PATCH 3/4] Rename BlankLineSeparation to EmptyLineSeparation For consistency, let's call it an "empty line" everywhere. --- lib/rubocop-rspec.rb | 2 +- lib/rubocop/cop/rspec/empty_line_after_example.rb | 2 +- lib/rubocop/cop/rspec/empty_line_after_example_group.rb | 2 +- lib/rubocop/cop/rspec/empty_line_after_final_let.rb | 2 +- lib/rubocop/cop/rspec/empty_line_after_hook.rb | 2 +- lib/rubocop/cop/rspec/empty_line_after_subject.rb | 2 +- .../{blank_line_separation.rb => empty_line_separation.rb} | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) rename lib/rubocop/rspec/{blank_line_separation.rb => empty_line_separation.rb} (89%) diff --git a/lib/rubocop-rspec.rb b/lib/rubocop-rspec.rb index 93b586449..0d7816575 100644 --- a/lib/rubocop-rspec.rb +++ b/lib/rubocop-rspec.rb @@ -23,7 +23,7 @@ require_relative 'rubocop/rspec/align_let_brace' require_relative 'rubocop/rspec/factory_bot' require_relative 'rubocop/rspec/final_end_location' -require_relative 'rubocop/rspec/blank_line_separation' +require_relative 'rubocop/rspec/empty_line_separation' require_relative 'rubocop/rspec/corrector/move_node' RuboCop::RSpec::Inject.defaults! diff --git a/lib/rubocop/cop/rspec/empty_line_after_example.rb b/lib/rubocop/cop/rspec/empty_line_after_example.rb index fb8287470..e6f5b4fb0 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_example.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_example.rb @@ -43,7 +43,7 @@ module RSpec # class EmptyLineAfterExample < Cop extend AutoCorrector - include RuboCop::RSpec::BlankLineSeparation + include RuboCop::RSpec::EmptyLineSeparation MSG = 'Add an empty line after `%s`.' diff --git a/lib/rubocop/cop/rspec/empty_line_after_example_group.rb b/lib/rubocop/cop/rspec/empty_line_after_example_group.rb index 5fe23fc54..f5a3edb00 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_example_group.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_example_group.rb @@ -25,7 +25,7 @@ module RSpec # class EmptyLineAfterExampleGroup < Cop extend AutoCorrector - include RuboCop::RSpec::BlankLineSeparation + include RuboCop::RSpec::EmptyLineSeparation MSG = 'Add an empty line after `%s`.' diff --git a/lib/rubocop/cop/rspec/empty_line_after_final_let.rb b/lib/rubocop/cop/rspec/empty_line_after_final_let.rb index 2d4a4c9da..351abd97d 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_final_let.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_final_let.rb @@ -18,7 +18,7 @@ module RSpec # it { does_something } class EmptyLineAfterFinalLet < Cop extend AutoCorrector - include RuboCop::RSpec::BlankLineSeparation + include RuboCop::RSpec::EmptyLineSeparation MSG = 'Add an empty line after the last `%s`.' diff --git a/lib/rubocop/cop/rspec/empty_line_after_hook.rb b/lib/rubocop/cop/rspec/empty_line_after_hook.rb index 1b5f49881..cfb43b785 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_hook.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_hook.rb @@ -35,7 +35,7 @@ module RSpec # class EmptyLineAfterHook < Cop extend AutoCorrector - include RuboCop::RSpec::BlankLineSeparation + include RuboCop::RSpec::EmptyLineSeparation MSG = 'Add an empty line after `%s`.' diff --git a/lib/rubocop/cop/rspec/empty_line_after_subject.rb b/lib/rubocop/cop/rspec/empty_line_after_subject.rb index 1bd80b3ac..abeeb0ce2 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_subject.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_subject.rb @@ -16,7 +16,7 @@ module RSpec # let(:foo) { bar } class EmptyLineAfterSubject < Cop extend AutoCorrector - include RuboCop::RSpec::BlankLineSeparation + include RuboCop::RSpec::EmptyLineSeparation MSG = 'Add an empty line after `%s`.' diff --git a/lib/rubocop/rspec/blank_line_separation.rb b/lib/rubocop/rspec/empty_line_separation.rb similarity index 89% rename from lib/rubocop/rspec/blank_line_separation.rb rename to lib/rubocop/rspec/empty_line_separation.rb index 45375e7c5..bc5b8ea8a 100644 --- a/lib/rubocop/rspec/blank_line_separation.rb +++ b/lib/rubocop/rspec/empty_line_separation.rb @@ -2,9 +2,9 @@ module RuboCop module RSpec - # Helps determine the offending location if there is not a blank line + # Helps determine the offending location if there is not an empty line # following the node. Allows comments to follow directly after. - module BlankLineSeparation + module EmptyLineSeparation include FinalEndLocation include RuboCop::Cop::RangeHelp From 51d593abcbf926b6bab1ebc7c7c73d938c09924f Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Mon, 13 Jul 2020 21:13:59 +0200 Subject: [PATCH 4/4] Extract some shared logic to BlankLineSeparation --- lib/rubocop/cop/rspec/empty_line_after_example.rb | 8 ++------ .../cop/rspec/empty_line_after_example_group.rb | 8 ++------ lib/rubocop/cop/rspec/empty_line_after_final_let.rb | 8 ++------ lib/rubocop/cop/rspec/empty_line_after_hook.rb | 8 ++------ lib/rubocop/cop/rspec/empty_line_after_subject.rb | 8 ++------ lib/rubocop/rspec/empty_line_separation.rb | 11 +++++++++++ 6 files changed, 21 insertions(+), 30 deletions(-) diff --git a/lib/rubocop/cop/rspec/empty_line_after_example.rb b/lib/rubocop/cop/rspec/empty_line_after_example.rb index e6f5b4fb0..7bfbc8804 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_example.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_example.rb @@ -49,14 +49,10 @@ class EmptyLineAfterExample < Cop def on_block(node) return unless example?(node) - return if last_child?(node) return if allowed_one_liner?(node) - missing_separating_line(node) do |location| - msg = format(MSG, example: node.method_name) - add_offense(location, message: msg) do |corrector| - corrector.insert_after(location.end, "\n") - end + missing_separating_line_offense(node) do |method| + format(MSG, example: method) end end diff --git a/lib/rubocop/cop/rspec/empty_line_after_example_group.rb b/lib/rubocop/cop/rspec/empty_line_after_example_group.rb index f5a3edb00..b6e9b9fc8 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_example_group.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_example_group.rb @@ -31,13 +31,9 @@ class EmptyLineAfterExampleGroup < Cop def on_block(node) return unless example_group?(node) - return if last_child?(node) - missing_separating_line(node) do |location| - msg = format(MSG, example_group: node.method_name) - add_offense(location, message: msg) do |corrector| - corrector.insert_after(location.end, "\n") - end + missing_separating_line_offense(node) do |method| + format(MSG, example_group: method) end end end diff --git a/lib/rubocop/cop/rspec/empty_line_after_final_let.rb b/lib/rubocop/cop/rspec/empty_line_after_final_let.rb index 351abd97d..f75253e4e 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_final_let.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_final_let.rb @@ -28,13 +28,9 @@ def on_block(node) latest_let = node.body.child_nodes.select { |child| let?(child) }.last return if latest_let.nil? - return if last_child?(latest_let) - missing_separating_line(latest_let) do |location| - msg = format(MSG, let: latest_let.method_name) - add_offense(location, message: msg) do |corrector| - corrector.insert_after(location.end, "\n") - end + missing_separating_line_offense(latest_let) do |method| + format(MSG, let: method) end end end diff --git a/lib/rubocop/cop/rspec/empty_line_after_hook.rb b/lib/rubocop/cop/rspec/empty_line_after_hook.rb index cfb43b785..abfbb9076 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_hook.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_hook.rb @@ -41,13 +41,9 @@ class EmptyLineAfterHook < Cop def on_block(node) return unless hook?(node) - return if last_child?(node) - missing_separating_line(node) do |location| - msg = format(MSG, hook: node.method_name) - add_offense(location, message: msg) do |corrector| - corrector.insert_after(location.end, "\n") - end + missing_separating_line_offense(node) do |method| + format(MSG, hook: method) end end end diff --git a/lib/rubocop/cop/rspec/empty_line_after_subject.rb b/lib/rubocop/cop/rspec/empty_line_after_subject.rb index abeeb0ce2..90384e8c5 100644 --- a/lib/rubocop/cop/rspec/empty_line_after_subject.rb +++ b/lib/rubocop/cop/rspec/empty_line_after_subject.rb @@ -22,13 +22,9 @@ class EmptyLineAfterSubject < Cop def on_block(node) return unless subject?(node) && !in_spec_block?(node) - return if last_child?(node) - missing_separating_line(node) do |location| - msg = format(MSG, subject: node.method_name) - add_offense(location, message: msg) do |corrector| - corrector.insert_after(location.end, "\n") - end + missing_separating_line_offense(node) do |method| + format(MSG, subject: method) end end diff --git a/lib/rubocop/rspec/empty_line_separation.rb b/lib/rubocop/rspec/empty_line_separation.rb index bc5b8ea8a..ac0b9d582 100644 --- a/lib/rubocop/rspec/empty_line_separation.rb +++ b/lib/rubocop/rspec/empty_line_separation.rb @@ -8,6 +8,17 @@ module EmptyLineSeparation include FinalEndLocation include RuboCop::Cop::RangeHelp + def missing_separating_line_offense(node) + return if last_child?(node) + + missing_separating_line(node) do |location| + msg = yield(node.method_name) + add_offense(location, message: msg) do |corrector| + corrector.insert_after(location.end, "\n") + end + end + end + def missing_separating_line(node) line = final_end_location(node).line