Sha256: 87862f8329835fbefb4e25a0117baa79ce19fe940328bb7c29fac3bd984c4099
Contents?: true
Size: 1.76 KB
Versions: 1
Compression:
Stored size: 1.76 KB
Contents
# frozen_string_literal: true require_relative 'smell_detector' require_relative 'smell_warning' module Reek module Smells # # The +InstanceVariableAssumption+ class is responsible for # detecting directly access of instance variables in a class # that does not define them in its initialize method. # class InstanceVariableAssumption < SmellDetector def self.contexts [:class] end # Checks +klass+ for instance # variables assumptions. # # @return [Array<SmellWarning>] # def sniff(ctx) method_expressions = ctx.node_instance_methods assumptions = (variables_from_context(method_expressions) - variables_from_initialize(method_expressions)).uniq assumptions.map do |assumption| build_smell_warning(ctx, assumption) end end private def build_smell_warning(ctx, assumption) message = "assumes too much for instance variable '#{assumption}'" smell_warning( context: ctx, lines: [ctx.exp.line], message: message, parameters: { assumption: assumption.to_s }) end # :reek:UtilityFunction def variables_from_initialize(instance_methods) initialize_exp = instance_methods.detect do |method| method.name == :initialize end return [] unless initialize_exp initialize_exp.each_node(:ivasgn).map(&:name) end def variables_from_context(instance_methods) instance_methods.map do |method| method.find_nodes(assumption_nodes, ignored_nodes).map(&:name) end.flatten end def assumption_nodes [:ivar] end def ignored_nodes [:or_asgn] end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
reek-4.4.2 | lib/reek/smells/instance_variable_assumption.rb |