rubocop-rspec 1.38.1 → 1.43.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +57 -0
- data/CODE_OF_CONDUCT.md +17 -0
- data/README.md +1 -61
- data/config/default.yml +159 -19
- data/lib/rubocop-rspec.rb +5 -2
- data/lib/rubocop/cop/rspec/align_left_let_brace.rb +12 -19
- data/lib/rubocop/cop/rspec/align_right_let_brace.rb +12 -19
- data/lib/rubocop/cop/rspec/any_instance.rb +1 -1
- data/lib/rubocop/cop/rspec/around_block.rb +1 -1
- data/lib/rubocop/cop/rspec/base.rb +74 -0
- data/lib/rubocop/cop/rspec/be.rb +2 -2
- data/lib/rubocop/cop/rspec/be_eql.rb +6 -6
- data/lib/rubocop/cop/rspec/before_after_all.rb +1 -1
- data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +19 -17
- data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +14 -12
- data/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb +69 -0
- data/lib/rubocop/cop/rspec/context_method.rb +7 -9
- data/lib/rubocop/cop/rspec/context_wording.rb +3 -3
- data/lib/rubocop/cop/rspec/cop.rb +3 -87
- data/lib/rubocop/cop/rspec/describe_class.rb +29 -23
- data/lib/rubocop/cop/rspec/describe_method.rb +14 -7
- data/lib/rubocop/cop/rspec/describe_symbol.rb +2 -2
- data/lib/rubocop/cop/rspec/described_class.rb +12 -9
- data/lib/rubocop/cop/rspec/described_class_module_wrapping.rb +1 -1
- data/lib/rubocop/cop/rspec/dialect.rb +5 -12
- data/lib/rubocop/cop/rspec/empty_example_group.rb +91 -7
- data/lib/rubocop/cop/rspec/empty_hook.rb +46 -0
- data/lib/rubocop/cop/rspec/empty_line_after_example.rb +5 -7
- data/lib/rubocop/cop/rspec/empty_line_after_example_group.rb +5 -9
- data/lib/rubocop/cop/rspec/empty_line_after_final_let.rb +8 -8
- data/lib/rubocop/cop/rspec/empty_line_after_hook.rb +5 -9
- data/lib/rubocop/cop/rspec/empty_line_after_subject.rb +6 -6
- data/lib/rubocop/cop/rspec/example_length.rb +1 -1
- data/lib/rubocop/cop/rspec/example_without_description.rb +1 -1
- data/lib/rubocop/cop/rspec/example_wording.rb +10 -11
- data/lib/rubocop/cop/rspec/expect_actual.rb +8 -11
- data/lib/rubocop/cop/rspec/expect_change.rb +10 -35
- data/lib/rubocop/cop/rspec/expect_in_hook.rb +3 -3
- data/lib/rubocop/cop/rspec/expect_output.rb +2 -2
- data/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +24 -21
- data/lib/rubocop/cop/rspec/factory_bot/create_list.rb +20 -22
- data/lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb +7 -8
- data/lib/rubocop/cop/rspec/file_path.rb +57 -21
- data/lib/rubocop/cop/rspec/focus.rb +7 -11
- data/lib/rubocop/cop/rspec/hook_argument.rb +16 -23
- data/lib/rubocop/cop/rspec/hooks_before_examples.rb +10 -29
- data/lib/rubocop/cop/rspec/implicit_block_expectation.rb +1 -1
- data/lib/rubocop/cop/rspec/implicit_expect.rb +7 -15
- data/lib/rubocop/cop/rspec/implicit_subject.rb +16 -11
- data/lib/rubocop/cop/rspec/instance_spy.rb +18 -12
- data/lib/rubocop/cop/rspec/instance_variable.rb +4 -8
- data/lib/rubocop/cop/rspec/invalid_predicate_matcher.rb +3 -6
- data/lib/rubocop/cop/rspec/it_behaves_like.rb +5 -6
- data/lib/rubocop/cop/rspec/iterated_expectation.rb +1 -1
- data/lib/rubocop/cop/rspec/leading_subject.rb +22 -26
- data/lib/rubocop/cop/rspec/leaky_constant_declaration.rb +2 -5
- data/lib/rubocop/cop/rspec/let_before_examples.rb +10 -26
- data/lib/rubocop/cop/rspec/let_setup.rb +21 -6
- data/lib/rubocop/cop/rspec/message_chain.rb +7 -6
- data/lib/rubocop/cop/rspec/message_expectation.rb +2 -2
- data/lib/rubocop/cop/rspec/message_spies.rb +2 -3
- data/lib/rubocop/cop/rspec/missing_example_group_argument.rb +1 -1
- data/lib/rubocop/cop/rspec/multiple_describes.rb +11 -8
- data/lib/rubocop/cop/rspec/multiple_expectations.rb +7 -11
- data/lib/rubocop/cop/rspec/multiple_memoized_helpers.rb +148 -0
- data/lib/rubocop/cop/rspec/multiple_subjects.rb +18 -19
- data/lib/rubocop/cop/rspec/named_subject.rb +8 -8
- data/lib/rubocop/cop/rspec/nested_groups.rb +12 -13
- data/lib/rubocop/cop/rspec/not_to_not.rb +5 -6
- data/lib/rubocop/cop/rspec/overwriting_setup.rb +1 -1
- data/lib/rubocop/cop/rspec/pending.rb +1 -1
- data/lib/rubocop/cop/rspec/predicate_matcher.rb +32 -69
- data/lib/rubocop/cop/rspec/rails/http_status.rb +7 -9
- data/lib/rubocop/cop/rspec/receive_counts.rb +15 -17
- data/lib/rubocop/cop/rspec/receive_never.rb +12 -12
- data/lib/rubocop/cop/rspec/repeated_description.rb +1 -1
- data/lib/rubocop/cop/rspec/repeated_example.rb +2 -2
- data/lib/rubocop/cop/rspec/repeated_example_group_body.rb +12 -2
- data/lib/rubocop/cop/rspec/repeated_example_group_description.rb +1 -1
- data/lib/rubocop/cop/rspec/return_from_stub.rb +12 -22
- data/lib/rubocop/cop/rspec/scattered_let.rb +12 -2
- data/lib/rubocop/cop/rspec/scattered_setup.rb +1 -1
- data/lib/rubocop/cop/rspec/shared_context.rb +8 -21
- data/lib/rubocop/cop/rspec/shared_examples.rb +7 -9
- data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +15 -18
- data/lib/rubocop/cop/rspec/subject_stub.rb +25 -53
- data/lib/rubocop/cop/rspec/unspecified_exception.rb +1 -1
- data/lib/rubocop/cop/rspec/variable_definition.rb +56 -0
- data/lib/rubocop/cop/rspec/variable_name.rb +66 -0
- data/lib/rubocop/cop/rspec/verified_doubles.rb +1 -1
- data/lib/rubocop/cop/rspec/void_expect.rb +1 -1
- data/lib/rubocop/cop/rspec/yield.rb +14 -11
- data/lib/rubocop/cop/rspec_cops.rb +6 -1
- data/lib/rubocop/rspec/corrector/move_node.rb +54 -0
- data/lib/rubocop/rspec/description_extractor.rb +2 -6
- data/lib/rubocop/rspec/{blank_line_separation.rb → empty_line_separation.rb} +13 -10
- data/lib/rubocop/rspec/example_group.rb +21 -49
- data/lib/rubocop/rspec/factory_bot.rb +7 -1
- data/lib/rubocop/rspec/language.rb +13 -3
- data/lib/rubocop/rspec/language/node_pattern.rb +11 -2
- data/lib/rubocop/rspec/top_level_describe.rb +2 -2
- data/lib/rubocop/rspec/top_level_group.rb +55 -0
- data/lib/rubocop/rspec/variable.rb +16 -0
- data/lib/rubocop/rspec/version.rb +1 -1
- metadata +36 -13
- data/lib/rubocop/rspec/util.rb +0 -19
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
# Checks for empty before and after hooks.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# before {}
|
11
|
+
# after do; end
|
12
|
+
# before(:all) do
|
13
|
+
# end
|
14
|
+
# after(:all) { }
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# before { create_users }
|
18
|
+
# after do
|
19
|
+
# cleanup_users
|
20
|
+
# end
|
21
|
+
# before(:all) do
|
22
|
+
# create_feed
|
23
|
+
# end
|
24
|
+
# after(:all) { cleanup_feed }
|
25
|
+
class EmptyHook < Base
|
26
|
+
extend AutoCorrector
|
27
|
+
include RuboCop::Cop::RangeHelp
|
28
|
+
|
29
|
+
MSG = 'Empty hook detected.'
|
30
|
+
|
31
|
+
def_node_matcher :empty_hook?, <<~PATTERN
|
32
|
+
(block $#{Hooks::ALL.send_pattern} _ nil?)
|
33
|
+
PATTERN
|
34
|
+
|
35
|
+
def on_block(node)
|
36
|
+
empty_hook?(node) do |hook|
|
37
|
+
add_offense(hook) do |corrector|
|
38
|
+
range = range_with_surrounding_space(range: node.loc.expression)
|
39
|
+
corrector.remove(range)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -41,20 +41,18 @@ module RuboCop
|
|
41
41
|
# it { two }
|
42
42
|
# end
|
43
43
|
#
|
44
|
-
class EmptyLineAfterExample <
|
45
|
-
|
44
|
+
class EmptyLineAfterExample < Base
|
45
|
+
extend AutoCorrector
|
46
|
+
include RuboCop::RSpec::EmptyLineSeparation
|
46
47
|
|
47
48
|
MSG = 'Add an empty line after `%<example>s`.'
|
48
49
|
|
49
50
|
def on_block(node)
|
50
51
|
return unless example?(node)
|
51
|
-
return if last_child?(node)
|
52
52
|
return if allowed_one_liner?(node)
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
location: location,
|
57
|
-
message: format(MSG, example: node.method_name))
|
54
|
+
missing_separating_line_offense(node) do |method|
|
55
|
+
format(MSG, example: method)
|
58
56
|
end
|
59
57
|
end
|
60
58
|
|
@@ -23,21 +23,17 @@ module RuboCop
|
|
23
23
|
# end
|
24
24
|
# end
|
25
25
|
#
|
26
|
-
class EmptyLineAfterExampleGroup <
|
27
|
-
|
26
|
+
class EmptyLineAfterExampleGroup < Base
|
27
|
+
extend AutoCorrector
|
28
|
+
include RuboCop::RSpec::EmptyLineSeparation
|
28
29
|
|
29
30
|
MSG = 'Add an empty line after `%<example_group>s`.'
|
30
31
|
|
31
32
|
def on_block(node)
|
32
33
|
return unless example_group?(node)
|
33
|
-
return if last_child?(node)
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
node,
|
38
|
-
location: location,
|
39
|
-
message: format(MSG, example_group: node.method_name)
|
40
|
-
)
|
35
|
+
missing_separating_line_offense(node) do |method|
|
36
|
+
format(MSG, example_group: method)
|
41
37
|
end
|
42
38
|
end
|
43
39
|
end
|
@@ -16,21 +16,21 @@ module RuboCop
|
|
16
16
|
# let(:something) { other }
|
17
17
|
#
|
18
18
|
# it { does_something }
|
19
|
-
class EmptyLineAfterFinalLet <
|
20
|
-
|
19
|
+
class EmptyLineAfterFinalLet < Base
|
20
|
+
extend AutoCorrector
|
21
|
+
include RuboCop::RSpec::EmptyLineSeparation
|
21
22
|
|
22
|
-
MSG = 'Add an empty line after the last
|
23
|
+
MSG = 'Add an empty line after the last `%<let>s`.'
|
23
24
|
|
24
25
|
def on_block(node)
|
25
26
|
return unless example_group_with_body?(node)
|
26
27
|
|
27
|
-
|
28
|
+
final_let = node.body.child_nodes.reverse.find { |child| let?(child) }
|
28
29
|
|
29
|
-
return if
|
30
|
-
return if last_child?(latest_let)
|
30
|
+
return if final_let.nil?
|
31
31
|
|
32
|
-
|
33
|
-
|
32
|
+
missing_separating_line_offense(final_let) do |method|
|
33
|
+
format(MSG, let: method)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
@@ -33,21 +33,17 @@ module RuboCop
|
|
33
33
|
#
|
34
34
|
# it { does_something }
|
35
35
|
#
|
36
|
-
class EmptyLineAfterHook <
|
37
|
-
|
36
|
+
class EmptyLineAfterHook < Base
|
37
|
+
extend AutoCorrector
|
38
|
+
include RuboCop::RSpec::EmptyLineSeparation
|
38
39
|
|
39
40
|
MSG = 'Add an empty line after `%<hook>s`.'
|
40
41
|
|
41
42
|
def on_block(node)
|
42
43
|
return unless hook?(node)
|
43
|
-
return if last_child?(node)
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
node,
|
48
|
-
location: location,
|
49
|
-
message: format(MSG, hook: node.method_name)
|
50
|
-
)
|
45
|
+
missing_separating_line_offense(node) do |method|
|
46
|
+
format(MSG, hook: method)
|
51
47
|
end
|
52
48
|
end
|
53
49
|
end
|
@@ -14,17 +14,17 @@ module RuboCop
|
|
14
14
|
# subject(:obj) { described_class }
|
15
15
|
#
|
16
16
|
# let(:foo) { bar }
|
17
|
-
class EmptyLineAfterSubject <
|
18
|
-
|
17
|
+
class EmptyLineAfterSubject < Base
|
18
|
+
extend AutoCorrector
|
19
|
+
include RuboCop::RSpec::EmptyLineSeparation
|
19
20
|
|
20
|
-
MSG = 'Add empty line after
|
21
|
+
MSG = 'Add an empty line after `%<subject>s`.'
|
21
22
|
|
22
23
|
def on_block(node)
|
23
24
|
return unless subject?(node) && !in_spec_block?(node)
|
24
|
-
return if last_child?(node)
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
missing_separating_line_offense(node) do |method|
|
27
|
+
format(MSG, subject: method)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -47,7 +47,7 @@ module RuboCop
|
|
47
47
|
# result = service.call
|
48
48
|
# expect(result).to be(true)
|
49
49
|
# end
|
50
|
-
class ExampleWithoutDescription <
|
50
|
+
class ExampleWithoutDescription < Base
|
51
51
|
include ConfigurableEnforcedStyle
|
52
52
|
|
53
53
|
MSG_DEFAULT_ARGUMENT = 'Omit the argument when you want to ' \
|
@@ -29,7 +29,9 @@ module RuboCop
|
|
29
29
|
# # good
|
30
30
|
# it 'does things' do
|
31
31
|
# end
|
32
|
-
class ExampleWording <
|
32
|
+
class ExampleWording < Base
|
33
|
+
extend AutoCorrector
|
34
|
+
|
33
35
|
MSG_SHOULD = 'Do not use should when describing your tests.'
|
34
36
|
MSG_IT = "Do not repeat 'it' when describing your tests."
|
35
37
|
|
@@ -45,24 +47,21 @@ module RuboCop
|
|
45
47
|
|
46
48
|
def on_block(node)
|
47
49
|
it_description(node) do |description_node, message|
|
48
|
-
if message
|
50
|
+
if message.match?(SHOULD_PREFIX)
|
49
51
|
add_wording_offense(description_node, MSG_SHOULD)
|
50
|
-
elsif message
|
52
|
+
elsif message.match?(IT_PREFIX)
|
51
53
|
add_wording_offense(description_node, MSG_IT)
|
52
54
|
end
|
53
55
|
end
|
54
56
|
end
|
55
57
|
|
56
|
-
def autocorrect(node)
|
57
|
-
lambda do |corrector|
|
58
|
-
corrector.replace(docstring(node), replacement_text(node))
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
58
|
private
|
63
59
|
|
64
60
|
def add_wording_offense(node, message)
|
65
|
-
|
61
|
+
docstring = docstring(node)
|
62
|
+
add_offense(docstring, message: message) do |corrector|
|
63
|
+
corrector.replace(docstring, replacement_text(node))
|
64
|
+
end
|
66
65
|
end
|
67
66
|
|
68
67
|
def docstring(node)
|
@@ -78,7 +77,7 @@ module RuboCop
|
|
78
77
|
def replacement_text(node)
|
79
78
|
text = text(node)
|
80
79
|
|
81
|
-
if text
|
80
|
+
if text.match?(SHOULD_PREFIX)
|
82
81
|
RuboCop::RSpec::Wording.new(
|
83
82
|
text,
|
84
83
|
ignore: ignored_words,
|
@@ -16,7 +16,9 @@ module RuboCop
|
|
16
16
|
# expect(pattern).to eq(/foo/)
|
17
17
|
# expect(name).to eq("John")
|
18
18
|
#
|
19
|
-
class ExpectActual <
|
19
|
+
class ExpectActual < Base
|
20
|
+
extend AutoCorrector
|
21
|
+
|
20
22
|
MSG = 'Provide the actual you are testing to `expect(...)`.'
|
21
23
|
|
22
24
|
SIMPLE_LITERALS = %i[
|
@@ -55,17 +57,12 @@ module RuboCop
|
|
55
57
|
PATTERN
|
56
58
|
|
57
59
|
def on_send(node)
|
58
|
-
expect_literal(node) do |
|
59
|
-
add_offense(
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
def autocorrect(node)
|
64
|
-
actual, matcher, expected = expect_literal(node)
|
65
|
-
lambda do |corrector|
|
66
|
-
return unless SUPPORTED_MATCHERS.include?(matcher)
|
60
|
+
expect_literal(node) do |actual, matcher, expected|
|
61
|
+
add_offense(actual.source_range) do |corrector|
|
62
|
+
next unless SUPPORTED_MATCHERS.include?(matcher)
|
67
63
|
|
68
|
-
|
64
|
+
swap(corrector, actual, expected)
|
65
|
+
end
|
69
66
|
end
|
70
67
|
end
|
71
68
|
|
@@ -29,7 +29,8 @@ module RuboCop
|
|
29
29
|
# expect { run }.to change { Foo.bar(:count) }
|
30
30
|
# expect { run }.to change { user.reload.name }
|
31
31
|
#
|
32
|
-
class ExpectChange <
|
32
|
+
class ExpectChange < Base
|
33
|
+
extend AutoCorrector
|
33
34
|
include ConfigurableEnforcedStyle
|
34
35
|
|
35
36
|
MSG_BLOCK = 'Prefer `change(%<obj>s, :%<attr>s)`.'
|
@@ -51,10 +52,11 @@ module RuboCop
|
|
51
52
|
return unless style == :block
|
52
53
|
|
53
54
|
expect_change_with_arguments(node) do |receiver, message|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
msg = format(MSG_CALL, obj: receiver, attr: message)
|
56
|
+
add_offense(node, message: msg) do |corrector|
|
57
|
+
replacement = "change { #{receiver}.#{message} }"
|
58
|
+
corrector.replace(node, replacement)
|
59
|
+
end
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
@@ -62,37 +64,10 @@ module RuboCop
|
|
62
64
|
return unless style == :method_call
|
63
65
|
|
64
66
|
expect_change_with_block(node) do |receiver, message|
|
65
|
-
|
66
|
-
|
67
|
-
message: format(MSG_BLOCK, obj: receiver, attr: message)
|
68
|
-
)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def autocorrect(node)
|
73
|
-
if style == :block
|
74
|
-
autocorrect_method_call_to_block(node)
|
75
|
-
else
|
76
|
-
autocorrect_block_to_method_call(node)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
private
|
81
|
-
|
82
|
-
def autocorrect_method_call_to_block(node)
|
83
|
-
lambda do |corrector|
|
84
|
-
expect_change_with_arguments(node) do |receiver, message|
|
85
|
-
replacement = "change { #{receiver}.#{message} }"
|
86
|
-
corrector.replace(node.loc.expression, replacement)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def autocorrect_block_to_method_call(node)
|
92
|
-
lambda do |corrector|
|
93
|
-
expect_change_with_block(node) do |receiver, message|
|
67
|
+
msg = format(MSG_BLOCK, obj: receiver, attr: message)
|
68
|
+
add_offense(node, message: msg) do |corrector|
|
94
69
|
replacement = "change(#{receiver}, :#{message})"
|
95
|
-
corrector.replace(node
|
70
|
+
corrector.replace(node, replacement)
|
96
71
|
end
|
97
72
|
end
|
98
73
|
end
|
@@ -20,7 +20,7 @@ module RuboCop
|
|
20
20
|
# it do
|
21
21
|
# expect(something).to eq 'foo'
|
22
22
|
# end
|
23
|
-
class ExpectInHook <
|
23
|
+
class ExpectInHook < Base
|
24
24
|
MSG = 'Do not use `%<expect>s` in `%<hook>s` hook'
|
25
25
|
|
26
26
|
def_node_search :expectation, Expectations::ALL.send_pattern
|
@@ -30,8 +30,8 @@ module RuboCop
|
|
30
30
|
return if node.body.nil?
|
31
31
|
|
32
32
|
expectation(node.body) do |expect|
|
33
|
-
add_offense(expect
|
34
|
-
|
33
|
+
add_offense(expect.loc.selector,
|
34
|
+
message: message(expect, node))
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -14,7 +14,7 @@ module RuboCop
|
|
14
14
|
#
|
15
15
|
# # good
|
16
16
|
# expect { my_app.print_report }.to output('Hello World').to_stdout
|
17
|
-
class ExpectOutput <
|
17
|
+
class ExpectOutput < Base
|
18
18
|
MSG = 'Use `expect { ... }.to output(...).to_%<name>s` '\
|
19
19
|
'instead of mutating $%<name>s.'
|
20
20
|
|
@@ -27,7 +27,7 @@ module RuboCop
|
|
27
27
|
name = variable_name[1..-1]
|
28
28
|
return unless name.eql?('stdout') || name.eql?('stderr')
|
29
29
|
|
30
|
-
add_offense(node
|
30
|
+
add_offense(node.loc.name, message: format(MSG, name: name))
|
31
31
|
end
|
32
32
|
|
33
33
|
private
|
@@ -24,36 +24,43 @@ module RuboCop
|
|
24
24
|
#
|
25
25
|
# # good
|
26
26
|
# count { 1 }
|
27
|
-
class AttributeDefinedStatically <
|
27
|
+
class AttributeDefinedStatically < Base
|
28
|
+
extend AutoCorrector
|
29
|
+
|
28
30
|
MSG = 'Use a block to declare attribute values.'
|
29
31
|
|
30
32
|
def_node_matcher :value_matcher, <<-PATTERN
|
31
33
|
(send _ !#reserved_method? $...)
|
32
34
|
PATTERN
|
33
35
|
|
34
|
-
|
36
|
+
def_node_matcher :factory_attributes, <<-PATTERN
|
35
37
|
(block (send _ #attribute_defining_method? ...) _ { (begin $...) $(send ...) } )
|
36
38
|
PATTERN
|
37
39
|
|
38
40
|
def on_block(node)
|
39
|
-
factory_attributes(node)
|
41
|
+
attributes = factory_attributes(node) || []
|
42
|
+
attributes = [attributes] unless attributes.is_a?(Array) # rubocop:disable Style/ArrayCoercion, Lint/RedundantCopDisableDirective
|
43
|
+
|
44
|
+
attributes.each do |attribute|
|
40
45
|
next unless offensive_receiver?(attribute.receiver, node)
|
41
46
|
next if proc?(attribute) || association?(attribute.first_argument)
|
42
47
|
|
43
|
-
add_offense(attribute)
|
48
|
+
add_offense(attribute) do |corrector|
|
49
|
+
autocorrect(corrector, attribute)
|
50
|
+
end
|
44
51
|
end
|
45
52
|
end
|
46
53
|
|
47
|
-
|
54
|
+
private
|
55
|
+
|
56
|
+
def autocorrect(corrector, node)
|
48
57
|
if node.parenthesized?
|
49
|
-
autocorrect_replacing_parens(node)
|
58
|
+
autocorrect_replacing_parens(corrector, node)
|
50
59
|
else
|
51
|
-
autocorrect_without_parens(node)
|
60
|
+
autocorrect_without_parens(corrector, node)
|
52
61
|
end
|
53
62
|
end
|
54
63
|
|
55
|
-
private
|
56
|
-
|
57
64
|
def offensive_receiver?(receiver, node)
|
58
65
|
receiver.nil? ||
|
59
66
|
receiver.self_type? ||
|
@@ -74,24 +81,20 @@ module RuboCop
|
|
74
81
|
|
75
82
|
def_node_matcher :association?, '(hash <(pair (sym :factory) _) ...>)'
|
76
83
|
|
77
|
-
def autocorrect_replacing_parens(node)
|
84
|
+
def autocorrect_replacing_parens(corrector, node)
|
78
85
|
left_braces, right_braces = braces(node)
|
79
86
|
|
80
|
-
|
81
|
-
|
82
|
-
corrector.replace(node.location.end, right_braces)
|
83
|
-
end
|
87
|
+
corrector.replace(node.location.begin, " #{left_braces}")
|
88
|
+
corrector.replace(node.location.end, right_braces)
|
84
89
|
end
|
85
90
|
|
86
|
-
def autocorrect_without_parens(node)
|
91
|
+
def autocorrect_without_parens(corrector, node)
|
87
92
|
left_braces, right_braces = braces(node)
|
88
93
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
corrector.insert_after(expression, right_braces)
|
94
|
-
end
|
94
|
+
argument = node.first_argument
|
95
|
+
expression = argument.location.expression
|
96
|
+
corrector.insert_before(expression, left_braces)
|
97
|
+
corrector.insert_after(expression, right_braces)
|
95
98
|
end
|
96
99
|
|
97
100
|
def braces(node)
|