dsl-fixes/simple_fixes.rb
# Tries to statically analyse class of local variable
Tries to statically analyse class of local variable
Tries to statically analyse class of local variable
def fix_class_reference
    d "Searching for local var #{name}"
    parent = self.parent
    until parent.is_a? DefNode
      if parent.nil?
        return self
      end
      for child in parent.children
        if child.is_a? AssignLocalVarNode and child.name == name
          d "  Found local var #{name}"
          @cls = child.value.cls
          d "  guessed class: #{cls.inspect}"
          return self
        end
        if child.is_a? EachNode and child.argument_names.include?(name)
          d "  Found each loop var #{name}"
          @cls = child.target.guess_each_class
          d "  guessed class: #{cls.inspect}"
          return self
        end
      end
      parent = parent.parent
    end
    parent.expect_class DefNode
    for argument in parent.arguments.children
      if argument.name == name
        d "  Found argument var #{name}"
        @cls = argument.cls
        d "  guessed class: #{cls.inspect}"
        return self
      end
    end
    return self
  end
def fix_class_reference d "Searching for local var #{name}" parent = self . parent until parent . is_a? DefNode if parent . nil? return self end for child in parent . children if child . is_a? AssignLocalVarNode and child . name == name d "  Found local var #{name}" @cls = child . value . cls d "  guessed class: #{cls.inspect}" return self end if child . is_a? EachNode and child . argument_names . include? ( name ) d "  Found each loop var #{name}" @cls = child . target . guess_each_class d "  guessed class: #{cls.inspect}" return self end end parent = parent . parent end parent . expect_class DefNode for argument in parent . arguments . children if argument . name == name d "  Found argument var #{name}" @cls = argument . cls d "  guessed class: #{cls.inspect}" return self end end return self end
Tries to statically analyse class of local variable
Tries to statically analyse class of local variable
# until we run of the current def # (note that the statements are inside StatementListNode so we can test this on the beginning of the loop) # Normal local variable #    x = 5 # Local variable in the for-each loop #    for x in [1, 2, 3] # Local variable from the method argument list