/
boolean_symbol.rb
50 lines (45 loc) · 1.13 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
# 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 < Cop
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)
add_offense(node, message: format(MSG, boolean: node.value))
end
def autocorrect(node)
lambda do |corrector|
boolean_literal = node.source.delete(':')
parent = node.parent
if parent&.pair_type?
corrector.remove(parent.loc.operator)
boolean_literal = "#{node.source} =>"
end
corrector.replace(node, boolean_literal)
end
end
end
end
end
end