Sha256: 070dbdf36ab19e55b6e46fd389096b839fc86823caabfda043538cc0a96da279
Contents?: true
Size: 1.38 KB
Versions: 1
Compression:
Stored size: 1.38 KB
Contents
# encoding: utf-8 module Rubocop module Cop module Style # This cop checks for uses of *and* and *or*. class AndOr < Cop MSG = 'Use `%s` instead of `%s`.' OPS = { 'and' => '&&', 'or' => '||' } def on_and(node) process_logical_op(node) end def on_or(node) process_logical_op(node) end private def process_logical_op(node) op = node.loc.operator.source op_type = node.type.to_s if op == op_type add_offense(node, :operator, format(MSG, OPS[op], op)) end end def autocorrect(node) c = correction(node) new_source = rewrite_node(node, c) # Make the correction only if it doesn't change the AST. @corrections << c if node == SourceParser.parse(new_source).ast end def rewrite_node(node, correction) processed_source = SourceParser.parse(node.loc.expression.source) c = correction(processed_source.ast) Corrector.new(processed_source.buffer, [c]).rewrite end def correction(node) lambda do |corrector| replacement = (node.type == :and ? '&&' : '||') corrector.replace(node.loc.operator, replacement) end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rubocop-0.20.0 | lib/rubocop/cop/style/and_or.rb |