lib/pelusa/lint/collection_wrappers.rb in pelusa-0.2.2 vs lib/pelusa/lint/collection_wrappers.rb in pelusa-0.2.3
- old
+ new
@@ -4,11 +4,10 @@
def initialize
@violations = Set.new
end
def check(klass)
- initialize
iterate_lines!(klass)
return SuccessfulAnalysis.new(name) if @violations.empty?
FailedAnalysis.new(name, @violations) do |violations|
@@ -21,30 +20,38 @@
def name
"Doesn't mix array instance variables with others"
end
def iterate_lines!(klass)
- array_assignment = nil
+ # the array of actual assignment nodes -- we only want to assign once
+ array_assignments = {}
+ # the name of all the assigned arrays, no others allowed
+ array_values = {}
- get_array_assignment = Iterator.new do |node|
+ ClassAnalyzer.walk(klass) do |node|
if node.is_a?(Rubinius::AST::InstanceVariableAssignment) &&
(node.value.is_a?(Rubinius::AST::ArrayLiteral) ||
- node.value.is_a?(Rubinius::AST::EmptyArray))
- array_assignment = node
+ node.value.is_a?(Rubinius::AST::EmptyArray))
+ array_assignments[node] = true
+ array_values[node.name] = true
end
end
- iterator = Iterator.new do |node|
- next if node == array_assignment || !array_assignment
-
- if node.is_a?(Rubinius::AST::InstanceVariableAssignment)
- @violations << node.line
- elsif node.is_a?(Rubinius::AST::InstanceVariableAccess) && node.name != array_assignment.name
- @violations << node.line
+ unless array_assignments.empty?
+ ClassAnalyzer.walk(klass) do |node|
+ # if this is where we assign the node for the first time, good
+ unless array_assignments[node]
+ # otherwise, if it's an instance variable assignment, verboten!
+ if node.is_a?(Rubinius::AST::InstanceVariableAssignment)
+ @violations << node.line
+ # or if we access any other ivars
+ elsif node.is_a?(Rubinius::AST::InstanceVariableAccess) &&
+ !array_values[node.name]
+ @violations << node.line
+ end
+ end
end
end
- Array(klass).each(&get_array_assignment)
- Array(klass).each(&iterator)
end
end
end
end