Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec73c97

Browse files
committed
JS: refactor ClassifyFiles.qll from ClassifyFiles.ql
1 parent 1ecfa2e commit ec73c97

2 files changed

Lines changed: 87 additions & 80 deletions

File tree

javascript/ql/src/filters/ClassifyFiles.ql

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,86 +6,8 @@
66
* @kind file-classifier
77
* @id js/file-classifier
88
*/
9-
10-
import semmle.javascript.GeneratedCode
11-
import semmle.javascript.frameworks.Testing
12-
import semmle.javascript.frameworks.Templating
13-
import semmle.javascript.dependencies.FrameworkLibraries
14-
15-
/**
16-
* Holds if `e` may be caused by parsing a template file as plain HTML or JavaScript.
17-
*
18-
* We use two heuristics: check for the presence of a known template delimiter preceding
19-
* the error on the same line, and check whether the file name contains `template` or
20-
* `templates`.
21-
*/
22-
predicate maybeCausedByTemplate(JSParseError e) {
23-
exists(File f | f = e.getFile() |
24-
e.getLine().indexOf(Templating::getADelimiter()) <= e.getLocation().getStartColumn()
25-
or
26-
f.getAbsolutePath().regexpMatch("(?i).*\\btemplates?\\b.*")
27-
)
28-
}
29-
30-
/**
31-
* Holds if `e` is an expression in the form `o.p1.p2.p3....pn`.
32-
*/
33-
private predicate isNestedDotExpr(DotExpr e) {
34-
e.getBase() instanceof VarAccess or
35-
isNestedDotExpr(e.getBase())
36-
}
37-
38-
/**
39-
* Holds if `tl` only contains variable declarations and field reads.
40-
*/
41-
private predicate looksLikeExterns(TopLevel tl) {
42-
forex(Stmt s | s.getParent() = tl |
43-
exists(VarDeclStmt vds | vds = s |
44-
forall(VariableDeclarator vd | vd = vds.getADecl() | not exists(vd.getInit()))
45-
)
46-
or
47-
isNestedDotExpr(s.(ExprStmt).getExpr())
48-
)
49-
}
50-
51-
/**
52-
* Holds if `f` is classified as belonging to `category`.
53-
*
54-
* There are currently four categories:
55-
* - `"generated"`: `f` contains generated or minified code;
56-
* - `"test"`: `f` contains test code;
57-
* - `"externs"`: `f` contains externs declarations;
58-
* - `"library"`: `f` contains library code;
59-
* - `"template"`: `f` contains template code.
60-
*/
61-
predicate classify(File f, string category) {
62-
isGenerated(f.getATopLevel()) and category = "generated"
63-
or
64-
(
65-
exists(Test t | t.getFile() = f)
66-
or
67-
exists(string stemExt | stemExt = "test" or stemExt = "spec" |
68-
f = getTestFile(any(File orig), stemExt)
69-
)
70-
or
71-
f.getAbsolutePath().regexpMatch(".*/__(mocks|tests)__/.*")
72-
) and
73-
category = "test"
74-
or
75-
(f.getATopLevel().isExterns() or looksLikeExterns(f.getATopLevel())) and
76-
category = "externs"
77-
or
78-
f.getATopLevel() instanceof FrameworkLibraryInstance and category = "library"
79-
or
80-
exists(JSParseError err | maybeCausedByTemplate(err) |
81-
f = err.getFile() and category = "template"
82-
)
83-
or
84-
// Polymer templates
85-
exists(HTML::Element elt | elt.getName() = "template" |
86-
f = elt.getFile() and category = "template"
87-
)
88-
}
9+
import javascript
10+
import ClassifyFiles
8911

9012
from File f, string category
9113
where classify(f, category)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Provides classes and predicates for classifying files as containing
3+
* generated code, test code, externs declarations, library code or
4+
* template code.
5+
*/
6+
7+
import semmle.javascript.GeneratedCode
8+
import semmle.javascript.frameworks.Testing
9+
import semmle.javascript.frameworks.Templating
10+
import semmle.javascript.dependencies.FrameworkLibraries
11+
12+
/**
13+
* Holds if `e` may be caused by parsing a template file as plain HTML or JavaScript.
14+
*
15+
* We use two heuristics: check for the presence of a known template delimiter preceding
16+
* the error on the same line, and check whether the file name contains `template` or
17+
* `templates`.
18+
*/
19+
predicate maybeCausedByTemplate(JSParseError e) {
20+
exists(File f | f = e.getFile() |
21+
e.getLine().indexOf(Templating::getADelimiter()) <= e.getLocation().getStartColumn()
22+
or
23+
f.getAbsolutePath().regexpMatch("(?i).*\\btemplates?\\b.*")
24+
)
25+
}
26+
27+
/**
28+
* Holds if `e` is an expression in the form `o.p1.p2.p3....pn`.
29+
*/
30+
private predicate isNestedDotExpr(DotExpr e) {
31+
e.getBase() instanceof VarAccess or
32+
isNestedDotExpr(e.getBase())
33+
}
34+
35+
/**
36+
* Holds if `tl` only contains variable declarations and field reads.
37+
*/
38+
private predicate looksLikeExterns(TopLevel tl) {
39+
forex(Stmt s | s.getParent() = tl |
40+
exists(VarDeclStmt vds | vds = s |
41+
forall(VariableDeclarator vd | vd = vds.getADecl() | not exists(vd.getInit()))
42+
)
43+
or
44+
isNestedDotExpr(s.(ExprStmt).getExpr())
45+
)
46+
}
47+
48+
/**
49+
* Holds if `f` is classified as belonging to `category`.
50+
*
51+
* There are currently four categories:
52+
* - `"generated"`: `f` contains generated or minified code;
53+
* - `"test"`: `f` contains test code;
54+
* - `"externs"`: `f` contains externs declarations;
55+
* - `"library"`: `f` contains library code;
56+
* - `"template"`: `f` contains template code.
57+
*/
58+
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"
71+
or
72+
(f.getATopLevel().isExterns() or looksLikeExterns(f.getATopLevel())) and
73+
category = "externs"
74+
or
75+
f.getATopLevel() instanceof FrameworkLibraryInstance and category = "library"
76+
or
77+
exists(JSParseError err | maybeCausedByTemplate(err) |
78+
f = err.getFile() and category = "template"
79+
)
80+
or
81+
// Polymer templates
82+
exists(HTML::Element elt | elt.getName() = "template" |
83+
f = elt.getFile() and category = "template"
84+
)
85+
}

0 commit comments

Comments
 (0)