Skip to content

Commit

Permalink
[Fix rubocop#4886] Fix false offense for Style/CommentedKeyword
Browse files Browse the repository at this point in the history
Commit also contains cop improvements:
Instead of going through each line of the processed_source we can easily take comments only and cop only those lines, where comments are located.
Instead of splitting line with `#` symbol we take column values from `comment.location`.
  • Loading branch information
michniewicz authored and michniewicz committed Oct 27, 2017
1 parent dd57e95 commit 7dc743b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
* [#4883](https://github.com/bbatsov/rubocop/pull/4883): Fix auto-correction for `Performance/HashEachMethods`. ([@pocke][])
* [#4888](https://github.com/bbatsov/rubocop/pull/4888): Improve offense message of `Style/StderPuts`. ([@jaredbeck][])
* [#4896](https://github.com/bbatsov/rubocop/pull/4896): Fix Style/DateTime wrongly triggered on classes `...::DateTime`. ([@dpostorivo][])
* [#4886](https://github.com/bbatsov/rubocop/issues/4886): Fix false offense for Style/CommentedKeyword. ([@michniewicz][])

## 0.51.0 (2017-10-18)

Expand Down Expand Up @@ -2983,3 +2984,4 @@
[@nelsonjr]: https://github.com/nelsonjr
[@jonatas]: https://github.com/jonatas
[@jaredbeck]: https://www.jaredbeck.com
[@michniewicz]: https://github.com/michniewicz
15 changes: 9 additions & 6 deletions lib/rubocop/cop/style/commented_keyword.rb
Expand Up @@ -39,13 +39,15 @@ class CommentedKeyword < Cop
def investigate(processed_source)
heredoc_lines = extract_heredoc_lines(processed_source.ast)

processed_source.lines.each_with_index do |line, index|
next if heredoc_lines.any? { |r| r.include?(index + 1) }
processed_source.comments.each 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,
index + 1,
(line.index('#'))...(line.length))
line_position,
(location.column)...(location.last_column))

add_offense(range, location: range)
end
Expand All @@ -57,7 +59,8 @@ def investigate(processed_source)
ALLOWED_COMMENTS = %w[:nodoc: rubocop:disable].freeze

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

Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/style/commented_keyword_spec.rb
Expand Up @@ -141,4 +141,17 @@ def x # rubocop:disable Metrics/MethodLength
end
RUBY
end

it 'does not register an offense if AST contains # symbol' do
expect_no_offenses(<<-RUBY.strip_indent)
def x(y = "#value")
y
end
RUBY
expect_no_offenses(<<-RUBY.strip_indent)
def x(y: "#value")
y
end
RUBY
end
end

0 comments on commit 7dc743b

Please sign in to comment.