Sha256: ac83782103594170f712f10b10a9fb751e7b2c618614ea53d5f1df3280c709a9

Contents?: true

Size: 1.85 KB

Versions: 67

Compression:

Stored size: 1.85 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.
      #
      # @safety
      #
      #   Note that this cop may flag false positives; for instance, the following legal
      #   use of a hand-rolled `OpenStruct` type would be considered an offense:
      #
      #   ```
      #   module MyNamespace
      #     class OpenStruct # not the OpenStruct we're looking for
      #     end
      #
      #     def new_struct
      #       OpenStruct.new # resolves to MyNamespace::OpenStruct
      #     end
      #   end
      #   ```
      #
      # @example
      #
      #   # bad
      #   point = OpenStruct.new(x: 0, y: 1)
      #
      #   # good
      #   Point = Struct.new(:x, :y)
      #   point = Point.new(0, 1)
      #
      #   # also good
      #   point = { x: 0, y: 1 }
      #
      #   # bad
      #   test_double = OpenStruct.new(a: 'b')
      #
      #   # good (assumes test using rspec-mocks)
      #   test_double = double
      #   allow(test_double).to receive(:a).and_return('b')
      #
      class OpenStructUse < Base
        MSG = 'Avoid using `OpenStruct`; use `Struct`, `Hash`, a class or test doubles instead.'

        # @!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

67 entries across 60 versions & 10 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/open_struct_use.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/open_struct_use.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/open_struct_use.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/open_struct_use.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/style/open_struct_use.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/open_struct_use.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/open_struct_use.rb
synctera_ruby_sdk-1.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/open_struct_use.rb
synctera_ruby_sdk-1.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/open_struct_use.rb
synctera_ruby_sdk-1.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/open_struct_use.rb
tursodb-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/open_struct_use.rb
synctera_ruby_sdk-1.0.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/open_struct_use.rb
rubocop-1.56.0 lib/rubocop/cop/style/open_struct_use.rb
rubocop-1.55.1 lib/rubocop/cop/style/open_struct_use.rb
rubocop-1.55.0 lib/rubocop/cop/style/open_struct_use.rb
rubocop-1.54.2 lib/rubocop/cop/style/open_struct_use.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/style/open_struct_use.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/style/open_struct_use.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/style/open_struct_use.rb
rubocop-1.54.1 lib/rubocop/cop/style/open_struct_use.rb