-
Notifications
You must be signed in to change notification settings - Fork 715
/
file_type_detector.rb
64 lines (54 loc) · 1.25 KB
/
file_type_detector.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
59
60
61
62
63
64
module Brakeman
class FileTypeDetector < BaseProcessor
def initialize
super(nil)
reset
end
def detect_type(file)
reset
process(file.ast)
if @file_type.nil?
@file_type = guess_from_path(file.path.relative)
end
@file_type || :libs
end
MODEL_CLASSES = [
:'ActiveRecord::Base',
:ApplicationRecord
]
def process_class exp
name = class_name(exp.class_name)
parent = class_name(exp.parent_name)
if name.match(/Controller$/)
@file_type = :controllers
return exp
elsif MODEL_CLASSES.include? parent
@file_type = :models
return exp
end
super
end
def guess_from_path path
case
when path.include?('app/models')
:models
when path.include?('app/controllers')
:controllers
when path.include?('config/initializers')
:initializers
when path.include?('lib/')
:libs
when path.match?(%r{config/environments/(?!production\.rb)$})
:skip
when path.match?(%r{environments/production\.rb$})
:skip
when path.match?(%r{application\.rb$})
:skip
end
end
private
def reset
@file_type = nil
end
end
end