Sha256: c78bd112be054e0f2db3edeba8eba828b68ac1032b0bb485edda5adc0b393bee
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
# encoding: utf-8 # frozen_string_literal: true module RuboCop module Cop module Performance # This cop identifies places where a case-insensitive string comparison # can better be implemented using `casecmp`. # # @example # @bad # 'abc'.downcase == 'ABC' # 'abc'.upcase.eql? 'ABC' # # @good # 'abc'.casecmp('ABC') class Casecmp < Cop MSG = 'Use `casecmp` instead of `%s %s`.'.freeze def_node_matcher :downcase_eq, <<-END (send $(send _ ${:downcase :upcase}) ${:== :eql?} _) END def on_send(node) downcase_eq(node) do |send_downcase, case_method, eq_method| range = node.loc.selector.join(send_downcase.loc.selector) add_offense(node, range, format(MSG, case_method, eq_method)) end end def autocorrect(node) receiver, _method, arg = *node range = Parser::Source::Range.new(node.source_range.source_buffer, receiver.loc.selector.begin_pos, arg.loc.begin.begin_pos) lambda do |corrector| # we want resulting call to be parenthesized # if arg already includes one or more sets of parens, don't add more # or if method call already used parens, again, don't add more if arg.loc.begin.source == '(' corrector.replace(range, 'casecmp') elsif range.source =~ /\(/ corrector.replace(range, 'casecmp(') else corrector.replace(range, 'casecmp(') corrector.insert_after(arg.source_range, ')') end end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rubocop-0.36.0 | lib/rubocop/cop/performance/casecmp.rb |