Module: Brakeman::ModuleHelper

Included in:
ControllerProcessor, LibraryProcessor, ModelProcessor
Defined in:
lib/brakeman/processors/lib/module_helper.rb

Instance Method Summary collapse

Instance Method Details

#handle_class(exp, collection, tracker_class) ⇒ Object



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
59
60
61
62
63
64
65
# File 'lib/brakeman/processors/lib/module_helper.rb', line 33

def handle_class exp, collection, tracker_class
  name = class_name(exp.class_name)
  parent = class_name exp.parent_name

  if @current_class
    outer_class = @current_class
    name = (outer_class.name.to_s + "::" + name.to_s).to_sym
  end

  if @current_module
    name = (@current_module.name.to_s + "::" + name.to_s).to_sym
  end

  if collection[name]
    @current_class = collection[name]
    @current_class.add_file @current_file, exp
  else
    @current_class = tracker_class.new name, parent, @current_file, exp, @tracker
    collection[name] = @current_class
  end

  exp.body = process_all! exp.body

  yield if block_given?

  if outer_class
    @current_class = outer_class
  else
    @current_class = nil
  end

  exp
end

#handle_module(exp, tracker_class, parent = nil) ⇒ Object



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
# File 'lib/brakeman/processors/lib/module_helper.rb', line 2

def handle_module exp, tracker_class, parent = nil
  name = class_name(exp.module_name)

  if @current_module
    outer_module = @current_module
    name = (outer_module.name.to_s + "::" + name.to_s).to_sym
  end

  if @current_class
    name = (@current_class.name.to_s + "::" + name.to_s).to_sym
  end

  if @tracker.libs[name]
    @current_module = @tracker.libs[name]
    @current_module.add_file @current_file, exp
  else
    @current_module = tracker_class.new name, parent, @current_file, exp, @tracker
    @tracker.libs[name] = @current_module
  end

  exp.body = process_all! exp.body

  if outer_module
    @current_module = outer_module
  else
    @current_module = nil
  end

  exp
end

#make_defs(exp) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/brakeman/processors/lib/module_helper.rb', line 132

def make_defs exp
  # 'What if' there was some crazy code that had a
  # defs inside a def inside an sclass? :|
  return exp if node_type? exp, :defs

  raise "Unexpected node type: #{exp.node_type}" unless node_type? exp, :defn

  Sexp.new(:defs, s(:self), exp.method_name, exp.formal_args, *exp.body).line(exp.line)
end

#process_defn(exp) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/brakeman/processors/lib/module_helper.rb', line 98

def process_defn exp
  name = exp.method_name

  @current_method = name

  if @inside_sclass
    res = Sexp.new :defs, s(:self), name, exp.formal_args, *process_all!(exp.body)
  else
    res = Sexp.new :defn, name, exp.formal_args, *process_all!(exp.body)
  end

  res.line(exp.line)
  @current_method = nil

  if @current_class
    @current_class.add_method @visibility, name, res, @current_file
  elsif @current_module
    @current_module.add_method @visibility, name, res, @current_file
  end

  res
end

#process_defs(exp) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/brakeman/processors/lib/module_helper.rb', line 67

def process_defs exp
  name = exp.method_name

  if node_type? exp[1], :self
    if @current_class
      target = @current_class.name
    elsif @current_module
      target = @current_module.name
    else
      target = nil
    end
  else
    target = class_name exp[1]
  end

  @current_method = name
  res = Sexp.new :defs, target, name, exp.formal_args, *process_all!(exp.body)
  res.line(exp.line)
  @current_method = nil

  # TODO: if target is not self/nil, then
  # the method should be added to `target`, not current class

  if @current_class
    @current_class.add_method @visibility, name, res, @current_file
  elsif @current_module
    @current_module.add_method @visibility, name, res, @current_file
  end
  res
end

#process_sclass(exp) ⇒ Object

class << self



122
123
124
125
126
127
128
129
130
# File 'lib/brakeman/processors/lib/module_helper.rb', line 122

def process_sclass exp
  @inside_sclass = true

  process_all! exp

  exp
ensure
  @inside_sclass = false
end