Sha256: 060cc99b595c12c027c2d7600a387f658eaddad3f2ac57f0402d8fc79d9ae4d5

Contents?: true

Size: 1.62 KB

Versions: 51

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Ensure a require statement is present for a standard library determined
    # by variable library_name
    module RequireLibrary
      extend NodePattern::Macros

      def ensure_required(corrector, node, library_name)
        node = node.parent while node.parent&.parent?

        if node.parent&.begin_type?
          return if @required_libs.include?(library_name)

          remove_subsequent_requires(corrector, node, library_name)
        end

        RequireLibraryCorrector.correct(corrector, node, library_name)
      end

      def remove_subsequent_requires(corrector, node, library_name)
        node.right_siblings.each do |sibling|
          next unless require_library_name?(sibling, library_name)

          range = range_by_whole_lines(sibling.source_range, include_final_newline: true)
          corrector.remove(range)
        end
      end

      def on_send(node)
        return if node.parent&.parent?

        name = require_any_library?(node)
        return if name.nil?

        @required_libs.add(name)
      end

      private

      def on_new_investigation
        # Holds the required files at top-level
        @required_libs = Set.new
        super
      end

      # @!method require_any_library?(node)
      def_node_matcher :require_any_library?, <<~PATTERN
        (send {(const {nil? cbase} :Kernel) nil?} :require (str $_))
      PATTERN

      # @!method require_library_name?(node, library_name)
      def_node_matcher :require_library_name?, <<~PATTERN
        (send {(const {nil? cbase} :Kernel) nil?} :require (str %1))
      PATTERN
    end
  end
end

Version data entries

51 entries across 45 versions & 5 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/require_library.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/require_library.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/require_library.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/require_library.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/require_library.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/require_library.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/require_library.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/require_library.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/require_library.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/require_library.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/require_library.rb
rubocop-1.40.0 lib/rubocop/cop/mixin/require_library.rb
rubocop-1.39.0 lib/rubocop/cop/mixin/require_library.rb
rubocop-1.38.0 lib/rubocop/cop/mixin/require_library.rb
rubocop-1.37.1 lib/rubocop/cop/mixin/require_library.rb
rubocop-1.37.0 lib/rubocop/cop/mixin/require_library.rb
rubocop-1.36.0 lib/rubocop/cop/mixin/require_library.rb
rubocop-1.35.1 lib/rubocop/cop/mixin/require_library.rb
rubocop-1.35.0 lib/rubocop/cop/mixin/require_library.rb
rubocop-1.34.1 lib/rubocop/cop/mixin/require_library.rb