forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
described_class.rb
65 lines (55 loc) · 1.72 KB
/
described_class.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
# encoding: utf-8
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# If the first argument of describe is a class, the class is exposed to
# each example via described_class - this should be used instead of
# repeating the class.
#
# @example
# # bad
# describe MyClass do
# subject { MyClass.do_something }
# end
#
# # good
# describe MyClass do
# subject { described_class.do_something }
# end
class DescribedClass < Cop
include RuboCop::RSpec::TopLevelDescribe
MESSAGE = 'Use `described_class` instead of `%s`'
def on_block(node)
method, _args, body = *node
return unless top_level_describe?(method)
_receiver, method_name, object = *method
return unless method_name == :describe
return unless object && object.type == :const
inspect_children(body, object)
end
def autocorrect(node)
lambda do |corrector|
corrector.replace(node.loc.expression, 'described_class')
end
end
private
def inspect_children(node, object)
return unless node.is_a? Parser::AST::Node
return if scope_change?(node) || node.type == :const
node.children.each do |child|
if child == object
name = object.loc.expression.source
add_offense(child, :expression, format(MESSAGE, name))
break
end
inspect_children(child, object)
end
end
def scope_change?(node)
[:def, :class, :module].include?(node.type)
end
end
end
end
end