class RuboCop::Cop::Layout::CommentIndentation
This cops checks the indentation of comments.
Constants
- MSG
Public Instance Methods
investigate(processed_source)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 12 def investigate(processed_source) processed_source.comments.each { |comment| check(comment) } end
Private Instance Methods
check(comment)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 18 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: format(MSG, column, correct_comment_indentation) ) end
correct_indentation(next_line)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 52 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 63 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 47 def line_after_comment(comment) lines = processed_source.lines lines[comment.loc.line..-1].find { |line| !line.blank? } end
own_line_comment?(comment)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 42 def own_line_comment?(comment) own_line = processed_source.lines[comment.loc.line - 1] own_line =~ /\A\s*#/ end
two_alternatives?(line)
click to toggle source
# File lib/rubocop/cop/layout/comment_indentation.rb, line 67 def two_alternatives?(line) line =~ /^\s*(else|elsif|when|rescue|ensure)\b/ end