Permalink
Browse files

Add cop code

  • Loading branch information...
pocke committed Jun 7, 2016
1 parent 8715375 commit c6485a9c1593bd3cb5aca1ee136df8026945b69d
Showing with 54 additions and 0 deletions.
  1. +54 −0 lib/rubocop/cop/lint/exception_call.rb
@@ -0,0 +1,54 @@
# encoding: utf-8
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
class ExceptionCall < Cop
MSG = 'カンマ(,)を忘れていませんか?'.freeze
def on_send(node)
receiver, method_name, *args = *node
return unless [:raise, :fail].include?(method_name)
return unless check_receiver(receiver)
return unless check_args(args)
add_offense(node, loc(args))
end
def autocorrect(node)
_receiver, _method_name, *args = *node
lambda do |corrector|
corrector.insert_before(loc(args), ',')
end
end
private
def check_receiver(receiver)
return true unless receiver
return true if receiver.const_type? && receiver.const_name == 'Kernel'
return false
end
def check_args(args)
return false unless args.size == 1
arg = args.first
return false unless arg.send_type?
_receiver, method_name, _args = *arg
return method_name =~ /^[A-Z]/
end
def loc(args)
arg = args.first
_receiver, _method_name, inner_args = *arg
end_pos = inner_args.loc.begin.begin_pos
begin_pos = arg.loc.selector.end_pos
Parser::Source::Range.new(arg.loc.expression.source_buffer, begin_pos, end_pos)
end
end
end
end
end

0 comments on commit c6485a9

Please sign in to comment.