Sha256: e17d898cac20b123444c4734f12ea2de35284d5a3f8418db2359dcac2b5820cd

Contents?: true

Size: 892 Bytes

Versions: 2

Compression:

Stored size: 892 Bytes

Contents

require 'simplabs/excellent/checks/base'

module Simplabs

  module Excellent

    module Checks

      class EmptyRescueBodyCheck < Base

        STATEMENT_NODES = [:fcall, :return, :attrasgn, :vcall, :call, :str, :lit]

        def interesting_nodes
          [:resbody]
        end

        def evaluate(node)
          add_error('Rescue block is empty.', {}, -1) unless has_statement?(node)
        end

        private

          def has_statement?(node)
            return true if STATEMENT_NODES.include?(node.node_type)
            return true if assigning_other_than_exception_to_local_variable?(node) 
            return true if node.children.any? { |child| has_statement?(child) }
          end

          def assigning_other_than_exception_to_local_variable?(node)
            node.node_type == :lasgn && node[2].to_a != [:gvar, :$!]
          end

      end

    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simplabs-excellent-1.0.0 lib/simplabs/excellent/checks/empty_rescue_body_check.rb
simplabs-excellent-1.0.1 lib/simplabs/excellent/checks/empty_rescue_body_check.rb