Skip to content

Commit 71c86fa

Browse files
authored
Merge pull request #1527 from esben-semmle/js/classify-more-generated-and-tests
Approved by asger-semmle
2 parents 26fd1b9 + 062778b commit 71c86fa

File tree

9 files changed

+53
-2
lines changed

9 files changed

+53
-2
lines changed

javascript/ql/src/filters/ClassifyFiles.ql

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ private predicate looksLikeExterns(TopLevel tl) {
6161
predicate classify(File f, string category) {
6262
isGenerated(f.getATopLevel()) and category = "generated"
6363
or
64-
exists(Test t | t.getFile() = f | category = "test")
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+
) and
71+
category = "test"
6572
or
6673
(f.getATopLevel().isExterns() or looksLikeExterns(f.getATopLevel())) and
6774
category = "externs"

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ private int countStartingHtmlElements(File f, int l) {
154154
result = strictcount(getAStartingElement(f, l))
155155
}
156156

157+
/**
158+
* Holds if the base name of `f` is a number followed by a single extension.
159+
*/
160+
predicate isGeneratedFileName(File f) {
161+
f.getStem().regexpMatch("[0-9]+")
162+
}
163+
157164
/**
158165
* Holds if `tl` looks like it contains generated code.
159166
*/
@@ -166,7 +173,8 @@ predicate isGenerated(TopLevel tl) {
166173
hasManyInvocations(tl) or
167174
isData(tl.getFile()) or
168175
isJsonLine(tl.getFile()) or
169-
isGeneratedHtml(tl.getFile())
176+
isGeneratedHtml(tl.getFile()) or
177+
isGeneratedFileName(tl.getFile())
170178
}
171179

172180
/**

javascript/ql/src/semmle/javascript/frameworks/Testing.qll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ class BDDTest extends Test, @callexpr {
3838
}
3939
}
4040

41+
/**
42+
* Gets the test file for `f` with stem extension `stemExt`.
43+
* That is, a file named file named `<base>.<stemExt>.<ext>` in the
44+
* same directory as `f` which is named `<base>.<ext>`.
45+
*/
46+
bindingset[stemExt]
47+
File getTestFile(File f, string stemExt) {
48+
result = f.getParentContainer().getFile(f.getStem() + "." + stemExt + "." + f.getExtension())
49+
}
50+
51+
/**
52+
* A Jest test, that is, an invocation of a global function named
53+
* `test` where the first argument is a string and the second
54+
* argument is a function. Additionally, the invocation happens in a file
55+
* named `<base>.test.<ext>` in the same directory as a file named
56+
* `<base>.<ext>`.
57+
*/
58+
class JestTest extends Test, @callexpr {
59+
JestTest() {
60+
exists(CallExpr call | call = this |
61+
call.getCallee().(GlobalVarAccess).getName() = "test" and
62+
exists(call.getArgument(0).getStringValue()) and
63+
call.getArgument(1).analyze().getAValue() instanceof AbstractFunction
64+
) and
65+
getFile() = getTestFile(any(File f), "test")
66+
}
67+
}
68+
4169
/**
4270
* A xUnit.js fact, that is, a function annotated with an xUnit.js
4371
* `Fact` annotation.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var x = 42;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var x = 42;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| 1.js:0:0:0:0 | 1.js | generated |
12
| AutoRest.js:0:0:0:0 | AutoRest.js | generated |
23
| ManyElementsOnLine.html:0:0:0:0 | ManyElementsOnLine.html | generated |
34
| ai.1.2.3-build0123.js:0:0:0:0 | ai.1.2.3-build0123.js | library |
@@ -6,6 +7,8 @@
67
| etherpad.html:0:0:0:0 | etherpad.html | generated |
78
| exported-data.js:0:0:0:0 | exported-data.js | generated |
89
| htmltidy.html:0:0:0:0 | htmltidy.html | generated |
10+
| implementation.spec.js:0:0:0:0 | implementation.spec.js | test |
11+
| implementation.test.js:0:0:0:0 | implementation.test.js | test |
912
| jison-lex.js:0:0:0:0 | jison-lex.js | generated |
1013
| jison.js:0:0:0:0 | jison.js | generated |
1114
| jquery-datatables.js:0:0:0:0 | jquery-datatables.js | library |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var x = 42;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test("this should work", function(){});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test("this should work", function(){});

0 commit comments

Comments
 (0)