Skip to content

Commit 13cb62f

Browse files
authored
Merge pull request #956 from sl4vr/rspec-aliases_runtime-matchers
RSpec aliases runtime matchers
2 parents 2cd7639 + e515d21 commit 13cb62f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+487
-308
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Remove deprecated class `::RuboCop::Cop::RSpec::Cop`. ([@bquorning][])
66
* Retire `RSpec/InvalidPredicateMatcher` cop. ([@pirj][])
77
* Remove the code responsible for filtering files to inspect. ([@pirj][])
8+
* Make RSpec language elements configurable. ([@sl4vr][])
89

910
## 2.0.0.pre (2020-10-22)
1011

@@ -580,3 +581,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
580581
[@koic]: https://github.com/koic
581582
[@Rafix02]: https://github.com/Rafix02
582583
[@PhilCoggins]: https://github.com/PhilCoggins
584+
[@sl4vr]: https://github.com/sl4vr

config/default.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,83 @@ RSpec:
33
Include:
44
- "**/*_spec.rb"
55
- "**/spec/**/*"
6+
Language:
7+
ExampleGroups:
8+
Regular:
9+
- describe
10+
- context
11+
- feature
12+
- example_group
13+
Skipped:
14+
- xdescribe
15+
- xcontext
16+
- xfeature
17+
Focused:
18+
- fdescribe
19+
- fcontext
20+
- ffeature
21+
Examples:
22+
Regular:
23+
- it
24+
- specify
25+
- example
26+
- scenario
27+
- its
28+
Focused:
29+
- fit
30+
- fspecify
31+
- fexample
32+
- fscenario
33+
- focus
34+
Skipped:
35+
- xit
36+
- xspecify
37+
- xexample
38+
- xscenario
39+
- skip
40+
Pending:
41+
- pending
42+
Expectations:
43+
- expect
44+
- is_expected
45+
- expect_any_instance_of
46+
Helpers:
47+
- let
48+
- let!
49+
Hooks:
50+
- prepend_before
51+
- before
52+
- append_before
53+
- around
54+
- prepend_after
55+
- after
56+
- append_after
57+
HookScopes:
58+
- each
59+
- example
60+
- context
61+
- all
62+
- suite
63+
Includes:
64+
Examples:
65+
- it_behaves_like
66+
- it_should_behave_like
67+
- include_examples
68+
Context:
69+
- include_context
70+
Runners:
71+
- to
72+
- to_not
73+
- not_to
74+
SharedGroups:
75+
Examples:
76+
- shared_examples
77+
- shared_examples_for
78+
Context:
79+
- shared_context
80+
Subjects:
81+
- subject
82+
- subject!
683

784
RSpec/AlignLeftLetBrace:
885
Description: Checks that left braces for adjacent single line lets are aligned.

lib/rubocop-rspec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55

66
require 'rubocop'
77

8-
require_relative 'rubocop/rspec'
98
require_relative 'rubocop/rspec/version'
109
require_relative 'rubocop/rspec/inject'
1110
require_relative 'rubocop/rspec/node'
1211
require_relative 'rubocop/rspec/wording'
13-
require_relative 'rubocop/rspec/language'
1412
require_relative 'rubocop/rspec/language/node_pattern'
13+
require_relative 'rubocop/rspec/language'
1514

1615
require_relative 'rubocop/cop/rspec/mixin/top_level_group'
1716
require_relative 'rubocop/cop/rspec/mixin/variable'

lib/rubocop/cop/rspec/align_left_let_brace.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ def self.autocorrect_incompatible_with
2727
end
2828

2929
def on_new_investigation
30+
super
3031
return if processed_source.blank?
3132

