forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
redundant_let_rubocop_config_new.rb
65 lines (54 loc) · 1.92 KB
/
redundant_let_rubocop_config_new.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
module RuboCop
module Cop
module InternalAffairs
# Checks that `let` is `RuboCop::Config.new` with no arguments.
#
# @example
# # bad
# RSpec.describe RuboCop::Cop::Department::Foo, :config do
# let(:config) { RuboCop::Config.new }
# end
#
# # good
# RSpec.describe RuboCop::Cop::Department::Foo, :config do
# end
#
# RSpec.describe RuboCop::Cop::Department::Foo, :config do
# let(:config) { RuboCop::Config.new(argument) }
# end
#
class RedundantLetRuboCopConfigNew < Base
include RangeHelp
extend AutoCorrector
MSG = 'Remove `let` that is `RuboCop::Config.new` with no arguments%<additional_message>s.'
# @!method let_rubocop_config_new?(node)
def_node_matcher :let_rubocop_config_new?, <<~PATTERN
(block
(send nil? :let
(sym :config))
(args)
(send
(const
(const nil? :RuboCop) :Config) :new))
PATTERN
def on_block(node)
return unless let_rubocop_config_new?(node)
describe = find_describe_method_node(node)
unless (exist_config = describe.last_argument.source == ':config')
additional_message = ' and specify `:config` in `describe`'
end
message = format(MSG, additional_message: additional_message)
add_offense(node, message: message) do |corrector|
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
corrector.insert_after(describe.last_argument, ', :config') unless exist_config
end
end
private
def find_describe_method_node(block_node)
block_node.ancestors.find { |node| node.block_type? && node.method?(:describe) }.send_node
end
end
end
end
end