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#3666] Add new Uncommunicative MethodArg & BlockParam cops
- Loading branch information
1 parent
150be51
commit 3f65be3
Showing
11 changed files
with
455 additions
and
0 deletions.
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
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,68 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# Common functionality shared by Uncommunicative cops | ||
module UncommunicativeName | ||
CASE_MSG = 'Only use lowercase characters for %<name_type>s.'.freeze | ||
NUM_MSG = 'Do not end %<name_type>s with a number.'.freeze | ||
LENGTH_MSG = '%<name_type>s must be longer than %<min>s ' \ | ||
'characters.'.freeze | ||
|
||
def check(node, args, min:) | ||
args.each do |arg| | ||
source = arg.children.first.to_s | ||
range = arg_range(arg, source.size) | ||
case_offense(node, range) if uppercase?(source) | ||
num_offense(node, range) if ends_with_num?(source) | ||
length_offense(node, range, min) unless long_enough?(source, min) | ||
end | ||
end | ||
|
||
def case_offense(node, range) | ||
add_offense(node, location: range, | ||
message: format(CASE_MSG, name_type: name_type(node))) | ||
end | ||
|
||
def uppercase?(name) | ||
name =~ /[[:upper:]]/ | ||
end | ||
|
||
def name_type(node) | ||
@name_type ||= begin | ||
case node.type | ||
when :block then 'block parameter' | ||
when :def, :defs then 'method argument' | ||
end | ||
end | ||
end | ||
|
||
def num_offense(node, range) | ||
add_offense(node, location: range, | ||
message: format(NUM_MSG, name_type: name_type(node))) | ||
end | ||
|
||
def ends_with_num?(name) | ||
name[-1] =~ /\d/ | ||
end | ||
|
||
def length_offense(node, range, min) | ||
add_offense(node, location: range, | ||
message: format(LENGTH_MSG, | ||
name_type: name_type(node).capitalize, | ||
min: min)) | ||
end | ||
|
||
def long_enough?(name, min) | ||
name.size >= min | ||
end | ||
|
||
def arg_range(arg, length) | ||
begin_pos = arg.source_range.begin_pos | ||
Parser::Source::Range.new(processed_source.buffer, | ||
begin_pos, | ||
begin_pos + length) | ||
end | ||
end | ||
end | ||
end |
44 changes: 44 additions & 0 deletions
44
lib/rubocop/cop/naming/uncommunicative_block_param_name.rb
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,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Naming | ||
# This cop makes sure block parameter names meet a configurable | ||
# level of description | ||
# | ||
# @example | ||
# # bad | ||
# foo { |num1, num2| num1 + num2 } | ||
# | ||
# bar do |varOne, varTwo| | ||
# varOne + varTwo | ||
# end | ||
# | ||
# # With `MinParamNameLength` set to number greater than 1 | ||
# baz { |x, y, z| do_stuff(x, y, z) } | ||
# | ||
# # good | ||
# foo { |first_num, second_num| first_num + second_num } | ||
# | ||
# bar do |var_one, var_two| | ||
# var_one + var_two | ||
# end | ||
# | ||
# baz { |age, height, gender| do_stuff(age, height, gender) } | ||
class UncommunicativeBlockParamName < Cop | ||
include UncommunicativeName | ||
|
||
def on_block(node) | ||
return unless node.arguments? | ||
check(node, node.arguments, min: min_length) | ||
end | ||
|
||
private | ||
|
||
def min_length | ||
cop_config['MinParamNameLength'] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Naming | ||
# This cop makes sure method argument names meet a configurable | ||
# level of description | ||
# @example | ||
# # bad | ||
# def foo(num1, num2) | ||
# num1 + num2 | ||
# end | ||
# | ||
# def bar(varOne, varTwo) | ||
# varOne + varTwo | ||
# end | ||
# | ||
# # With `MinArgNameLength` set to number greater than 1 | ||
# def baz(x, y, z) | ||
# do_stuff(x, y, z) | ||
# end | ||
# | ||
# # good | ||
# def foo(first_num, second_num) | ||
# first_num + second_num | ||
# end | ||
# | ||
# def bar(var_one, var_two) | ||
# var_one + var_two | ||
# end | ||
# | ||
# def baz(age_x, height_y, gender_z) | ||
# do_stuff(age_x, height_y, gender_z) | ||
# end | ||
class UncommunicativeMethodArgName < Cop | ||
include UncommunicativeName | ||
|
||
def on_def(node) | ||
return unless node.arguments? | ||
check(node, node.arguments, min: min_length) | ||
end | ||
alias on_defs on_def | ||
|
||
private | ||
|
||
def min_length | ||
cop_config['MinArgNameLength'] | ||
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
64 changes: 64 additions & 0 deletions
64
spec/rubocop/cop/naming/uncommunicative_block_param_name_spec.rb
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,64 @@ | ||
# frozen_string_literal: true | ||
|
||
describe RuboCop::Cop::Naming::UncommunicativeBlockParamName, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:cop_config) { { 'MinParamNameLength' => 2 } } | ||
|
||
it 'does not register for block without parameters' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
something do | ||
do_stuff | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register for brace block without parameters' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
something { do_stuff } | ||
RUBY | ||
end | ||
|
||
it 'does not register offense for valid parameter names' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
something { |foo, bar| do_stuff } | ||
RUBY | ||
end | ||
|
||
it 'registers offense when param ends in number' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
something { |foo1, bar| do_stuff } | ||
^^^^ Do not end block parameter with a number. | ||
RUBY | ||
end | ||
|
||
it 'registers offense when param is less than minimum length' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
something do |x| | ||
^ Block parameter must be longer than 2 characters. | ||
do_stuff | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offense when param contains uppercase characters' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
something { |number_One| do_stuff } | ||
^^^^^^^^^^ Only use lowercase characters for block parameter. | ||
RUBY | ||
end | ||
|
||
it 'can register multiple offenses in one block' do | ||
inspect_source(<<-RUBY.strip_indent) | ||
something do |y, num1, oFo| | ||
do_stuff | ||
end | ||
RUBY | ||
expect(cop.offenses.size).to eq(3) | ||
expect(cop.messages).to eq [ | ||
'Block parameter must be longer than 2 characters.', | ||
'Do not end block parameter with a number.', | ||
'Only use lowercase characters for block parameter.' | ||
] | ||
end | ||
end |
Oops, something went wrong.