Skip to content

Commit

Permalink
Simplify Style/CommentedKeyword
Browse files Browse the repository at this point in the history
  • Loading branch information
buehmann authored and bbatsov committed Jul 29, 2019
1 parent 1065300 commit f1eaf7f
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions lib/rubocop/cop/style/commented_keyword.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,12 @@ module Style
# y
# end
class CommentedKeyword < Cop
include RangeHelp

MSG = 'Do not place comments on the same line as the ' \
'`%<keyword>s` keyword.'

def investigate(processed_source)
heredoc_lines = extract_heredoc_lines(processed_source.ast)

processed_source.each_comment do |comment|
location = comment.location
line_position = location.line
line = processed_source.lines[line_position - 1]
next if heredoc_lines.any? { |r| r.include?(line_position) }
next unless offensive?(line)

range = source_range(processed_source.buffer,
line_position,
(location.column)...(location.last_column))

add_offense(range, location: range)
add_offense(comment) if offensive?(comment)
end
end

Expand All @@ -61,25 +47,19 @@ def investigate(processed_source)
KEYWORDS = %w[begin class def end module].freeze
ALLOWED_COMMENTS = %w[:nodoc: :yields: rubocop:disable].freeze

def offensive?(line)
line = line.lstrip
KEYWORDS.any? { |word| line =~ /^#{word}\s/ } &&
def offensive?(comment)
line = line(comment)
KEYWORDS.any? { |word| line =~ /^\s*#{word}\s/ } &&
ALLOWED_COMMENTS.none? { |c| line =~ /#\s*#{c}/ }
end

def message(node)
line = node.source_line
keyword = /^\s*(\S+).*#/.match(line)[1]
def message(comment)
keyword = line(comment).match(/(\S+).*#/)[1]
format(MSG, keyword: keyword)
end

def extract_heredoc_lines(ast)
return [] unless ast

ast.each_node(:str, :dstr, :xstr).select(&:heredoc?).map do |node|
body = node.location.heredoc_body
(body.first_line...body.last_line)
end
def line(comment)
comment.location.expression.source_line
end
end
end
Expand Down

0 comments on commit f1eaf7f

Please sign in to comment.