Skip to content

Commit 6a1aea7

Browse files
committed
JS: Avoid scanning individual comment lines to find generated code markers
Some subclasses of GeneratedCodeMarkerComment regex match against `getLine(_)`. When evaluated, this results in multiple scans (one per subclass that uses it) of all comment lines in the database, before regex matching against those lines. To make these scans smaller, regex match against the entire comment text without splitting them into lines. This is achieved using `?m` (multiline) and line boundaries in the regexes.
1 parent c9a8723 commit 6a1aea7

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

javascript/ql/lib/semmle/javascript/GeneratedCode.qll

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ private predicate codeGeneratorMarkerComment(Comment c, string tool) {
4949
*/
5050
private class GenericGeneratedCodeMarkerComment extends GeneratedCodeMarkerComment {
5151
GenericGeneratedCodeMarkerComment() {
52-
exists(string line | line = getLine(_) |
53-
exists(string entity, string was, string automatically |
54-
entity = "code|file|class|interface|art[ei]fact|module|script" and
55-
was = "was|is|has been" and
56-
automatically = "automatically |mechanically |auto[- ]?" and
57-
line.regexpMatch("(?i).*\\b(This|The following) (" + entity + ") (" + was + ") (" +
58-
automatically + ")?gener(e?)ated\\b.*")
59-
)
52+
exists(string entity, string was, string automatically |
53+
entity = "code|file|class|interface|art[ei]fact|module|script" and
54+
was = "was|is|has been" and
55+
automatically = "automatically |mechanically |auto[- ]?" and
56+
// Look for this pattern in each line of the comment.
57+
this.getText()
58+
.regexpMatch("(?im)^.*\\b(This|The following) (" + entity + ") (" + was + ") (" +
59+
automatically + ")?gener(e?)ated\\b.*$")
6060
)
6161
}
6262
}
@@ -66,9 +66,14 @@ private class GenericGeneratedCodeMarkerComment extends GeneratedCodeMarkerComme
6666
*/
6767
private class DontModifyMarkerComment extends GeneratedCodeMarkerComment {
6868
DontModifyMarkerComment() {
69-
exists(string line | line = getLine(_) |
70-
line.regexpMatch("(?i).*\\bGenerated by\\b.*\\bDo not edit\\b.*") or
71-
line.regexpMatch("(?i).*\\bAny modifications to this file will be lost\\b.*")
69+
exists(string pattern |
70+
// Look for these patterns in each line of the comment.
71+
this.getText().regexpMatch(pattern) and
72+
pattern =
73+
[
74+
"(?im)^.*\\bGenerated by\\b.*\\bDo not edit\\b.*$",
75+
"(?im)^.*\\bAny modifications to this file will be lost\\b.*$"
76+
]
7277
)
7378
}
7479
}

0 commit comments

Comments
 (0)