Sha256: 92db2e65e147b8b73cc1ca390ed84ee9da464ce87a5396167cb65c9d516c185d

Contents?: true

Size: 1.87 KB

Versions: 12

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_violation?(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_violation?(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

12 entries across 12 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/variable_definition.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/variable_definition.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/variable_definition.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/variable_definition.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/variable_definition.rb