Sha256: ffeba997a0f8a85e2230418e7387ca7f4c3d518ca852144e4f996ac13f7d22ef

Contents?: true

Size: 1.35 KB

Versions: 7

Compression:

Stored size: 1.35 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
        include ConfigurableEnforcedStyle
        include Variable

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

        def on_send(node)
          variable_definition?(node) do |variable|
            if style_violation?(variable)
              add_offense(variable, message: format(MSG, style: style))
            end
          end
        end

        private

        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

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-rspec-2.5.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.4.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.3.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.2.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.1.0 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.0.1 lib/rubocop/cop/rspec/variable_definition.rb
rubocop-rspec-2.0.0 lib/rubocop/cop/rspec/variable_definition.rb