Skip to content

Commit fa9e9dd

Browse files
committed
split out predicates in ClassifyFiles to avoid unnecessary computations
1 parent 3d124cf commit fa9e9dd

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

javascript/ql/lib/semmle/javascript/filters/ClassifyFiles.qll

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,49 @@ private predicate looksLikeExterns(TopLevel tl) {
4545
)
4646
}
4747

48+
/**
49+
* Holds if `f` contains generated or minified code.
50+
*/
51+
predicate isGeneratedCodeFile(File f) { isGenerated(f.getATopLevel()) }
52+
53+
/**
54+
* Holds if `f` contains test code.
55+
*/
56+
predicate isTestFile(File f) {
57+
exists(Test t | t.getFile() = f)
58+
or
59+
exists(string stemExt | stemExt = "test" or stemExt = "spec" |
60+
f = getTestFile(any(File orig), stemExt)
61+
)
62+
or
63+
f.getAbsolutePath().regexpMatch(".*/__(mocks|tests)__/.*")
64+
}
65+
66+
/**
67+
* Holds if `f` contains externs declarations.
68+
*/
69+
predicate isExternsFile(File f) {
70+
(f.getATopLevel().isExterns() or looksLikeExterns(f.getATopLevel()))
71+
}
72+
73+
/**
74+
* Holds if `f` contains library code.
75+
*/
76+
predicate isLibaryFile(File f) { f.getATopLevel() instanceof FrameworkLibraryInstance }
77+
78+
/**
79+
* Holds if `f` contains template code.
80+
*/
81+
predicate isTemplateFile(File f) {
82+
exists(JSParseError err | maybeCausedByTemplate(err) | f = err.getFile())
83+
or
84+
// Polymer templates
85+
exists(HTML::Element elt | elt.getName() = "template" |
86+
f = elt.getFile() and
87+
not f.getExtension() = "vue"
88+
)
89+
}
90+
4891
/**
4992
* Holds if `f` is classified as belonging to `category`.
5093
*
@@ -55,33 +98,15 @@ private predicate looksLikeExterns(TopLevel tl) {
5598
* - `"library"`: `f` contains library code;
5699
* - `"template"`: `f` contains template code.
57100
*/
101+
pragma[inline]
58102
predicate classify(File f, string category) {
59-
isGenerated(f.getATopLevel()) and category = "generated"
60-
or
61-
(
62-
exists(Test t | t.getFile() = f)
63-
or
64-
exists(string stemExt | stemExt = "test" or stemExt = "spec" |
65-
f = getTestFile(any(File orig), stemExt)
66-
)
67-
or
68-
f.getAbsolutePath().regexpMatch(".*/__(mocks|tests)__/.*")
69-
) and
70-
category = "test"
103+
isGeneratedCodeFile(f) and category = "generated"
71104
or
72-
(f.getATopLevel().isExterns() or looksLikeExterns(f.getATopLevel())) and
73-
category = "externs"
105+
isTestFile(f) and category = "test"
74106
or
75-
f.getATopLevel() instanceof FrameworkLibraryInstance and category = "library"
107+
isExternsFile(f) and category = "externs"
76108
or
77-
exists(JSParseError err | maybeCausedByTemplate(err) |
78-
f = err.getFile() and category = "template"
79-
)
109+
isLibaryFile(f) and category = "library"
80110
or
81-
// Polymer templates
82-
exists(HTML::Element elt | elt.getName() = "template" |
83-
f = elt.getFile() and
84-
category = "template" and
85-
not f.getExtension() = "vue"
86-
)
111+
isTemplateFile(f) and category = "template"
87112
}

javascript/ql/lib/semmle/javascript/security/dataflow/PrototypePollutingAssignmentQuery.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private DataFlow::SourceNode prototypeLessObject(DataFlow::TypeTracker t) {
116116
* Objects created in such files are ignored in the `prototypeLessObject` predicate.
117117
*/
118118
private class TestFile extends File {
119-
TestFile() { ClassifyFiles::classify(this, "test") }
119+
TestFile() { ClassifyFiles::isTestFile(this) }
120120
}
121121

122122
/** Holds if `Object.prototype` has a member named `prop`. */

0 commit comments

Comments
 (0)