Sha256: 43cff5f87b6fcee4b874ded92315dd680fe203749a9352a89eb9d8c380821492

Contents?: true

Size: 1.87 KB

Versions: 24

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks that memoized helpers names are symbols or strings.
      #
      # @example EnforcedStyle: symbols (default)
      #   # bad
      #   subject('user') { create_user }
      #   let('user_name') { 'Adam' }
      #
      #   # good
      #   subject(:user) { create_user }
      #   let(:user_name) { 'Adam' }
      #
      # @example EnforcedStyle: strings
      #   # bad
      #   subject(:user) { create_user }
      #   let(:user_name) { 'Adam' }
      #
      #   # good
      #   subject('user') { create_user }
      #   let('user_name') { 'Adam' }
      #
      class VariableDefinition < Base
        extend AutoCorrector
        include ConfigurableEnforcedStyle
        include Variable
        include InsideExampleGroup

        MSG = 'Use %<style>s for variable names.'

        def on_send(node)
          return unless inside_example_group?(node)

          variable_definition?(node) do |variable|
            next unless style_offense?(variable)

            add_offense(
              variable,
              message: format(MSG, style: style)
            ) do |corrector|
              corrector.replace(variable, correct_variable(variable))
            end
          end
        end

        private

        def correct_variable(variable)
          case variable.type
          when :dsym
            variable.source[1..]
          when :sym
            variable.value.to_s.inspect
          else
            variable.value.to_sym.inspect
          end
        end

        def style_offense?(variable)
          (style == :symbols && string?(variable)) ||
            (style == :strings && symbol?(variable))
        end

        def string?(node)
          node.str_type?
        end

        def symbol?(node)
          node.sym_type? || node.dsym_type?
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/variable_definition.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/variable_definition.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/variable_definition.rb