forked from troessner/reek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_source.rb
58 lines (52 loc) · 1.42 KB
/
object_source.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Reek
class Source
#
# Factory method: creates a +Source+ from obj.
# The code is not parsed until +report+ is called.
# (This feature is only enabled if you have the ParseTree gem installed.)
#
def self.from_object(obj)
return ObjectSource.new(obj, obj.to_s)
end
end
class ObjectSource < Source # :nodoc:
def self.unify(sexp) # :nodoc:
unifier = Unifier.new
unifier.processors.each do |proc|
proc.unsupported.delete :cfunc # HACK
end
return unifier.process(sexp[0])
end
def initialize(code, desc) # :nodoc:
super
@sniffer.disable(LargeClass)
end
def can_parse_objects?
return true if Object.const_defined?(:ParseTree)
begin
require 'parse_tree'
true
rescue LoadError
false
end
end
def generate_syntax_tree
if can_parse_objects?
ObjectSource.unify(ParseTree.new.parse_tree(@source))
else
throw ArgumentError.new('You must install the ParseTree gem to use this feature')
end
end
end
end
class Object
#
# Constructs a Source representing this object; the source can then be used
# to generate an abstract syntax tree for the object, which can in turn then
# be examined for code smells.
# (This feature is only enabled if you have the ParseTree gem installed.)
#
def to_source
Reek::Source.from_object(self)
end
end