Merge pull request #1527 from esben-semmle/js/classify-more-generated-and-tests

Approved by asger-semmle
This commit is contained in:
semmle-qlci
2019-07-02 07:38:10 +01:00
committed by GitHub
9 changed files with 53 additions and 2 deletions

View File

@@ -61,7 +61,14 @@ private predicate looksLikeExterns(TopLevel tl) {
predicate classify(File f, string category) {
isGenerated(f.getATopLevel()) and category = "generated"
or
exists(Test t | t.getFile() = f | category = "test")
(
exists(Test t | t.getFile() = f)
or
exists(string stemExt | stemExt = "test" or stemExt = "spec" |
f = getTestFile(any(File orig), stemExt)
)
) and
category = "test"
or
(f.getATopLevel().isExterns() or looksLikeExterns(f.getATopLevel())) and
category = "externs"

View File

@@ -154,6 +154,13 @@ private int countStartingHtmlElements(File f, int l) {
result = strictcount(getAStartingElement(f, l))
}
/**
* Holds if the base name of `f` is a number followed by a single extension.
*/
predicate isGeneratedFileName(File f) {
f.getStem().regexpMatch("[0-9]+")
}
/**
* Holds if `tl` looks like it contains generated code.
*/
@@ -166,7 +173,8 @@ predicate isGenerated(TopLevel tl) {
hasManyInvocations(tl) or
isData(tl.getFile()) or
isJsonLine(tl.getFile()) or
isGeneratedHtml(tl.getFile())
isGeneratedHtml(tl.getFile()) or
isGeneratedFileName(tl.getFile())
}
/**

View File

@@ -38,6 +38,34 @@ class BDDTest extends Test, @callexpr {
}
}
/**
* Gets the test file for `f` with stem extension `stemExt`.
* That is, a file named file named `<base>.<stemExt>.<ext>` in the
* same directory as `f` which is named `<base>.<ext>`.
*/
bindingset[stemExt]
File getTestFile(File f, string stemExt) {
result = f.getParentContainer().getFile(f.getStem() + "." + stemExt + "." + f.getExtension())
}
/**
* A Jest test, that is, an invocation of a global function named
* `test` where the first argument is a string and the second
* argument is a function. Additionally, the invocation happens in a file
* named `<base>.test.<ext>` in the same directory as a file named
* `<base>.<ext>`.
*/
class JestTest extends Test, @callexpr {
JestTest() {
exists(CallExpr call | call = this |
call.getCallee().(GlobalVarAccess).getName() = "test" and
exists(call.getArgument(0).getStringValue()) and
call.getArgument(1).analyze().getAValue() instanceof AbstractFunction
) and
getFile() = getTestFile(any(File f), "test")
}
}
/**
* A xUnit.js fact, that is, a function annotated with an xUnit.js
* `Fact` annotation.

View File

@@ -0,0 +1 @@
var x = 42;

View File

@@ -0,0 +1 @@
var x = 42;

View File

@@ -1,3 +1,4 @@
| 1.js:0:0:0:0 | 1.js | generated |
| AutoRest.js:0:0:0:0 | AutoRest.js | generated |
| ManyElementsOnLine.html:0:0:0:0 | ManyElementsOnLine.html | generated |
| ai.1.2.3-build0123.js:0:0:0:0 | ai.1.2.3-build0123.js | library |
@@ -6,6 +7,8 @@
| etherpad.html:0:0:0:0 | etherpad.html | generated |
| exported-data.js:0:0:0:0 | exported-data.js | generated |
| htmltidy.html:0:0:0:0 | htmltidy.html | generated |
| implementation.spec.js:0:0:0:0 | implementation.spec.js | test |
| implementation.test.js:0:0:0:0 | implementation.test.js | test |
| jison-lex.js:0:0:0:0 | jison-lex.js | generated |
| jison.js:0:0:0:0 | jison.js | generated |
| jquery-datatables.js:0:0:0:0 | jquery-datatables.js | library |

View File

@@ -0,0 +1 @@
var x = 42;

View File

@@ -0,0 +1 @@
test("this should work", function(){});

View File

@@ -0,0 +1 @@
test("this should work", function(){});