-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a string methods cop #2204
Add a string methods cop #2204
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# encoding: utf-8 | ||
|
||
module RuboCop | ||
module Cop | ||
# Common code for cops that deal with preferred methods. | ||
module MethodPreference | ||
def preferred_method(method) | ||
preferred_methods[method.to_sym] | ||
end | ||
|
||
def preferred_methods | ||
@preferred_methods ||= | ||
begin | ||
# Make sure default configuration 'foo' => 'bar' is removed from | ||
# the total configuration if there is a 'bar' => 'foo' override. | ||
default = default_cop_config['PreferredMethods'] | ||
merged = cop_config['PreferredMethods'] | ||
overrides = merged.values - default.values | ||
merged.reject { |key, _| overrides.include?(key) }.symbolize_keys | ||
end | ||
end | ||
|
||
def default_cop_config | ||
ConfigLoader.default_configuration[cop_name] | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# encoding: utf-8 | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop enforces the use of consistent method names | ||
# from the String class. | ||
class StringMethods < Cop | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please extract code that's the same in |
||
include MethodPreference | ||
|
||
MSG = 'Prefer `%s` over `%s`.' | ||
|
||
def on_send(node) | ||
_receiver, method_name, *_args = *node | ||
return unless preferred_methods[method_name] | ||
add_offense(node, :selector, | ||
format(MSG, | ||
preferred_method(method_name), | ||
method_name) | ||
) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
corrector.replace(node.loc.selector, | ||
preferred_method(node.loc.selector.source)) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe RuboCop::Cop::Style::StringMethods, :config do | ||
cop_config = { | ||
'PreferredMethods' => { | ||
'intern' => 'to_sym' | ||
} | ||
} | ||
|
||
subject(:cop) { described_class.new(config) } | ||
let(:cop_config) { cop_config } | ||
|
||
cop_config['PreferredMethods'].each do |method, preferred_method| | ||
it "registers an offense for #{method}" do | ||
inspect_source(cop, "'something'.#{method}") | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.messages) | ||
.to eq(["Prefer `#{preferred_method}` over `#{method}`."]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there be a check for |
||
expect(cop.highlights).to eq(%w(intern)) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to add the cop in
enabled.yml
too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do. Just noticed that
CollectionMethods
is not in there - is that intentional?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found it in
disabled.yml
and that's because we expect it to generate a fair amount of false positives.StringMethods
might be OK to enable by default since the methodintern
is less likely to appear in other classes, as far as I can see.