From 154dacbbef8ff1ff69f187010b9afc3cd672aaa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=BChmann?= Date: Tue, 2 Jul 2019 09:37:31 +0200 Subject: [PATCH] [#1117] Correct indentation around block comments The earlier solution simply avoided auto-correcting every node that contains a block comment. This solution indents as usual but leaves block comments untouched. --- .../cop/correctors/alignment_corrector.rb | 15 +++++++---- spec/rubocop/cop/alignment_corrector_spec.rb | 26 +++++++++++++++++++ .../cop/layout/indentation_width_spec.rb | 25 +++++++++++------- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/lib/rubocop/cop/correctors/alignment_corrector.rb b/lib/rubocop/cop/correctors/alignment_corrector.rb index d61d2b85b167..c89b6316cb33 100644 --- a/lib/rubocop/cop/correctors/alignment_corrector.rb +++ b/lib/rubocop/cop/correctors/alignment_corrector.rb @@ -17,9 +17,9 @@ def correct(processed_source, node, column_delta) @processed_source = processed_source expr = node.respond_to?(:loc) ? node.loc.expression : node - return if block_comment_within?(expr) - taboo_ranges = inside_string_ranges(node) + taboo_ranges = inside_string_ranges(node) + + block_comment_ranges lambda do |corrector| each_line(expr) do |line_begin_pos| @@ -79,9 +79,14 @@ def inside_regular_string_range(node) loc.begin.end.join(loc.end.begin) end - def block_comment_within?(expr) - processed_source.comments.select(&:document?).any? do |c| - within?(c.loc.expression, expr) + def block_comment_ranges + processed_source.comments.select(&:document?).map do |c| + range = c.loc.expression + if range.source.end_with?("\n") + range.adjust(end_pos: -1) + else + range + end end end diff --git a/spec/rubocop/cop/alignment_corrector_spec.rb b/spec/rubocop/cop/alignment_corrector_spec.rb index 695df4ac6d44..57e1e45e83fd 100644 --- a/spec/rubocop/cop/alignment_corrector_spec.rb +++ b/spec/rubocop/cop/alignment_corrector_spec.rb @@ -115,5 +115,31 @@ OUTPUT end end + + context 'with block comments' do + it 'does not indent block comments' do + expect(autocorrect_source(<<~INPUT)).to eq(<<~OUTPUT) + # >> 2 + begin + bar + =begin + Ancient + comment + =end + baz + end + INPUT + # >> 2 + begin + bar + =begin + Ancient + comment + =end + baz + end + OUTPUT + end + end end end diff --git a/spec/rubocop/cop/layout/indentation_width_spec.rb b/spec/rubocop/cop/layout/indentation_width_spec.rb index 27f135faea17..0d8a76dc4e2c 100644 --- a/spec/rubocop/cop/layout/indentation_width_spec.rb +++ b/spec/rubocop/cop/layout/indentation_width_spec.rb @@ -248,27 +248,34 @@ class Test RUBY end - it 'does not correct in scopes that contain block comments' do + it 'does not indent block comments' do source = <<~RUBY module Foo - # The class has a block comment within, so it's not corrected. class Bar =begin This is a nice long comment which spans a few lines + and must not be indented =end - # The method has no block comment inside, - # but it's within a class that has a block - # comment, so it's not corrected either. - def baz - do_something - end + do_something # indented end end RUBY - expect(autocorrect_source(source)).to eq source + expect(autocorrect_source(source)).to eq <<~RUBY + module Foo + class Bar + =begin + This is a nice long + comment + which spans a few lines + and must not be indented + =end + do_something # indented + end + end + RUBY end it 'does not indent heredoc strings' do