Skip to content

Commit 02d5630

Browse files
author
Esben Sparre Andreasen
committed
JS: classify generated data files
1 parent 61bd003 commit 02d5630

File tree

4 files changed

+2424
-1
lines changed

4 files changed

+2424
-1
lines changed

javascript/ql/src/semmle/javascript/GeneratedCode.qll

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ private predicate hasManyInvocations(TopLevel tl) {
9999
)
100100
}
101101

102+
/**
103+
* Holds if `f` is side effect free, and full of primitive literals, which is often a sign of generated data code.
104+
*/
105+
private predicate isData(File f) {
106+
// heuristic: `f` has more than 1000 primitive literal expressions ...
107+
count(SyntacticConstants::PrimitiveLiteralConstant e | e.getFile() = f) > 1000 and
108+
// ... but no expressions with side effects ...
109+
not exists (Expr e |
110+
e.getFile() = f and
111+
e.isImpure() and
112+
// ... except for variable initializers
113+
not e instanceof VariableDeclarator
114+
)
115+
}
116+
102117
/**
103118
* Holds if `tl` looks like it contains generated code.
104119
*/
@@ -108,7 +123,8 @@ predicate isGenerated(TopLevel tl) {
108123
tl instanceof GWTGeneratedTopLevel or
109124
tl instanceof DartGeneratedTopLevel or
110125
exists (GeneratedCodeMarkerComment gcmc | tl = gcmc.getTopLevel()) or
111-
hasManyInvocations(tl)
126+
hasManyInvocations(tl) or
127+
isData(tl.getFile())
112128
}
113129

114130
/**

javascript/ql/test/query-tests/filters/ClassifyFiles/ClassifyFiles.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
| ai.1.2.3-build0123.js:0:0:0:0 | ai.1.2.3-build0123.js | library |
22
| bundle-directive.js:0:0:0:0 | bundle-directive.js | generated |
3+
| data.js:0:0:0:0 | data.js | generated |
4+
| exported-data.js:0:0:0:0 | exported-data.js | generated |
35
| jison-lex.js:0:0:0:0 | jison-lex.js | generated |
46
| jison.js:0:0:0:0 | jison.js | generated |
57
| jquery-datatables.js:0:0:0:0 | jquery-datatables.js | library |

0 commit comments

Comments
 (0)