Skip to content

Commit

Permalink
Deprecate IgnoredPatterns option
Browse files Browse the repository at this point in the history
Follow up rubocop/rubocop#10555

This PR obsoletes the `IgnoredPatterns` option and
replaces it with the `AllowedPatterns` option.
  • Loading branch information
ydah committed Aug 4, 2022
1 parent 5416f72 commit bb9d2d9
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
* Fix a false positive for `RSpec/Capybara/SpecificMatcher`. ([@ydah][])
* Fix a false negative for `RSpec/Capybara/SpecificMatcher` for `have_field`. ([@ydah][])
* Fix a false positive for `RSpec/Capybara/SpecificMatcher` when may not have a `href` by `have_link`. ([@ydah][])
* Deprecate `IgnoredPatterns` option. ([@ydah][])

## 2.12.1 (2022-07-03)

Expand Down
3 changes: 2 additions & 1 deletion config/default.yml
Expand Up @@ -790,9 +790,10 @@ RSpec/VariableName:
SupportedStyles:
- snake_case
- camelCase
AllowedPatterns: []
IgnoredPatterns: []
VersionAdded: '1.40'
VersionChanged: '1.43'
VersionChanged: '2.13'
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VariableName

RSpec/VerifiedDoubleReference:
Expand Down
14 changes: 14 additions & 0 deletions config/obsoletion.yml
@@ -0,0 +1,14 @@
#
# Configuration of obsolete/deprecated cops used by `ConfigObsoletion`.
#
# See: https://docs.rubocop.org/rubocop/extensions.html#config-obsoletions
#

# Cop parameters that have been changed
# Can be treated as a warning instead of a failure with `severity: warning`
changed_parameters:
- cops:
- RSpec/VariableName
parameters: IgnoredPatterns
alternative: AllowedPatterns
severity: warning
14 changes: 9 additions & 5 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Expand Up @@ -4465,12 +4465,12 @@ let('user_name') { 'Adam' }
| Yes
| No
| 1.40
| 1.43
| 2.13
|===

Checks that memoized helper names use the configured style.

Variables can be excluded from checking using the `IgnoredPatterns`
Variables can be excluded from checking using the `AllowedPatterns`
option.

=== Examples
Expand Down Expand Up @@ -4501,20 +4501,20 @@ subject(:userName1) { 'Adam' }
let(:userName2) { 'Adam' }
----

==== IgnoredPatterns configuration
==== AllowedPatterns configuration

[source,ruby]
----
# rubocop.yml
# RSpec/VariableName:
# EnforcedStyle: snake_case
# IgnoredPatterns:
# AllowedPatterns:
# - ^userFood
----

[source,ruby]
----
# okay because it matches the `^userFood` regex in `IgnoredPatterns`
# okay because it matches the `^userFood` regex in `AllowedPatterns`
subject(:userFood_1) { 'spaghetti' }
let(:userFood_2) { 'fettuccine' }
----
Expand All @@ -4528,6 +4528,10 @@ let(:userFood_2) { 'fettuccine' }
| `snake_case`
| `snake_case`, `camelCase`

| AllowedPatterns
| `[]`
| Array

| IgnoredPatterns
| `[]`
| Array
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop-rspec.rb
Expand Up @@ -5,6 +5,7 @@

require 'rubocop'

require_relative 'rubocop/rspec'
require_relative 'rubocop/rspec/version'
require_relative 'rubocop/rspec/inject'
require_relative 'rubocop/rspec/node'
Expand Down
12 changes: 6 additions & 6 deletions lib/rubocop/cop/rspec/variable_name.rb
Expand Up @@ -5,7 +5,7 @@ module Cop
module RSpec
# Checks that memoized helper names use the configured style.
#
# Variables can be excluded from checking using the `IgnoredPatterns`
# Variables can be excluded from checking using the `AllowedPatterns`
# option.
#
# @example EnforcedStyle: snake_case (default)
Expand All @@ -26,30 +26,30 @@ module RSpec
# subject(:userName1) { 'Adam' }
# let(:userName2) { 'Adam' }
#
# @example IgnoredPatterns configuration
# @example AllowedPatterns configuration
#
# # rubocop.yml
# # RSpec/VariableName:
# # EnforcedStyle: snake_case
# # IgnoredPatterns:
# # AllowedPatterns:
# # - ^userFood
#
# @example
# # okay because it matches the `^userFood` regex in `IgnoredPatterns`
# # okay because it matches the `^userFood` regex in `AllowedPatterns`
# subject(:userFood_1) { 'spaghetti' }
# let(:userFood_2) { 'fettuccine' }
#
class VariableName < Base
include ConfigurableNaming
include IgnoredPattern
include AllowedPattern
include Variable

MSG = 'Use %<style>s for variable names.'

def on_send(node)
variable_definition?(node) do |variable|
return if variable.dstr_type? || variable.dsym_type?
return if matches_ignored_pattern?(variable.value)
return if matches_allowed_pattern?(variable.value)

check_name(node, variable.value, variable.loc.expression)
end
Expand Down
14 changes: 14 additions & 0 deletions lib/rubocop/rspec.rb
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module RuboCop
# RuboCop RSpec project namespace
module RSpec
PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze

private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)

::RuboCop::ConfigObsoletion.files << PROJECT_ROOT.join('config',
'obsoletion.yml')
end
end
4 changes: 1 addition & 3 deletions lib/rubocop/rspec/inject.rb
Expand Up @@ -6,9 +6,7 @@ module RSpec
# bit of our configuration.
module Inject
def self.defaults!
project_root = Pathname.new(__dir__).parent.parent.parent.expand_path
config_default = project_root.join('config', 'default.yml')
path = config_default.to_s
path = CONFIG_DEFAULT.to_s
hash = ConfigLoader.send(:load_yaml_configuration, path)
config = RuboCop::Config.new(hash, path)
puts "configuration from #{path}" if ConfigLoader.debug?
Expand Down
22 changes: 21 additions & 1 deletion spec/rubocop/cop/rspec/variable_name_spec.rb
Expand Up @@ -189,7 +189,7 @@
end
end

context 'when configured to ignore certain patterns' do
context 'when configured to ignore certain patterns (deprecated key)' do
let(:cop_config) do
{ 'EnforcedStyle' => 'snake_case',
'IgnoredPatterns' => ['^userFood$', '^userPet$'] }
Expand All @@ -208,4 +208,24 @@
RUBY
end
end

context 'when configured to allow certain patterns' do
let(:cop_config) do
{ 'EnforcedStyle' => 'snake_case',
'AllowedPatterns' => ['^userFood$', '^userPet$'] }
end

it 'registers an offense when not matching any allowed patterns' do
expect_offense(<<~RUBY)
let(:userName) { 'Adam' }
^^^^^^^^^ Use snake_case for variable names.
RUBY
end

it 'does not register an offense when matching any allowed pattern' do
expect_no_offenses(<<~RUBY)
let(:userFood) { 'Adam' }
RUBY
end
end
end

0 comments on commit bb9d2d9

Please sign in to comment.