Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #7737
- Loading branch information
1 parent
f4151b4
commit 1b8d5d8
Showing
9 changed files
with
258 additions
and
2 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
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,69 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'parser/current' | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for redundant arguments to methods. | ||
# Method names & arguments can be configured like: | ||
# | ||
# RedundantArguments: | ||
# join: '' | ||
# split: ' ' | ||
# foo: 2 | ||
# | ||
# Limitations: | ||
# 1. This cop matches for method names only and hence cannot tell apart | ||
# methods with same name in different classes. | ||
# 2. This cop is limited to methods with single parameter. | ||
# | ||
# @example | ||
# # bad | ||
# | ||
# array.join('') | ||
# [1, 2, 3].join("") | ||
# string.split(" ") | ||
# "first\nsecond".split(" ") | ||
# A.foo(2) | ||
# | ||
# # good | ||
# array.join | ||
# [1, 2, 3].join | ||
# string.split | ||
# "first second".split | ||
# A.foo | ||
class RedundantArguments < Cop | ||
MSG = 'Argument is redundant.' | ||
|
||
def on_send(node) | ||
return unless redundant_argument?(node) | ||
|
||
add_offense(node) | ||
end | ||
|
||
private | ||
|
||
def redundant_argument?(node) | ||
redundant_argument = redundant_arg_for_method(node.method_name.to_s) | ||
return false if redundant_argument.nil? | ||
|
||
node.arguments.first == redundant_argument | ||
end | ||
|
||
def redundant_arg_for_method(method_name) | ||
return nil unless cop_config['RedundantArguments'].key?(method_name) | ||
|
||
@mem ||= {} | ||
@mem[method_name] ||= begin | ||
arg = cop_config['RedundantArguments'].fetch(method_name) | ||
buffer = Parser::Source::Buffer.new('(string)', 1) | ||
buffer.source = arg.inspect | ||
builder = RuboCop::AST::Builder.new | ||
Parser::CurrentRuby.new(builder).parse(buffer) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::RedundantArguments, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context 'join' do | ||
let(:cop_config) do | ||
{ 'RedundantArguments' => { 'join' => '' } } | ||
end | ||
|
||
it 'registers an offense when using `#join` with empty string argument' do | ||
expect_offense(<<~RUBY) | ||
foo.join('') | ||
^^^^^^^^^^^^ Argument is redundant. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `#join` with double quoted string' do | ||
expect_offense(<<~RUBY) | ||
foo.join("") | ||
^^^^^^^^^^^^ Argument is redundant. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `#join` on literal arrays' do | ||
expect_offense(<<~RUBY) | ||
[1, 2, 3].join("") | ||
^^^^^^^^^^^^^^^^^^ Argument is redundant. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#join` with no arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.join | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#join` with array literals' do | ||
expect_no_offenses(<<~RUBY) | ||
[1, 2, 3].join | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#join` with separator' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.join(',') | ||
RUBY | ||
end | ||
end | ||
|
||
context 'split' do | ||
let(:cop_config) do | ||
{ 'RedundantArguments' => { 'split' => ' ' } } | ||
end | ||
|
||
it 'registers an offense when using `#split` with space' do | ||
expect_offense(<<~RUBY) | ||
foo.split(' ') | ||
^^^^^^^^^^^^^^ Argument is redundant. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `#split` on string literal' do | ||
expect_offense(<<~RUBY) | ||
"first second".split(' ') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Argument is redundant. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `#join` with double quoted string' do | ||
expect_offense(<<~RUBY) | ||
foo.split(" ") | ||
^^^^^^^^^^^^^^ Argument is redundant. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#split` with no arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.split | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#split` with string literal' do | ||
expect_no_offenses(<<~RUBY) | ||
"first second".split | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#split` with separator' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.split(',') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#split` with empty string' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.split('') | ||
RUBY | ||
end | ||
end | ||
|
||
context 'non-builtin method' do | ||
let(:cop_config) do | ||
{ 'RedundantArguments' => { 'foo' => 2 } } | ||
end | ||
|
||
it 'registers an offense with configured argument' do | ||
expect_offense(<<~RUBY) | ||
A.foo(2) | ||
^^^^^^^^ Argument is redundant. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense with other argument' do | ||
expect_no_offenses(<<~RUBY) | ||
A.foo(5) | ||
RUBY | ||
end | ||
end | ||
end |