forked from Shopify/theme-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredundant_comparison.rb
35 lines (27 loc) · 1 KB
/
redundant_comparison.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
# frozen_string_literal: true
module ThemeCheck
# Suggest replace { x == true } and { x != false } with just { x }
class RedundantComparison < LiquidCheck
include LiquidHelper
severity :style
category :liquid
def on_condition(node)
return unless standard_condition?(node.value) && redundant_comparison?(node.value)
ancestor = non_condition_ancestor(node)
condition_markup = recover_single_condition_markup(node.value)
markup = ancestor.markup.include?(condition_markup) ? condition_markup : nil
add_offense('Apparent redundant boolean comparison with true or false', node: ancestor, markup: markup)
end
private
def redundant_comparison?(condition)
case condition.operator
when '==' then condition.right == true # ironic
when '!=' then condition.right == false
else false
end
end
def non_condition_ancestor(node)
node.value.is_a?(Liquid::Condition) ? non_condition_ancestor(node.parent) : node
end
end
end