32-
token_aligner =
33-
RuboCop::RSpec::AlignLetBrace.new(processed_source.ast, :begin)
34-
3533
token_aligner.offending_tokens.each do |let|
3634
add_offense(let.loc.begin) do |corrector|
3735
corrector.insert_before(
@@ -40,6 +38,12 @@ def on_new_investigation
4038
end
4139
end
4240
end
41+
42+
private
43+
44+
def token_aligner
45+
RuboCop::RSpec::AlignLetBrace.new(processed_source.ast, :begin)
46+
end
4347
end
4448
end
4549
end

lib/rubocop/cop/rspec/align_right_let_brace.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ def self.autocorrect_incompatible_with
2727
end
2828

2929
def on_new_investigation
30+
super
3031
return if processed_source.blank?
3132

32-
token_aligner =
33-
RuboCop::RSpec::AlignLetBrace.new(processed_source.ast, :end)
34-
3533
token_aligner.offending_tokens.each do |let|
3634
add_offense(let.loc.end) do |corrector|
3735
corrector.insert_before(
@@ -40,6 +38,12 @@ def on_new_investigation
4038
end
4139
end
4240
end
41+
42+
private
43+
44+
def token_aligner
45+
RuboCop::RSpec::AlignLetBrace.new(processed_source.ast, :end)
46+
end
4347
end
4448
end
4549
end

lib/rubocop/cop/rspec/base.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ module RSpec
66
# @abstract parent class to RSpec cops
77
class Base < ::RuboCop::Cop::Base
88
include RuboCop::RSpec::Language
9-
include RuboCop::RSpec::Language::NodePattern
9+
extend RuboCop::RSpec::Language::NodePattern
1010

1111
exclude_from_registry
1212

1313
# Invoke the original inherited hook so our cops are recognized
1414
def self.inherited(subclass) # rubocop:disable Lint/MissingSuper
1515
RuboCop::Cop::Base.inherited(subclass)
1616
end
17+
18+
# Set the config for dynamic DSL configuration-aware helpers
19+
# that have no other means of accessing the configuration.
20+
def on_new_investigation
21+
super
22+
RuboCop::RSpec::Language.config = config['RSpec']['Language']
23+
end
1724
end
1825
end
1926
end

lib/rubocop/cop/rspec/be.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Be < Base
2323
MSG = 'Don\'t use `be` without an argument.'
2424

2525
def_node_matcher :be_without_args, <<-PATTERN
26-
(send _ #{Runners::ALL.node_pattern_union} $(send nil? :be))
26+
(send _ #Runners.all $(send nil? :be))
2727
PATTERN
2828

2929
def on_send(node)

lib/rubocop/cop/rspec/capybara/current_path_expectation.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ class CurrentPathExpectation < Base
3737
# Supported matchers: eq(...) / match(/regexp/) / match('regexp')
3838
def_node_matcher :as_is_matcher, <<-PATTERN
3939
(send
40-
#expectation_set_on_current_path $#{Runners::ALL.node_pattern_union}
40+
#expectation_set_on_current_path $#Runners.all
4141
${(send nil? :eq ...) (send nil? :match (regexp ...))})
4242
PATTERN
4343

4444
def_node_matcher :regexp_str_matcher, <<-PATTERN
4545
(send
46-
#expectation_set_on_current_path $#{Runners::ALL.node_pattern_union}
46+
#expectation_set_on_current_path $#Runners.all
4747
$(send nil? :match (str $_)))
4848
PATTERN
4949

lib/rubocop/cop/rspec/capybara/feature_methods.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ class FeatureMethods < Base
5555
feature: :describe
5656
}.freeze
5757

58-
def_node_matcher :capybara_speak,
59-
SelectorSet.new(MAP.keys).node_pattern_union
58+
def_node_matcher :capybara_speak, <<-PATTERN
59+
{#{MAP.keys.map(&:inspect).join(' ')}}
60+
PATTERN
6061

6162
def_node_matcher :spec?, <<-PATTERN
6263
(block

lib/rubocop/cop/rspec/described_class.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ class DescribedClass < Base
6565
(block (send (const nil? {:Class :Module :Struct}) :new ...) ...)
6666
PATTERN
6767

68-
def_node_matcher :rspec_block?,
69-
RuboCop::RSpec::Language::ALL.block_pattern
68+
def_node_matcher :rspec_block?, block_pattern('#ALL.all')
7069

7170
def_node_matcher :scope_changing_syntax?, '{def class module}'
7271

0 commit comments

Comments
 (0)