Skip to content

Commit 29f9103

Browse files
author
Esben Sparre Andreasen
committed
JS: classify single-line JSON files as generated
1 parent 7790ac4 commit 29f9103

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ private predicate isData(File f) {
112112
)
113113
}
114114

115+
/**
116+
* Holds if `f` is a single line that looks like a non-trivial amount of JSON data, which is often a sign of generated data code.
117+
*/
118+
private predicate isJsonLine(File f) {
119+
f.getNumberOfLines() = 1 and
120+
count(Expr e | e.getFile() = f) > 100 and
121+
forall(Expr e | e.getFile() = f |
122+
e instanceof ObjectExpr or
123+
e instanceof ArrayExpr or
124+
e instanceof NumberLiteral or
125+
e instanceof StringLiteral or
126+
e instanceof BooleanLiteral
127+
)
128+
}
129+
115130
/**
116131
* Holds if `f` is a generated HTML file.
117132
*/
@@ -132,7 +147,6 @@ private HTML::Element getAStartingElement(File f, int l) {
132147
result.getFile() = f and result.getLocation().getStartLine() = l
133148
}
134149

135-
136150
/**
137151
* Gets the number of HTML elements that start at line `l` in file `f`.
138152
*/
@@ -151,6 +165,7 @@ predicate isGenerated(TopLevel tl) {
151165
exists(GeneratedCodeMarkerComment gcmc | tl = gcmc.getTopLevel()) or
152166
hasManyInvocations(tl) or
153167
isData(tl.getFile()) or
168+
isJsonLine(tl.getFile()) or
154169
isGeneratedHtml(tl.getFile())
155170
}
156171

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| jquery-datatables.js:0:0:0:0 | jquery-datatables.js | library |
1212
| jquery-jstree.js:0:0:0:0 | jquery-jstree.js | library |
1313
| jquery-snippet.js:0:0:0:0 | jquery-snippet.js | library |
14+
| json-like.js:0:0:0:0 | json-like.js | generated |
1415
| jsx-old.js:0:0:0:0 | jsx-old.js | generated |
1516
| jsx.js:0:0:0:0 | jsx.js | generated |
1617
| multi-part-bundle.html:0:0:0:0 | multi-part-bundle.html | generated |

javascript/ql/test/query-tests/filters/ClassifyFiles/json-like.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)