FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql_ruby/lib/codeql_ruby/extractor_file.rb at main · agius/codeql_ruby · GitHub
agius
codeql_ruby
Repository navigation
Code
Issues
Pull requests
1
(1)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codeql_ruby
/
lib
/
codeql_ruby
/
extractor_file.rb
Copy path
More file actions
More file actions
Latest commit
History
History
History
91 lines (71 loc) · 2.31 KB
Breadcrumbs
codeql_ruby
/
lib
/
codeql_ruby
/
extractor_file.rb
Copy path
File metadata and controls
91 lines (71 loc) · 2.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
65
66
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
require
'forwardable'
require
'pathname'
module
CodeqlRuby
class
ExtractorFile
extend
Forwardable
# allow ExtractorFile to be sorted and unique'd based on filepath string
def_delegator
:@filepath
,
:hash
attr_reader
:filepath
,
:source_kind
# these should be kept in sync with files#fromSource in ruby.dbscheme
SOURCE_KINDS
=
{
unknown
:
0
,
source
:
1
,
library
:
2
}
.
freeze
def
self
.
source_kinds
SOURCE_KINDS
end
def
initialize
(
filepath
,
source_kind
:
nil
)
@filepath
=
filepath
@source_kind
=
source_kind
||
:source
end
# allow ExtractorFile to be sorted and unique'd based on filepath string
def
eql?
(
other
)
@filepath
.
eql?
(
other
)
end
def
contents
@contents
||=
File
.
read
(
filepath
)
end
def
trapfile_name
@trapfile_name
||=
"
#{
File
.
basename
(
filepath
,
'.rb'
)
}
.trap"
end
def
structure
@structure
||=
begin
Node
.
new
(
Ripper
.
sexp
(
contents
)
)
end
end
# https://youtu.be/b8m9zhNAgKs?t=44
def
to_trap
trapout
=
[
]
idx
=
10_000
folders
=
Pathname
.
new
(
filepath
)
.
ascend
.
to_a
.
reverse
[
0
..-
2
]
prev
=
nil
folders
.
each
do
|
paf
|
simple
=
File
.
basename
(
paf
)
trapout
<<
"#
#{
idx
}
=@
\"
#{
paf
}
;folder
\"
"
trapout
<<
"folders(#
#{
idx
}
,
\"
#{
paf
}
\"
,
\"
#{
simple
}
\"
)"
trapout
<<
"containerparent(#
#{
prev
}
,#
#{
idx
}
)"
if
prev
prev
=
idx
idx
+=
1
end
file_ref
=
idx
trapout
<<
"#
#{
file_ref
}
=@
\"
#{
filepath
}
;sourcefile
\"
"
basename
=
File
.
basename
(
filepath
,
'.*'
)
extname
=
File
.
extname
(
filepath
)
extname
=
extname
[
1
..-
1
]
if
extname
.
start_with?
(
'.'
)
src_kind
=
SOURCE_KINDS
[
source_kind
]
trapout
<<
"files(#
#{
file_ref
}
,
\"
#{
filepath
}
\"
,
\"
#{
basename
}
\"
,
\"
#{
extname
}
\"
,
#{
src_kind
}
)"
trapout
<<
"containerparent(#
#{
prev
}
, #
#{
file_ref
}
)"
idx
+=
1
loc_ref
=
idx
trapout
<<
"#
#{
loc_ref
}
=@
\"
loc,{#
#{
file_ref
}
},0,0,0,0
\"
"
trapout
<<
"locations_default(#
#{
loc_ref
}
, #
#{
file_ref
}
, 0, 0, 0, 0)"
visitor
=
Visitor
.
new
(
idx
,
file_ref
)
visitor
.
visit
(
structure
)
trapout
+=
visitor
.
trap_entries
num_lines
=
contents
.
count
(
"
\n
"
)
trapout
<<
"numlines(#
#{
prev
}
,
#{
num_lines
}
,
#{
visitor
.
num_code
}
,
#{
visitor
.
num_comment
}
)"
trapout
.
join
(
"
\n
"
)
end
end
end
Back
|
FazBrowse Home
|
New Git URL