/
language.rb
187 lines (152 loc) · 5.11 KB
/
language.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# frozen_string_literal: true
module RuboCop
module RSpec
# Contains node matchers for common RSpec DSL.
#
# RSpec allows for configuring aliases for commonly used DSL elements, e.g.
# example groups and hooks. It is possible to configure RuboCop RSpec to
# be able to properly detect these elements in the `RSpec/Language` section
# of the RuboCop YAML configuration file.
#
# In addition to providing useful matchers, this class is responsible for
# using the configured aliases.
module Language
extend RuboCop::NodePattern::Macros
extend NodePattern
class << self
attr_accessor :config
end
def_node_matcher :rspec?, '{(const {nil? cbase} :RSpec) nil?}'
def_node_matcher :example_group?, block_pattern('#ExampleGroups.all')
def_node_matcher :shared_group?, block_pattern('#SharedGroups.all')
def_node_matcher :spec_group?,
block_pattern('{#SharedGroups.all #ExampleGroups.all}')
def_node_matcher :example_group_with_body?, <<-PATTERN
(block #{send_pattern('#ExampleGroups.all')} args !nil?)
PATTERN
def_node_matcher :example?, block_pattern('#Examples.all')
def_node_matcher :hook?, block_pattern('#Hooks.all')
def_node_matcher :let?, <<-PATTERN
{
#{block_pattern('#Helpers.all')}
(send #rspec? #Helpers.all _ block_pass)
}
PATTERN
def_node_matcher :include?, <<-PATTERN
{
#{send_pattern('#Includes.all')}
#{block_pattern('#Includes.all')}
}
PATTERN
def_node_matcher :subject?, block_pattern('#Subjects.all')
module ExampleGroups # :nodoc:
class << self
def all(element)
regular(element) ||
skipped(element) ||
focused(element)
end
def regular(element)
Language.config['ExampleGroups']['Regular'].include?(element.to_s)
end
def focused(element)
Language.config['ExampleGroups']['Focused'].include?(element.to_s)
end
def skipped(element)
Language.config['ExampleGroups']['Skipped'].include?(element.to_s)
end
end
end
module Examples # :nodoc:
class << self
def all(element)
regular(element) ||
focused(element) ||
skipped(element) ||
pending(element)
end
def regular(element)
Language.config['Examples']['Regular'].include?(element.to_s)
end
def focused(element)
Language.config['Examples']['Focused'].include?(element.to_s)
end
def skipped(element)
Language.config['Examples']['Skipped'].include?(element.to_s)
end
def pending(element)
Language.config['Examples']['Pending'].include?(element.to_s)
end
end
end
module Expectations # :nodoc:
def self.all(element)
Language.config['Expectations'].include?(element.to_s)
end
end
module Helpers # :nodoc:
def self.all(element)
Language.config['Helpers'].include?(element.to_s)
end
end
module Hooks # :nodoc:
def self.all(element)
Language.config['Hooks'].include?(element.to_s)
end
end
module HookScopes # :nodoc:
def self.all(element)
Language.config['HookScopes'].include?(element.to_s)
end
end
module Includes # :nodoc:
class << self
def all(element)
examples(element) ||
context(element)
end
def examples(element)
Language.config['Includes']['Examples'].include?(element.to_s)
end
def context(element)
Language.config['Includes']['Context'].include?(element.to_s)
end
end
end
module Runners # :nodoc:
def self.all(element)
Language.config['Runners'].include?(element.to_s)
end
end
module SharedGroups # :nodoc:
class << self
def all(element)
examples(element) ||
context(element)
end
def examples(element)
Language.config['SharedGroups']['Examples'].include?(element.to_s)
end
def context(element)
Language.config['SharedGroups']['Context'].include?(element.to_s)
end
end
end
module Subjects # :nodoc:
def self.all(element)
Language.config['Subjects'].include?(element.to_s)
end
end
# This is used in Dialect and DescribeClass cops to detect RSpec blocks.
module ALL # :nodoc:
def self.all(element)
[ExampleGroups, Examples, Expectations, Helpers, Hooks, Includes,
Runners, SharedGroups, Subjects]
.find { |concept| concept.all(element) }
end
end
private_constant :ExampleGroups, :Examples, :Expectations, :Hooks,
:Includes, :Runners, :SharedGroups, :ALL
end
end
end