lib/ripper/extract_constants.rb in grape-reload-0.0.3 vs lib/ripper/extract_constants.rb in grape-reload-0.0.4
- old
+ new
@@ -51,11 +51,11 @@
else
(variants << const_ary.dup).last.unshift(@namespace) unless @namespace.nil?
end
variants << [ const_ary ]
- @used << variants.map{|v| v.join('::')}
+ @used << (variants.map{|v| v.join('::')} << '::' + const)
end
else
@used << const
end
end
@@ -105,11 +105,11 @@
used = self.used.reject {|variants| !variants.find{|v| result[:declared].include?(v) }.nil? }
if namespace.nil?
used = used.map {|variants| variants.map{|v| (v.start_with?('::') ? '' : (namespace || '') + '::') + v }}
end
- result[:used] = result[:used].concat(used)
+ result[:used] = result[:used].concat(used).uniq
result
end
end
@@ -133,11 +133,11 @@
if node_ary.first.kind_of?(Symbol)
load(node_ary)
else
# Code position for identifier
return if node_ary.kind_of?(Array) and (node_ary.size == 2) and node_ary[0].kind_of?(Integer) and node_ary[1].kind_of?(Integer)
- node_ary.map{|n| load(n) }
+ node_ary.map{|n| node_for(n) }
end
end
end
def load(node)
new(*node[1..-1])
@@ -155,11 +155,11 @@
@body.each{|e|
case e
when ASTEntity
e.collect_constants(result, context || (TraversingContext.new)) unless e.nil?
when Array
- e.map{|e| e.collect_constants(result, context || (TraversingContext.new)) unless e.nil? }
+ e.flatten.map{|e| e.collect_constants(result, context || (TraversingContext.new)) unless e.nil? }
else
end
} unless @body.nil?
result
end
@@ -170,11 +170,32 @@
def initialize(*args)
@body = args.first.map{|a| ASTEntity.node_for(a)}
end
end
+class ASTDef < ASTEntity
+ def self.ripper_id; :def end
+ def collect_constants(result, context)
+ result
+ end
+end
+class ASTCommand < ASTEntity
+ def self.ripper_id; :command end
+ def initialize(*args)
+ @command = args.first[1]
+ super(*args)
+ end
+ def collect_constants(result, context)
+ @old_stop_collect_constants = context[:stop_collect_constants]
+ context[:stop_collect_constants] = nil unless %w{desc mount params}.index(@command).nil?
+ ret = super(result, context)
+ context[:stop_collect_constants] = @old_stop_collect_constants
+ ret
+ end
+end
+
class ASTBody < ASTEntity
def self.ripper_id; :bodystmt end
def initialize(*args)
@body = args.first.map{ |node| ASTEntity.node_for(node) }
end
@@ -205,12 +226,13 @@
class ASTTopConstRef < ASTEntity
def self.ripper_id; :top_const_ref end
def collect_constants(result, context)
context[:top] = true
- super(result, context)
+ ret = super(result, context)
context[:top] = false
+ ret
end
end
class ASTArgsAddBlock < ASTEntity
def self.ripper_id; :args_add_block end
@@ -237,10 +259,11 @@
def self.ripper_id; :'@const' end
def initialize(*args)
@const_name = args[0]
end
def collect_constants(result, context)
+ return super(result, context) if context[:stop_collect_constants]
if context[:variable_assignment]
result.declare_const(@const_name)
else
analyze_const = context[:analyze_const].nil? ? true : context[:analyze_const]
if context[:top]
@@ -260,11 +283,12 @@
def initialize(*args)
@path = ASTEntity.node_for(args.first)
@const = ASTEntity.node_for(args.last)
end
def collect_constants(result, context)
- if context[:const_path_ref]
+ return super(result, context) if context[:stop_collect_constants]
+ if context[:const_path_ref] || context[:method_add_arg]
r = TraversingResult.new
c = context.dup
c[:analyze_const] = false
path_consts = @path.collect_constants(r, context)
const = @const.collect_constants(r, context)
@@ -278,10 +302,52 @@
end
result
end
end
+class ASTMethodAddArg < ASTEntity
+ def self.ripper_id; :method_add_arg end
+ def initialize(*args)
+ @path = ASTEntity.node_for(args.first)
+ end
+
+ def collect_constants(result, context)
+ return super(result, context) if context[:stop_collect_constants]
+ if context[:method_add_arg]
+ r = TraversingResult.new
+ c = context.dup
+ c[:analyze_const] = false
+ path_consts = @path.collect_constants(r, context)
+ result.use_const(path_consts.used.join('::'), false)
+ else
+ r = TraversingResult.new
+ new_context = TraversingContext.new([], {method_add_arg: true, analyze_const: false})
+ path_consts = @path.collect_constants(r, new_context)
+ result.use_const(path_consts.used.join('::'))
+ end
+ result
+ end
+end
+
+class ASTDefs < ASTEntity
+ def self.ripper_id; :defs end
+ def collect_constants(result, context)
+ result
+ end
+end
+
+class ASTMethodAddBlock < ASTEntity
+ def self.ripper_id; :method_add_block end
+
+ def collect_constants(result, context)
+ context[:stop_collect_constants] = true
+ ret = super(result, context)
+ context[:stop_collect_constants] = nil
+ ret
+ end
+end
+
class ASTModule < ASTEntity
def self.ripper_id; :module end
def initialize(*args)
@module_name = args.find{|a| a.first == :const_ref}.last[1]
@body = args.find{|a| a.first == :bodystmt}[1].map{|node|
@@ -298,12 +364,13 @@
class ASTVarField < ASTEntity
def self.ripper_id; :var_field end
def collect_constants(result, context)
context[:variable_assignment] = true
- super(result, context)
+ ret = super(result, context)
context[:variable_assignment] = false
+ ret
end
end
class ASTRef < ASTEntity
def self.ripper_id; :var_ref end
@@ -311,9 +378,15 @@
context[:variable_assignment] = false
super(result, context)
end
end
+class ASTLambda < ASTEntity
+ def self.ripper_id; :lambda end
+ def initialize(*args)
+ super(*(args[0..-2]))
+ end
+end
class Ripper
def self.extract_constants(code)
ast = Ripper.sexp(code)
result = ASTEntity.node_for(ast).collect_constants(TraversingResult.new)