/
variable_definition.rb
56 lines (49 loc) · 1.35 KB
/
variable_definition.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
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks that memoized helpers names are symbols or strings.
#
# @example EnforcedStyle: symbols (default)
# # bad
# subject('user') { create_user }
# let('user_name') { 'Adam' }
#
# # good
# subject(:user) { create_user }
# let(:user_name) { 'Adam' }
#
# @example EnforcedStyle: strings
# # bad
# subject(:user) { create_user }
# let(:user_name) { 'Adam' }
#
# # good
# subject('user') { create_user }
# let('user_name') { 'Adam' }
class VariableDefinition < Base
include ConfigurableEnforcedStyle
include Variable
MSG = 'Use %<style>s for variable names.'
def on_send(node)
variable_definition?(node) do |variable|
if style_violation?(variable)
add_offense(variable, message: format(MSG, style: style))
end
end
end
private
def style_violation?(variable)
style == :symbols && string?(variable) ||
style == :strings && symbol?(variable)
end
def string?(node)
node.str_type?
end
def symbol?(node)
node.sym_type? || node.dsym_type?
end
end
end
end
end