Sha256: 78bb548c7b573b96fcf701faa0476234d58bce4942c4bef014c513eac1e313ec

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Gitlab
  module Styles
    module Rubocop
      module Cop
        module Style
          # This cop 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::Cop
            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'

            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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gitlab-styles-7.1.0 lib/gitlab/styles/rubocop/cop/style/open_struct_use.rb
gitlab-styles-7.0.0 lib/gitlab/styles/rubocop/cop/style/open_struct_use.rb
gitlab-styles-6.6.0 lib/gitlab/styles/rubocop/cop/style/open_struct_use.rb
gitlab-styles-6.5.0 lib/gitlab/styles/rubocop/cop/style/open_struct_use.rb
gitlab-styles-6.4.0 lib/gitlab/styles/rubocop/cop/style/open_struct_use.rb