Sha256: 233ba8abc9e88ff4e16d6a0feae93932efefb862f41cde2653a99bccfb4831f9

Contents?: true

Size: 1.84 KB

Versions: 151

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for the use of `YAML.load`, `YAML.safe_load`, and `YAML.parse` with
      # `File.read` argument.
      #
      # NOTE: `YAML.safe_load_file` was introduced in Ruby 3.0.
      #
      # @example
      #
      #   # bad
      #   YAML.load(File.read(path))
      #   YAML.parse(File.read(path))
      #
      #   # good
      #   YAML.load_file(path)
      #   YAML.parse_file(path)
      #
      #   # bad
      #   YAML.safe_load(File.read(path)) # Ruby 3.0 and newer
      #
      #   # good
      #   YAML.safe_load_file(path)       # Ruby 3.0 and newer
      #
      class YAMLFileRead < Base
        extend AutoCorrector

        MSG = 'Use `%<prefer>s` instead.'
        RESTRICT_ON_SEND = %i[load safe_load parse].freeze

        # @!method yaml_file_read?(node)
        def_node_matcher :yaml_file_read?, <<~PATTERN
          (send
            (const {cbase nil?} :YAML) _
            (send
              (const {cbase nil?} :File) :read $_) $...)
        PATTERN

        def on_send(node)
          return if node.method?(:safe_load) && target_ruby_version <= 2.7
          return unless (file_path, rest_arguments = yaml_file_read?(node))

          range = offense_range(node)
          rest_arguments = if rest_arguments.empty?
                             ''
                           else
                             ", #{rest_arguments.map(&:source).join(', ')}"
                           end
          prefer = "#{node.method_name}_file(#{file_path.source}#{rest_arguments})"

          add_offense(range, message: format(MSG, prefer: prefer)) do |corrector|
            corrector.replace(range, prefer)
          end
        end

        private

        def offense_range(node)
          node.loc.selector.join(node.source_range.end)
        end
      end
    end
  end
end

Version data entries

151 entries across 151 versions & 15 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.73.2 lib/rubocop/cop/style/yaml_file_read.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.73.1 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.73.0 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.72.2 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.72.1 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.72.0 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.71.2 lib/rubocop/cop/style/yaml_file_read.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.71.1 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.71.0 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.70.0 lib/rubocop/cop/style/yaml_file_read.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.69.2 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.69.1 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.69.0 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.68.0 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.67.0 lib/rubocop/cop/style/yaml_file_read.rb
rubocop-1.66.1 lib/rubocop/cop/style/yaml_file_read.rb