Sha256: 1a92fb7adcca175755c5a190c3f8122b29d6be6636c7cb6e44fe85add4e98208

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Rubocop
  module Cop
    module Style
      # Flags uses of OpenStruct, as it is now officially discouraged
      # to be used for performance, version compatibility, and potential security issues.
      #
      # See also:
      # - https://rubyreferences.github.io/rubychanges/3.0.html#standard-library
      # - https://docs.ruby-lang.org/en/3.0.0/OpenStruct.html#class-OpenStruct-label-Caveats
      # - https://gitlab.com/gitlab-org/gitlab/-/merge_requests/67855
      class OpenStructUse < RuboCop::Cop::Base
        MSG = 'Avoid using `OpenStruct`. It is officially discouraged. ' \
              'Replace it with `Struct`, `Hash`, or RSpec doubles. ' \
              'See https://docs.ruby-lang.org/en/3.0.0/OpenStruct.html#class-OpenStruct-label-Caveats'

        # @!method uses_open_struct?(node)
        def_node_matcher :uses_open_struct?, <<-PATTERN
          (const {nil? (cbase)} :OpenStruct)
        PATTERN

        def on_const(node)
          return unless uses_open_struct?(node)
          return if custom_class_or_module_definition?(node)

          add_offense(node)
        end

        private

        def custom_class_or_module_definition?(node)
          parent = node.parent

          (parent.class_type? || parent.module_type?) && node.left_siblings.empty?
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gitlab-styles-11.0.0 lib/rubocop/cop/style/open_struct_use.rb
gitlab-styles-10.1.0 lib/rubocop/cop/style/open_struct_use.rb
gitlab-styles-10.0.0 lib/rubocop/cop/style/open_struct_use.rb
gitlab-styles-9.2.0 lib/rubocop/cop/style/open_struct_use.rb
gitlab-styles-9.1.0 lib/rubocop/cop/style/open_struct_use.rb
gitlab-styles-9.0.0 lib/rubocop/cop/style/open_struct_use.rb