Sha256: 5c6c7022ceec7bd5c33cfadc0f47ae771fc7b5fc38d4963431cfbdd69e1dc70b
Contents?: true
Size: 1.77 KB
Versions: 6
Compression:
Stored size: 1.77 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 MSG = 'Use %<style>s for variable names.' def on_send(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
6 entries across 6 versions & 2 rubygems