Skip to content
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

Merged
merged 4 commits into from
Sep 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,16 @@ Style/StringLiteralsInInterpolation:
- single_quotes
- double_quotes

Style/StringMethods:
Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

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 method intern is less likely to appear in other classes, as far as I can see.

# Mapping from undesired method to desired_method
# e.g. to use `to_sym` over `intern`:
#
# StringMethods:
# PreferredMethods:
# intern: to_sym
PreferredMethods:
intern: to_sym

Style/SpaceAroundBlockParameters:
EnforcedStyleInsidePipes: no_space
SupportedStyles:
Expand Down
4 changes: 4 additions & 0 deletions config/enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,10 @@ Style/StringLiteralsInInterpolation:
strings match the configured preference.
Enabled: true

Style/StringMethods:
Description: 'Checks if configured preferred methods are used over non-preferred.'
Enabled: true

Style/StructInheritance:
Description: 'Checks for inheritance from Struct.new.'
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-extend-struct-new'
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
require 'rubocop/cop/mixin/negative_conditional'
require 'rubocop/cop/mixin/on_method_def'
require 'rubocop/cop/mixin/method_complexity'
require 'rubocop/cop/mixin/method_preference'
require 'rubocop/cop/mixin/min_body_length'
require 'rubocop/cop/mixin/on_normal_if_unless'
require 'rubocop/cop/mixin/parser_diagnostic'
Expand Down Expand Up @@ -279,6 +280,7 @@
require 'rubocop/cop/style/special_global_vars'
require 'rubocop/cop/style/string_literals'
require 'rubocop/cop/style/string_literals_in_interpolation'
require 'rubocop/cop/style/string_methods'
require 'rubocop/cop/style/struct_inheritance'
require 'rubocop/cop/style/symbol_array'
require 'rubocop/cop/style/symbol_literal'
Expand Down
28 changes: 28 additions & 0 deletions lib/rubocop/cop/mixin/method_preference.rb
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
22 changes: 2 additions & 20 deletions lib/rubocop/cop/style/collection_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Style
# Enumerable or not (static analysis limitation), so this cop
# can yield some false positives.
class CollectionMethods < Cop
include MethodPreference

MSG = 'Prefer `%s` over `%s`.'

def on_block(node)
Expand Down Expand Up @@ -44,26 +46,6 @@ def check_method_node(node)
method_name)
)
end

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
Expand Down
32 changes: 32 additions & 0 deletions lib/rubocop/cop/style/string_methods.rb
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract code that's the same in CollectionMethods to a new module, calledMethodPreference or PreferredMethods under cop/mixin.

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
24 changes: 24 additions & 0 deletions spec/rubocop/cop/style/string_methods_spec.rb
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}`."])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a check for cop.highlights?

expect(cop.highlights).to eq(%w(intern))
end
end
end