Class: Rubocop::Cop::MethodAndVariableSnakeCase
- Inherits:
-
Cop
- Object
- Parser::AST::Processor
- Cop
- Rubocop::Cop::MethodAndVariableSnakeCase
show all
- Defined in:
- lib/rubocop/cop/method_and_variable_snake_case.rb
Constant Summary
collapse
- MSG =
'Use snake_case for methods and variables.'
- SNAKE_CASE =
/^@?[\da-z_]+[!?=]?$/
- OPERATOR_METHODS =
%w(
| ^ & <=> == === =~ > >= < <= << >>
+ - * / % ** ~ +@ -@ [] []= ` ! != !~
).map(&:to_sym)
Instance Attribute Summary
Attributes inherited from Cop
#debug, #disabled_lines, #offences
Instance Method Summary
collapse
Methods inherited from Cop
#add_offence, cop_name, #has_report?, #ignore_node, inherited, #initialize, #name, #on_comment
Instance Method Details
#inspect(source, tokens, node, comments) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/rubocop/cop/method_and_variable_snake_case.rb', line 15
def inspect(source, tokens, node, )
on_node([:def, :defs, :lvasgn, :ivasgn, :send], node) do |n|
name = case n.type
when :def
name_of_instance_method(n)
when :defs
name_of_singleton_method(n)
when :lvasgn, :ivasgn
name_of_variable(n)
when :send
name_of_setter(n)
end
next unless name
next if name =~ SNAKE_CASE || OPERATOR_METHODS.include?(name)
add_offence(:convention, n.location.line, MSG)
end
end
|
#name_of_instance_method(def_node) ⇒ Object
35
36
37
|
# File 'lib/rubocop/cop/method_and_variable_snake_case.rb', line 35
def name_of_instance_method(def_node)
def_node.children.first
end
|
#name_of_setter(send_node) ⇒ Object
47
48
49
50
51
52
|
# File 'lib/rubocop/cop/method_and_variable_snake_case.rb', line 47
def name_of_setter(send_node)
receiver, method_name = *send_node
return nil unless receiver && receiver.type == :self
return nil unless method_name.to_s.end_with?('=')
method_name
end
|
#name_of_singleton_method(defs_node) ⇒ Object
39
40
41
|
# File 'lib/rubocop/cop/method_and_variable_snake_case.rb', line 39
def name_of_singleton_method(defs_node)
defs_node.children[1]
end
|
#name_of_variable(vasgn_node) ⇒ Object
43
44
45
|
# File 'lib/rubocop/cop/method_and_variable_snake_case.rb', line 43
def name_of_variable(vasgn_node)
vasgn_node.children.first
end
|