forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix rubocop#4663] Add new CommentedKeyword cop
- Loading branch information
Showing
8 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for comments put on the same line as some keywords. | ||
# These keywords are: `begin`, `class`, `def`, `end`, `module`. | ||
# | ||
# Note that some comments (such as `:nodoc:` and `rubocop:disable`) are | ||
# allowed. | ||
# | ||
# @example | ||
# # bad | ||
# if condition | ||
# statement | ||
# end # end if | ||
# | ||
# # bad | ||
# class X # comment | ||
# statement | ||
# end | ||
# | ||
# # bad | ||
# def x; end # comment | ||
# | ||
# # good | ||
# if condition | ||
# statement | ||
# end | ||
# | ||
# # good | ||
# class x # :nodoc: | ||
# y | ||
# end | ||
class CommentedKeyword < Cop | ||
MSG = 'Do not place comments on the same line as the ' \ | ||
'`%s` keyword.'.freeze | ||
|
||
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) } | ||
next unless offensive?(line) | ||
|
||
range = source_range(processed_source.buffer, | ||
index + 1, | ||
(line.index('#'))...(line.length)) | ||
|
||
add_offense(range, location: range) | ||
end | ||
end | ||
|
||
private | ||
|
||
KEYWORDS = %w[begin class def end module].freeze | ||
ALLOWED_COMMENTS = %w[:nodoc: rubocop:disable].freeze | ||
|
||
def offensive?(line) | ||
KEYWORDS.any? { |k| line =~ /^\s*#{k}\s+.*#/ } && | ||
ALLOWED_COMMENTS.none? { |c| line =~ /#\s*#{c}/ } | ||
end | ||
|
||
def message(node) | ||
line = node.source_line | ||
keyword = /^\s*(\S+).*#/.match(line)[1] | ||
format(MSG, keyword) | ||
end | ||
|
||
def extract_heredoc_lines(ast) | ||
return [] unless ast | ||
ast.each_node.with_object([]) do |node, heredocs| | ||
next unless node.location.is_a?(Parser::Source::Map::Heredoc) | ||
body = node.location.heredoc_body | ||
heredocs << (body.first_line...body.last_line) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# frozen_string_literal: true | ||
|
||
describe RuboCop::Cop::Style::CommentedKeyword do | ||
let(:config) { RuboCop::Config.new } | ||
subject(:cop) { described_class.new(config) } | ||
|
||
it 'registers an offense when commenting on the same line as `end`' do | ||
inspect_source(<<-RUBY.strip_indent) | ||
if x | ||
y | ||
end # comment | ||
RUBY | ||
expect(cop.highlights).to eq(['# comment']) | ||
expect(cop.messages).to eq(['Do not place comments on the same line as ' \ | ||
'the `end` keyword.']) | ||
end | ||
|
||
it 'registers an offense when commenting on the same line as `begin`' do | ||
inspect_source(<<-RUBY.strip_indent) | ||
begin # comment | ||
y | ||
end | ||
RUBY | ||
expect(cop.highlights).to eq(['# comment']) | ||
expect(cop.messages).to eq(['Do not place comments on the same line as ' \ | ||
'the `begin` keyword.']) | ||
end | ||
|
||
it 'registers an offense when commenting on the same line as `class`' do | ||
inspect_source(<<-RUBY.strip_indent) | ||
class X # comment | ||
y | ||
end | ||
RUBY | ||
expect(cop.highlights).to eq(['# comment']) | ||
expect(cop.messages).to eq(['Do not place comments on the same line as ' \ | ||
'the `class` keyword.']) | ||
end | ||
|
||
it 'registers an offense when commenting on the same line as `module`' do | ||
inspect_source(<<-RUBY.strip_indent) | ||
module X # comment | ||
y | ||
end | ||
RUBY | ||
expect(cop.highlights).to eq(['# comment']) | ||
expect(cop.messages).to eq(['Do not place comments on the same line as ' \ | ||
'the `module` keyword.']) | ||
end | ||
|
||
it 'registers an offense when commenting on the same line as `def`' do | ||
inspect_source(<<-RUBY.strip_indent) | ||
def x # comment | ||
y | ||
end | ||
RUBY | ||
expect(cop.highlights).to eq(['# comment']) | ||
expect(cop.messages).to eq(['Do not place comments on the same line as ' \ | ||
'the `def` keyword.']) | ||
end | ||
|
||
it 'registers an offense when commenting on indented keywords' do | ||
inspect_source(<<-RUBY.strip_indent) | ||
module X | ||
class Y # comment | ||
z | ||
end | ||
end | ||
RUBY | ||
expect(cop.highlights).to eq(['# comment']) | ||
expect(cop.messages).to eq(['Do not place comments on the same line as ' \ | ||
'the `class` keyword.']) | ||
end | ||
|
||
it 'registers an offense when commenting after keyword with spaces' do | ||
inspect_source(<<-RUBY.strip_indent) | ||
def x(a, b) # comment | ||
y | ||
end | ||
RUBY | ||
expect(cop.highlights).to eq(['# comment']) | ||
expect(cop.messages).to eq(['Do not place comments on the same line as ' \ | ||
'the `def` keyword.']) | ||
end | ||
|
||
it 'registers an offense for one-line cases' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
def x; end # comment' | ||
^^^^^^^^^^ Do not place comments on the same line as the `def` keyword. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense if there are no comments after keywords' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
if x | ||
y | ||
end | ||
RUBY | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
class X | ||
y | ||
end | ||
RUBY | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
begin | ||
x | ||
end | ||
RUBY | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
def x | ||
y | ||
end | ||
RUBY | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
module X | ||
y | ||
end | ||
RUBY | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
# module Y # trap comment | ||
RUBY | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
'end' # comment | ||
RUBY | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
<<-HEREDOC | ||
def # not a comment | ||
HEREDOC | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for certain comments' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
class X # :nodoc: | ||
y | ||
end | ||
RUBY | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
def x # rubocop:disable Metrics/MethodLength | ||
y | ||
end | ||
RUBY | ||
end | ||
end |