class RuboCop::Cop::Layout::CommentIndentation
This cop checks the indentation of comments.
@example
# bad # comment here def method_name end # comment here a = 'hello' # yet another comment if true true end # good # comment here def method_name end # comment here a = 'hello' # yet another comment if true true end
Constants
- MSG
Public Instance Methods
autocorrect(comment)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 45 def autocorrect(comment) corrections = autocorrect_preceding_comments(comment) << autocorrect_one(comment) ->(corrector) { corrections.each { |corr| corr.call(corrector) } } end
investigate(processed_source)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 41 def investigate(processed_source) processed_source.each_comment { |comment| check(comment) } end
Private Instance Methods
autocorrect_one(comment)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 77 def autocorrect_one(comment) AlignmentCorrector.correct(processed_source, comment, @column_delta) end
autocorrect_preceding_comments(comment)
click to toggle source
Corrects all comment lines that occur immediately before the given comment and have the same indentation. This is to avoid a long chain of correcting, saving the file, parsing and inspecting again, and then correcting one more line, and so on.
# File lib/rubocop/cop/layout/comment_indentation.rb, line 57 def autocorrect_preceding_comments(comment) corrections = [] line_no = comment.loc.line column = comment.loc.column comments = processed_source.comments (comments.index(comment) - 1).downto(0) do |ix| previous_comment = comments[ix] break unless should_correct?(previous_comment, column, line_no - 1) corrections << autocorrect_one(previous_comment) line_no -= 1 end corrections end
check(comment)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 81 def check(comment) return unless own_line_comment?(comment) next_line = line_after_comment(comment) correct_comment_indentation = correct_indentation(next_line) column = comment.loc.column @column_delta = correct_comment_indentation - column return if @column_delta.zero? if two_alternatives?(next_line) # Try the other correct_comment_indentation += configured_indentation_width # We keep @column_delta unchanged so that autocorrect changes to # the preferred style of aligning the comment with the keyword. return if column == correct_comment_indentation end add_offense( comment, message: message(column, correct_comment_indentation) ) end
correct_indentation(next_line)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 123 def correct_indentation(next_line) return 0 unless next_line indentation_of_next_line = next_line =~ /\S/ indentation_of_next_line + if less_indented?(next_line) configured_indentation_width else 0 end end
less_indented?(line)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 134 def less_indented?(line) line =~ /^\s*(end\b|[)}\]])/ end
line_after_comment(comment)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 118 def line_after_comment(comment) lines = processed_source.lines lines[comment.loc.line..-1].find { |line| !line.blank? } end
message(column, correct_comment_indentation)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 105 def message(column, correct_comment_indentation) format( MSG, column: column, correct_comment_indentation: correct_comment_indentation ) end
own_line_comment?(comment)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 113 def own_line_comment?(comment) own_line = processed_source.lines[comment.loc.line - 1] own_line =~ /\A\s*#/ end
should_correct?(comment, column, line_no)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 72 def should_correct?(comment, column, line_no) loc = comment.loc loc.line == line_no && loc.column == column end
two_alternatives?(line)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 138 def two_alternatives?(line) line =~ /^\s*(else|elsif|when|rescue|ensure)\b/ end