-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
boolean_symbol.rb
58 lines (49 loc) · 1.34 KB
/
boolean_symbol.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
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for `:true` and `:false` symbols.
# In most cases it would be a typo.
#
# @example
#
# # bad
# :true
#
# # good
# true
#
# @example
#
# # bad
# :false
#
# # good
# false
class BooleanSymbol < Base
extend AutoCorrector
MSG = 'Symbol with a boolean name - ' \
'you probably meant to use `%<boolean>s`.'
def_node_matcher :boolean_symbol?, '(sym {:true :false})'
def on_sym(node)
return unless boolean_symbol?(node)
parent = node.parent
return if parent&.array_type? && parent&.percent_literal?(:symbol)
add_offense(node, message: format(MSG, boolean: node.value)) do |corrector|
autocorrect(corrector, node)
end
end
private
def autocorrect(corrector, node)
boolean_literal = node.source.delete(':')
parent = node.parent
if parent&.pair_type? && node.equal?(parent.children[0])
corrector.remove(parent.loc.operator)
boolean_literal = "#{node.source} =>"
end
corrector.replace(node, boolean_literal)
end
end
end
end
end