Sha256: bb149938c3bb8ffba4fbff512bbb44ce51a6b258115f59b5f5f42c0fdfb087f5

Contents?: true

Size: 1.95 KB

Versions: 13

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Do not define constants within a block, since the block's scope does not
      # isolate or namespace the constant in any way.
      #
      # If you are trying to define that constant once, define it outside of
      # the block instead, or use a variable or method if defining the constant
      # in the outer scope would be problematic.
      #
      # For meta-programming, use `const_set`.
      #
      # @example
      #   # bad
      #   task :lint do
      #     FILES_TO_LINT = Dir['lib/*.rb']
      #   end
      #
      #   # bad
      #   describe 'making a request' do
      #     class TestRequest; end
      #   end
      #
      #   # bad
      #   module M
      #     extend ActiveSupport::Concern
      #     included do
      #       LIST = []
      #     end
      #   end
      #
      #   # good
      #   task :lint do
      #     files_to_lint = Dir['lib/*.rb']
      #   end
      #
      #   # good
      #   describe 'making a request' do
      #     let(:test_request) { Class.new }
      #     # see also `stub_const` for RSpec
      #   end
      #
      #   # good
      #   module M
      #     extend ActiveSupport::Concern
      #     included do
      #       const_set(:LIST, [])
      #     end
      #   end
      class ConstantDefinitionInBlock < Base
        MSG = 'Do not define constants this way within a block.'

        def_node_matcher :constant_assigned_in_block?, <<~PATTERN
          ({^block_type? [^begin_type? ^^block_type?]} nil? ...)
        PATTERN

        def_node_matcher :module_defined_in_block?, <<~PATTERN
          ({^block_type? [^begin_type? ^^block_type?]} ...)
        PATTERN

        def on_casgn(node)
          add_offense(node) if constant_assigned_in_block?(node)
        end

        def on_class(node)
          add_offense(node) if module_defined_in_block?(node)
        end
        alias on_module on_class
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/constant_definition_in_block.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/constant_definition_in_block.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/constant_definition_in_block.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/constant_definition_in_block.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/constant_definition_in_block.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/constant_definition_in_block.rb
rubocop-1.2.0 lib/rubocop/cop/lint/constant_definition_in_block.rb
rubocop-1.1.0 lib/rubocop/cop/lint/constant_definition_in_block.rb
rubocop-1.0.0 lib/rubocop/cop/lint/constant_definition_in_block.rb
rubocop-0.93.1 lib/rubocop/cop/lint/constant_definition_in_block.rb
rubocop-0.93.0 lib/rubocop/cop/lint/constant_definition_in_block.rb
rubocop-0.92.0 lib/rubocop/cop/lint/constant_definition_in_block.rb
rubocop-0.91.1 lib/rubocop/cop/lint/constant_definition_in_block.rb