From 08fb1dc12428d5b4f767b186b902c53f45ca3015 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 10 Sep 2020 10:45:24 -0400 Subject: [PATCH 1/4] Add helper 'trailing_whitespace' --- spec/rubocop/cop/style/parentheses_around_condition_spec.rb | 1 - spec/rubocop/cop/style/redundant_begin_spec.rb | 2 -- spec/rubocop/cop/style/single_line_methods_spec.rb | 1 - spec/rubocop/cop/style/trailing_body_on_class_spec.rb | 1 - .../cop/style/trailing_body_on_method_definition_spec.rb | 1 - spec/rubocop/cop/style/trailing_body_on_module_spec.rb | 1 - spec/rubocop/cop/style/trailing_method_end_statement_spec.rb | 1 - spec/support/misc_helper.rb | 5 +++++ 8 files changed, 5 insertions(+), 8 deletions(-) create mode 100644 spec/support/misc_helper.rb diff --git a/spec/rubocop/cop/style/parentheses_around_condition_spec.rb b/spec/rubocop/cop/style/parentheses_around_condition_spec.rb index 6f24cacf992..e5317aef4d2 100644 --- a/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +++ b/spec/rubocop/cop/style/parentheses_around_condition_spec.rb @@ -189,7 +189,6 @@ context 'parentheses in multiline conditions are not allowed' do let(:cop_config) { { 'AllowInMultilineConditions' => false } } - let(:trailing_whitespace) { ' ' } it 'registers an offense for parentheses around multiline condition' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/style/redundant_begin_spec.rb b/spec/rubocop/cop/style/redundant_begin_spec.rb index 4d7cc2b1fab..b7bb1b8ab8e 100644 --- a/spec/rubocop/cop/style/redundant_begin_spec.rb +++ b/spec/rubocop/cop/style/redundant_begin_spec.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true RSpec.describe RuboCop::Cop::Style::RedundantBegin, :config do - let(:trailing_whitespace) { ' ' } - it 'reports an offense for single line def with redundant begin block' do expect_offense(<<~RUBY) def func; begin; x; y; rescue; z end; end diff --git a/spec/rubocop/cop/style/single_line_methods_spec.rb b/spec/rubocop/cop/style/single_line_methods_spec.rb index 250f7e6b91a..a6549c98f76 100644 --- a/spec/rubocop/cop/style/single_line_methods_spec.rb +++ b/spec/rubocop/cop/style/single_line_methods_spec.rb @@ -8,7 +8,6 @@ 'Layout/IndentationWidth' => { 'Width' => 2 }) end let(:cop_config) { { 'AllowIfMethodIsEmpty' => true } } - let(:trailing_whitespace) { ' ' } it 'registers an offense for a single-line method' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/style/trailing_body_on_class_spec.rb b/spec/rubocop/cop/style/trailing_body_on_class_spec.rb index 4e7b5a49040..581f8c9f492 100644 --- a/spec/rubocop/cop/style/trailing_body_on_class_spec.rb +++ b/spec/rubocop/cop/style/trailing_body_on_class_spec.rb @@ -6,7 +6,6 @@ let(:config) do RuboCop::Config.new('Layout/IndentationWidth' => { 'Width' => 2 }) end - let(:trailing_whitespace) { ' ' } it 'registers an offense when body trails after class definition' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/style/trailing_body_on_method_definition_spec.rb b/spec/rubocop/cop/style/trailing_body_on_method_definition_spec.rb index e9b1dc2f4a3..ed709ea0266 100644 --- a/spec/rubocop/cop/style/trailing_body_on_method_definition_spec.rb +++ b/spec/rubocop/cop/style/trailing_body_on_method_definition_spec.rb @@ -6,7 +6,6 @@ let(:config) do RuboCop::Config.new('Layout/IndentationWidth' => { 'Width' => 2 }) end - let(:trailing_whitespace) { ' ' } it 'registers an offense when body trails after method definition' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/style/trailing_body_on_module_spec.rb b/spec/rubocop/cop/style/trailing_body_on_module_spec.rb index aa476df401e..ada8e713822 100644 --- a/spec/rubocop/cop/style/trailing_body_on_module_spec.rb +++ b/spec/rubocop/cop/style/trailing_body_on_module_spec.rb @@ -6,7 +6,6 @@ let(:config) do RuboCop::Config.new('Layout/IndentationWidth' => { 'Width' => 2 }) end - let(:trailing_whitespace) { ' ' } it 'registers an offense when body trails after module definition' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/style/trailing_method_end_statement_spec.rb b/spec/rubocop/cop/style/trailing_method_end_statement_spec.rb index c3b24222ffe..a56249913fc 100644 --- a/spec/rubocop/cop/style/trailing_method_end_statement_spec.rb +++ b/spec/rubocop/cop/style/trailing_method_end_statement_spec.rb @@ -6,7 +6,6 @@ let(:config) do RuboCop::Config.new('Layout/IndentationWidth' => { 'Width' => 2 }) end - let(:trailing_whitespace) { ' ' } it 'register offense with trailing end on 2 line method' do expect_offense(<<~RUBY) diff --git a/spec/support/misc_helper.rb b/spec/support/misc_helper.rb new file mode 100644 index 00000000000..331d630d624 --- /dev/null +++ b/spec/support/misc_helper.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +def trailing_whitespace + ' ' +end From 80d8c6f4d7963b6c129c111f585807efa1223e39 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 10 Sep 2020 11:32:28 -0400 Subject: [PATCH 2/4] Remove useless trailing whitespace --- .../empty_lines_around_access_modifier_spec.rb | 4 ++-- .../layout/empty_lines_around_block_body_spec.rb | 2 +- .../layout/empty_lines_around_method_body_spec.rb | 2 +- .../cop/style/trailing_comma_in_block_args_spec.rb | 14 +++++++------- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb index 28abcb0b623..027ab3b2a57 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb @@ -43,7 +43,7 @@ class Test something #{access_modifier} - + def test; end end RUBY @@ -339,7 +339,7 @@ class Test #{access_modifier} #{'^' * access_modifier.size} Remove a blank line after `#{access_modifier}`. - + def test; end end RUBY diff --git a/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb index 89056795767..47cffacd9d5 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb @@ -28,7 +28,7 @@ expect_offense(<<~RUBY) some_method #{open} do_something - + ^{} Extra empty line detected at block body end. #{close} RUBY diff --git a/spec/rubocop/cop/layout/empty_lines_around_method_body_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_method_body_spec.rb index d37e59c3782..bcd04663ba7 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_method_body_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_method_body_spec.rb @@ -63,7 +63,7 @@ def some_method expect_offense(<<~RUBY) def Test.some_method do_something - + #{end_offense_annotation} end RUBY diff --git a/spec/rubocop/cop/style/trailing_comma_in_block_args_spec.rb b/spec/rubocop/cop/style/trailing_comma_in_block_args_spec.rb index 91e79a352fe..f0c9a233d73 100644 --- a/spec/rubocop/cop/style/trailing_comma_in_block_args_spec.rb +++ b/spec/rubocop/cop/style/trailing_comma_in_block_args_spec.rb @@ -65,12 +65,12 @@ expect_offense(<<~RUBY) test do |a, b,| ^ Useless trailing comma present in block arguments. - a + b + a + b end RUBY expect_correction(<<~RUBY) test do |a, b| - a + b + a + b end RUBY end @@ -78,7 +78,7 @@ it 'does not register an offense when a trailing comma is required' do expect_no_offenses(<<~RUBY) test do |a,| - a + a end RUBY end @@ -86,7 +86,7 @@ it 'does not register an offense when no arguments are present' do expect_no_offenses(<<~RUBY) test do - a + a end RUBY end @@ -125,15 +125,15 @@ it 'ignores commas in default argument strings' do expect_no_offenses(<<~RUBY) - add do |foo, bar = ','| - foo + bar + add do |foo, bar = ','| + foo + bar end RUBY end it 'preserves semicolons in block/local variables' do expect_no_offenses(<<~RUBY) - add do |foo, bar,; baz| + add do |foo, bar,; baz| foo + bar end RUBY From 731a624c7ff0ce29ebd0be114bc6e7161d927da5 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 10 Sep 2020 10:57:07 -0400 Subject: [PATCH 3/4] Use trailing_whitespace where useful --- .../cop/layout/empty_line_between_defs_spec.rb | 2 +- .../empty_lines_around_block_body_spec.rb | 2 +- .../first_array_element_line_break_spec.rb | 4 ++-- .../first_hash_element_line_break_spec.rb | 4 ++-- .../cop/layout/multiline_block_layout_spec.rb | 18 +++++++++--------- .../cop/lint/literal_in_interpolation_spec.rb | 8 ++++---- .../cop/lint/useless_method_definition_spec.rb | 16 ++++++++-------- .../cop/style/bisected_attr_accessor_spec.rb | 12 ++++++------ .../style/class_methods_definitions_spec.rb | 4 ++-- .../cop/style/line_end_concatenation_spec.rb | 2 +- .../cop/style/method_def_parentheses_spec.rb | 2 +- spec/rubocop/cop/style/mixin_grouping_spec.rb | 4 ++-- .../cop/style/redundant_assignment_spec.rb | 16 ++++++++-------- 13 files changed, 47 insertions(+), 47 deletions(-) diff --git a/spec/rubocop/cop/layout/empty_line_between_defs_spec.rb b/spec/rubocop/cop/layout/empty_line_between_defs_spec.rb index c49a64be753..155f707ba22 100644 --- a/spec/rubocop/cop/layout/empty_line_between_defs_spec.rb +++ b/spec/rubocop/cop/layout/empty_line_between_defs_spec.rb @@ -283,7 +283,7 @@ def a; end; def b; end RUBY expect_correction(<<~RUBY) - def a; end; + def a; end;#{trailing_whitespace} def b; end RUBY diff --git a/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb index 47cffacd9d5..a28ddc27cee 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb @@ -43,7 +43,7 @@ it 'accepts block body starting with a line with spaces' do expect_no_offenses(<<~RUBY) some_method #{open} - + #{trailing_whitespace} do_something #{close} RUBY diff --git a/spec/rubocop/cop/layout/first_array_element_line_break_spec.rb b/spec/rubocop/cop/layout/first_array_element_line_break_spec.rb index eec06932583..1a62800a860 100644 --- a/spec/rubocop/cop/layout/first_array_element_line_break_spec.rb +++ b/spec/rubocop/cop/layout/first_array_element_line_break_spec.rb @@ -92,7 +92,7 @@ expect(corrected).to eq(<<~RUBY) a, b, - c = + c =#{trailing_whitespace} 1, 2, 3 RUBY @@ -118,7 +118,7 @@ expect(new_source).to eq(<<~RUBY) a - .c = + .c =#{trailing_whitespace} 1, 2, 3 RUBY diff --git a/spec/rubocop/cop/layout/first_hash_element_line_break_spec.rb b/spec/rubocop/cop/layout/first_hash_element_line_break_spec.rb index dffcaaa46b9..9af13986b0f 100644 --- a/spec/rubocop/cop/layout/first_hash_element_line_break_spec.rb +++ b/spec/rubocop/cop/layout/first_hash_element_line_break_spec.rb @@ -11,7 +11,7 @@ RUBY expect_correction(<<~RUBY) - a = { + a = {#{trailing_whitespace} a: 1, b: 2 } RUBY @@ -25,7 +25,7 @@ RUBY expect_correction(<<~RUBY) - method({ + method({#{trailing_whitespace} foo: 1, bar: 2 }) RUBY diff --git a/spec/rubocop/cop/layout/multiline_block_layout_spec.rb b/spec/rubocop/cop/layout/multiline_block_layout_spec.rb index cc923aeb688..48d0c466436 100644 --- a/spec/rubocop/cop/layout/multiline_block_layout_spec.rb +++ b/spec/rubocop/cop/layout/multiline_block_layout_spec.rb @@ -11,7 +11,7 @@ RUBY expect_correction(<<~RUBY) - test do + test do#{trailing_whitespace} foo end RUBY @@ -26,7 +26,7 @@ RUBY expect_correction(<<~RUBY) - test { + test {#{trailing_whitespace} foo } RUBY @@ -41,7 +41,7 @@ RUBY expect_correction(<<~RUBY) - test do |x| + test do |x|#{trailing_whitespace} foo end RUBY @@ -56,7 +56,7 @@ RUBY expect_correction(<<~RUBY) - test { |x| + test { |x|#{trailing_whitespace} foo } RUBY @@ -150,7 +150,7 @@ RUBY expect_correction(<<~RUBY) - -> (x) do + -> (x) do#{trailing_whitespace} foo bar end @@ -166,7 +166,7 @@ RUBY expect_correction(<<~RUBY) - -> x do + -> x do#{trailing_whitespace} foo bar end @@ -226,7 +226,7 @@ RUBY expect_correction(<<~RUBY) - test do |foo| + test do |foo|#{trailing_whitespace} bar test end @@ -243,7 +243,7 @@ RUBY expect_correction(<<~RUBY) - x = -> (y) { + x = -> (y) {#{trailing_whitespace} foo bar } @@ -260,7 +260,7 @@ RUBY expect_correction(<<~RUBY) - foo do |o| + foo do |o|#{trailing_whitespace} ( bar ) diff --git a/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb b/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb index dd4b2ddabe4..2da6eba9b8e 100644 --- a/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +++ b/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb @@ -202,12 +202,12 @@ ^^^^^^ Literal interpolation detected. RUBY - expect_correction(<<~'RUBY') - "this is + expect_correction(<<~RUBY) + "this is#{trailing_whitespace} silly" - "this is + "this is#{trailing_whitespace} silly" - "this is + "this is#{trailing_whitespace} silly" RUBY end diff --git a/spec/rubocop/cop/lint/useless_method_definition_spec.rb b/spec/rubocop/cop/lint/useless_method_definition_spec.rb index c8cde11b7b6..ab52c9870b4 100644 --- a/spec/rubocop/cop/lint/useless_method_definition_spec.rb +++ b/spec/rubocop/cop/lint/useless_method_definition_spec.rb @@ -18,7 +18,7 @@ def initialize(arg1, arg2) expect_correction(<<~RUBY) class Foo - + #{trailing_whitespace} end RUBY end @@ -102,26 +102,26 @@ def useful_instance_method do_something end - + #{trailing_whitespace} - + #{trailing_whitespace} def self.useful_class_method do_something end - + #{trailing_whitespace} - + #{trailing_whitespace} class << self def self.other_useful_class_method do_something end - + #{trailing_whitespace} - + #{trailing_whitespace} end end RUBY @@ -189,7 +189,7 @@ def initialize expect_correction(<<~RUBY) class Foo - + #{trailing_whitespace} end RUBY end diff --git a/spec/rubocop/cop/style/bisected_attr_accessor_spec.rb b/spec/rubocop/cop/style/bisected_attr_accessor_spec.rb index d22e35fe707..79626ac0b76 100644 --- a/spec/rubocop/cop/style/bisected_attr_accessor_spec.rb +++ b/spec/rubocop/cop/style/bisected_attr_accessor_spec.rb @@ -17,7 +17,7 @@ class Foo expect_correction(<<~RUBY) class Foo attr_accessor :bar - + #{trailing_whitespace} other_macro :something end RUBY @@ -37,7 +37,7 @@ class Foo expect_correction(<<~RUBY) class Foo attr_accessor :bar - + #{trailing_whitespace} other_macro :something end RUBY @@ -59,7 +59,7 @@ class Foo class Foo ATTRIBUTES = %i[foo bar] attr_accessor *ATTRIBUTES - + #{trailing_whitespace} other_macro :something end RUBY @@ -106,11 +106,11 @@ class Foo expect_correction(<<~RUBY) class Foo attr_accessor :bar - + #{trailing_whitespace} private - + #{trailing_whitespace} attr_accessor :baz end RUBY @@ -140,7 +140,7 @@ class Foo class << self attr_accessor :baz - + #{trailing_whitespace} private diff --git a/spec/rubocop/cop/style/class_methods_definitions_spec.rb b/spec/rubocop/cop/style/class_methods_definitions_spec.rb index 111be4f8517..21e9cc52418 100644 --- a/spec/rubocop/cop/style/class_methods_definitions_spec.rb +++ b/spec/rubocop/cop/style/class_methods_definitions_spec.rb @@ -21,7 +21,7 @@ def three expect_correction(<<~RUBY) class A - + #{trailing_whitespace} def self.three end @@ -49,7 +49,7 @@ def two expect_correction(<<~RUBY) class A - + #{trailing_whitespace} # Multiline # comment. def self.two diff --git a/spec/rubocop/cop/style/line_end_concatenation_spec.rb b/spec/rubocop/cop/style/line_end_concatenation_spec.rb index bd9e426f3a2..608d8aab8fd 100644 --- a/spec/rubocop/cop/style/line_end_concatenation_spec.rb +++ b/spec/rubocop/cop/style/line_end_concatenation_spec.rb @@ -192,7 +192,7 @@ # whitespace in this cop. it 'autocorrects a + with trailing whitespace to \\' do expect_offense(<<~RUBY) - top = "test" + + top = "test" +#{trailing_whitespace} ^ Use `\\` instead of `+` or `<<` to concatenate those strings. "top" RUBY diff --git a/spec/rubocop/cop/style/method_def_parentheses_spec.rb b/spec/rubocop/cop/style/method_def_parentheses_spec.rb index 16d77e654a8..7b0de1c706d 100644 --- a/spec/rubocop/cop/style/method_def_parentheses_spec.rb +++ b/spec/rubocop/cop/style/method_def_parentheses_spec.rb @@ -149,7 +149,7 @@ def func() RUBY expect_correction(<<~RUBY) - def func + def func#{trailing_whitespace} end RUBY end diff --git a/spec/rubocop/cop/style/mixin_grouping_spec.rb b/spec/rubocop/cop/style/mixin_grouping_spec.rb index 17441cb842c..5106b2e16b1 100644 --- a/spec/rubocop/cop/style/mixin_grouping_spec.rb +++ b/spec/rubocop/cop/style/mixin_grouping_spec.rb @@ -206,7 +206,7 @@ class Foo class Foo prepend Qux, Baz, Bar do_something_else - + #{trailing_whitespace} end RUBY end @@ -229,7 +229,7 @@ class Foo prepend Qux, Bar Other.prepend Baz do_something_else - + #{trailing_whitespace} end RUBY end diff --git a/spec/rubocop/cop/style/redundant_assignment_spec.rb b/spec/rubocop/cop/style/redundant_assignment_spec.rb index a7dc9ac984b..3a9fb12735b 100644 --- a/spec/rubocop/cop/style/redundant_assignment_spec.rb +++ b/spec/rubocop/cop/style/redundant_assignment_spec.rb @@ -17,7 +17,7 @@ def func def func some_preceding_statements something - + #{trailing_whitespace} end RUBY end @@ -40,7 +40,7 @@ def func some_preceding_statements begin something - + #{trailing_whitespace} end end RUBY @@ -69,11 +69,11 @@ def func def func 1 2 - + #{trailing_whitespace} rescue SomeException 3 4 - + #{trailing_whitespace} rescue AnotherException 5 end @@ -117,12 +117,12 @@ def func some_preceding_statements if x 1 - + #{trailing_whitespace} elsif y 2 else 3 - + #{trailing_whitespace} end end RUBY @@ -156,13 +156,13 @@ def func case x when y 1 - + #{trailing_whitespace} when z 2 when q else 3 - + #{trailing_whitespace} end end RUBY From de4a5befec4d53cdbab44daba7948c6cd2bc1f25 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 10 Sep 2020 13:48:21 -0400 Subject: [PATCH 4/4] Disallow trailing whitespaces internally --- .rubocop.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 4f05d5be2d7..4a8e63e4686 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -59,9 +59,8 @@ Layout/ClassStructure: - protected_methods - private_methods -# Trailing white space is meaningful in code examples Layout/TrailingWhitespace: - AllowInHeredoc: true + AllowInHeredoc: false Lint/AmbiguousBlockAssociation: Exclude: