Javascript: Autoformat.

This commit is contained in:
Anders Schack-Mulligen
2019-01-11 11:02:42 +01:00
parent 28261d6787
commit e58094c732
472 changed files with 1723 additions and 1834 deletions

View File

@@ -3,10 +3,7 @@
import semmle.javascript.Files
/** Gets the relative path of `file`, with backslashes replaced by forward slashes. */
private
string relativePath(File file) {
result = file.getRelativePath().replaceAll("\\", "/")
}
private string relativePath(File file) { result = file.getRelativePath().replaceAll("\\", "/") }
/**
* Holds if the `index`-th token of block `copy` is in file `file`, spanning
@@ -23,9 +20,7 @@ private predicate tokenLocation(File file, int sl, int sc, int ec, int el, Copy
/** A token block used for detection of duplicate and similar code. */
class Copy extends @duplication_or_similarity {
/** Gets the index of the last token in this block. */
private int lastToken() {
result = max(int i | tokens(this, i, _, _, _, _) | i)
}
private int lastToken() { result = max(int i | tokens(this, i, _, _, _, _) | i) }
/** Gets the index of the token in this block starting at the location `loc`, if any. */
int tokenStartingAt(Location loc) {
@@ -38,39 +33,26 @@ class Copy extends @duplication_or_similarity {
}
/** Gets the line on which the first token in this block starts. */
int sourceStartLine() {
tokens(this, 0, result, _, _, _)
}
int sourceStartLine() { tokens(this, 0, result, _, _, _) }
/** Gets the column on which the first token in this block starts. */
int sourceStartColumn() {
tokens(this, 0, _, result, _, _)
}
int sourceStartColumn() { tokens(this, 0, _, result, _, _) }
/** Gets the line on which the last token in this block ends. */
int sourceEndLine() {
tokens(this, this.lastToken(), _, _, result, _)
}
int sourceEndLine() { tokens(this, this.lastToken(), _, _, result, _) }
/** Gets the column on which the last token in this block ends. */
int sourceEndColumn() {
tokens(this, this.lastToken(), _, _, _, result)
}
int sourceEndColumn() { tokens(this, this.lastToken(), _, _, _, result) }
/** Gets the number of lines containing at least (part of) one token in this block. */
int sourceLines() {
result = this.sourceEndLine() + 1 - this.sourceStartLine()
}
int sourceLines() { result = this.sourceEndLine() + 1 - this.sourceStartLine() }
/** Gets an opaque identifier for the equivalence class of this block. */
int getEquivalenceClass() {
duplicateCode(this, _, result) or similarCode(this, _, result)
}
int getEquivalenceClass() { duplicateCode(this, _, result) or similarCode(this, _, result) }
/** Gets the source file in which this block appears. */
File sourceFile() {
exists(string name |
duplicateCode(this, name, _) or similarCode(this, name, _) |
exists(string name | duplicateCode(this, name, _) or similarCode(this, name, _) |
name.replaceAll("\\", "/") = relativePath(result)
)
}
@@ -82,13 +64,14 @@ class Copy extends @duplication_or_similarity {
* For more information, see
* [LGTM locations](https://lgtm.com/help/ql/locations).
*/
predicate hasLocationInfo(string filepath, int startline, int startcolumn,
int endline, int endcolumn) {
sourceFile().getAbsolutePath() = filepath and
startline = sourceStartLine() and
startcolumn = sourceStartColumn() and
endline = sourceEndLine() and
endcolumn = sourceEndColumn()
predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
sourceFile().getAbsolutePath() = filepath and
startline = sourceStartLine() and
startcolumn = sourceStartColumn() and
endline = sourceEndLine() and
endcolumn = sourceEndColumn()
}
/** Gets a textual representation of this element. */
@@ -99,7 +82,7 @@ class Copy extends @duplication_or_similarity {
* covered by this block, but they are not the same block.
*/
Copy extendingBlock() {
exists (File file, int sl, int sc, int ec, int el |
exists(File file, int sl, int sc, int ec, int el |
tokenLocation(file, sl, sc, ec, el, this, _) and
tokenLocation(file, sl, sc, ec, el, result, 0)
) and
@@ -113,16 +96,19 @@ class Copy extends @duplication_or_similarity {
* have the same equivalence class, with `start` being the equivalence class of `start1` and
* `start2`, and `end` the equivalence class of `end1` and `end2`.
*/
predicate similar_extension(SimilarBlock start1, SimilarBlock start2,
SimilarBlock end1, SimilarBlock end2, int start, int end) {
start1.getEquivalenceClass() = start and
start2.getEquivalenceClass() = start and
end1.getEquivalenceClass() = end and
end2.getEquivalenceClass() = end and
start1 != start2 and
(end1 = start1 and end2 = start2 or
similar_extension(start1.extendingBlock(), start2.extendingBlock(), end1, end2, _, end)
)
predicate similar_extension(
SimilarBlock start1, SimilarBlock start2, SimilarBlock end1, SimilarBlock end2, int start, int end
) {
start1.getEquivalenceClass() = start and
start2.getEquivalenceClass() = start and
end1.getEquivalenceClass() = end and
end2.getEquivalenceClass() = end and
start1 != start2 and
(
end1 = start1 and end2 = start2
or
similar_extension(start1.extendingBlock(), start2.extendingBlock(), end1, end2, _, end)
)
}
/**
@@ -131,23 +117,25 @@ predicate similar_extension(SimilarBlock start1, SimilarBlock start2,
* have the same equivalence class, with `start` being the equivalence class of `start1` and
* `start2`, and `end` the equivalence class of `end1` and `end2`.
*/
predicate duplicate_extension(DuplicateBlock start1, DuplicateBlock start2,
DuplicateBlock end1, DuplicateBlock end2, int start, int end) {
start1.getEquivalenceClass() = start and
start2.getEquivalenceClass() = start and
end1.getEquivalenceClass() = end and
end2.getEquivalenceClass() = end and
start1 != start2 and
(end1 = start1 and end2 = start2 or
duplicate_extension(start1.extendingBlock(), start2.extendingBlock(), end1, end2, _, end)
)
predicate duplicate_extension(
DuplicateBlock start1, DuplicateBlock start2, DuplicateBlock end1, DuplicateBlock end2, int start,
int end
) {
start1.getEquivalenceClass() = start and
start2.getEquivalenceClass() = start and
end1.getEquivalenceClass() = end and
end2.getEquivalenceClass() = end and
start1 != start2 and
(
end1 = start1 and end2 = start2
or
duplicate_extension(start1.extendingBlock(), start2.extendingBlock(), end1, end2, _, end)
)
}
/** A block of duplicated code. */
class DuplicateBlock extends Copy, @duplication {
override string toString() {
result = "Duplicate code: " + sourceLines() + " duplicated lines."
}
override string toString() { result = "Duplicate code: " + sourceLines() + " duplicated lines." }
}
/** A block of similar code. */
@@ -168,7 +156,7 @@ private predicate stmtInContainer(Stmt s, StmtContainer sc) {
* respectively, where `sc1` and `sc2` are not the same.
*/
predicate duplicateStatement(StmtContainer sc1, StmtContainer sc2, Stmt stmt1, Stmt stmt2) {
exists(int equivstart, int equivend, int first, int last |
exists(int equivstart, int equivend, int first, int last |
stmtInContainer(stmt1, sc1) and
stmtInContainer(stmt2, sc2) and
duplicateCoversStatement(equivstart, equivend, first, last, stmt1) and
@@ -185,8 +173,9 @@ predicate duplicateStatement(StmtContainer sc1, StmtContainer sc2, Stmt stmt1, S
* and `equivstart` and `equivend` are the equivalence classes of the first and the last
* block, respectively.
*/
private
predicate duplicateCoversStatement(int equivstart, int equivend, int first, int last, Stmt stmt) {
private predicate duplicateCoversStatement(
int equivstart, int equivend, int first, int last, Stmt stmt
) {
exists(DuplicateBlock b1, DuplicateBlock b2, Location loc |
stmt.getLocation() = loc and
first = b1.tokenStartingAt(loc) and
@@ -201,8 +190,7 @@ predicate duplicateCoversStatement(int equivstart, int equivend, int first, int
* Holds if `sc1` is a function or toplevel with `total` lines, and `sc2` is a function or
* toplevel that has `duplicate` lines in common with `sc1`.
*/
private
predicate duplicateStatements(StmtContainer sc1, StmtContainer sc2, int duplicate, int total) {
private predicate duplicateStatements(StmtContainer sc1, StmtContainer sc2, int duplicate, int total) {
duplicate = strictcount(Stmt stmt | duplicateStatement(sc1, sc2, stmt, _)) and
total = strictcount(Stmt stmt | stmtInContainer(stmt, sc1))
}
@@ -212,11 +200,10 @@ predicate duplicateStatements(StmtContainer sc1, StmtContainer sc2, int duplicat
* of lines they have in common, which is greater than 90%.
*/
predicate duplicateContainers(StmtContainer sc, StmtContainer other, float percent) {
exists(int total, int duplicate |
duplicateStatements(sc, other, duplicate, total) |
percent = 100.0*duplicate/total and
exists(int total, int duplicate | duplicateStatements(sc, other, duplicate, total) |
percent = 100.0 * duplicate / total and
percent > 90.0
)
)
}
/**
@@ -224,7 +211,7 @@ predicate duplicateContainers(StmtContainer sc, StmtContainer other, float perce
* respectively, where `sc1` and `sc2` are not the same.
*/
private predicate similarStatement(StmtContainer sc1, StmtContainer sc2, Stmt stmt1, Stmt stmt2) {
exists(int start, int end, int first, int last |
exists(int start, int end, int first, int last |
stmtInContainer(stmt1, sc1) and
stmtInContainer(stmt2, sc2) and
similarCoversStatement(start, end, first, last, stmt1) and
@@ -241,8 +228,9 @@ private predicate similarStatement(StmtContainer sc1, StmtContainer sc2, Stmt st
* and `equivstart` and `equivend` are the equivalence classes of the first and the last
* block, respectively.
*/
private predicate similarCoversStatement(int equivstart, int equivend, int first, int last,
Stmt stmt) {
private predicate similarCoversStatement(
int equivstart, int equivend, int first, int last, Stmt stmt
) {
exists(SimilarBlock b1, SimilarBlock b2, Location loc |
stmt.getLocation() = loc and
first = b1.tokenStartingAt(loc) and
@@ -267,41 +255,37 @@ private predicate similarStatements(StmtContainer sc1, StmtContainer sc2, int si
* of similar lines between the two, which is greater than 90%.
*/
predicate similarContainers(StmtContainer sc, StmtContainer other, float percent) {
exists(int total, int similar |
similarStatements(sc, other, similar, total) |
percent = 100.0*similar/total and
exists(int total, int similar | similarStatements(sc, other, similar, total) |
percent = 100.0 * similar / total and
percent > 90.0
)
}
predicate similarLines(File f, int line) {
exists(SimilarBlock b |
b.sourceFile() = f and line in [b.sourceStartLine() .. b.sourceEndLine()]
)
exists(SimilarBlock b | b.sourceFile() = f and line in [b.sourceStartLine() .. b.sourceEndLine()])
}
private
predicate similarLinesPerEquivalenceClass(int equivClass, int lines, File f)
{
private predicate similarLinesPerEquivalenceClass(int equivClass, int lines, File f) {
lines = strictsum(SimilarBlock b, int toSum |
(b.sourceFile() = f and b.getEquivalenceClass() = equivClass) and (toSum = b.sourceLines()) | toSum)
(b.sourceFile() = f and b.getEquivalenceClass() = equivClass) and
toSum = b.sourceLines()
|
toSum
)
}
private pragma[noopt]
predicate similarLinesCovered(File f, int coveredLines, File otherFile) {
pragma[noopt]
private predicate similarLinesCovered(File f, int coveredLines, File otherFile) {
exists(int numLines | numLines = f.getNumberOfLines() |
exists(int coveredApprox |
coveredApprox = strictsum(int num |
exists(int equivClass |
similarLinesPerEquivalenceClass(equivClass, num, f) and
similarLinesPerEquivalenceClass(equivClass, num, otherFile) and
f != otherFile
)
) and
exists(int n, int product |
product = coveredApprox * 100 and n = product / numLines |
n > 75
)
coveredApprox = strictsum(int num |
exists(int equivClass |
similarLinesPerEquivalenceClass(equivClass, num, f) and
similarLinesPerEquivalenceClass(equivClass, num, otherFile) and
f != otherFile
)
) and
exists(int n, int product | product = coveredApprox * 100 and n = product / numLines | n > 75)
) and
exists(int notCovered |
notCovered = count(int j | j in [1 .. numLines] and not similarLines(f, j)) and
@@ -311,30 +295,32 @@ predicate similarLinesCovered(File f, int coveredLines, File otherFile) {
}
predicate duplicateLines(File f, int line) {
exists(DuplicateBlock b |
exists(DuplicateBlock b |
b.sourceFile() = f and line in [b.sourceStartLine() .. b.sourceEndLine()]
)
}
private
predicate duplicateLinesPerEquivalenceClass(int equivClass, int lines, File f)
{
private predicate duplicateLinesPerEquivalenceClass(int equivClass, int lines, File f) {
lines = strictsum(DuplicateBlock b, int toSum |
(b.sourceFile() = f and b.getEquivalenceClass() = equivClass) and (toSum = b.sourceLines()) | toSum)
(b.sourceFile() = f and b.getEquivalenceClass() = equivClass) and
toSum = b.sourceLines()
|
toSum
)
}
private pragma[noopt]
predicate duplicateLinesCovered(File f, int coveredLines, File otherFile) {
pragma[noopt]
private predicate duplicateLinesCovered(File f, int coveredLines, File otherFile) {
exists(int numLines | numLines = f.getNumberOfLines() |
exists(int coveredApprox |
coveredApprox = strictsum(int num |
exists(int equivClass |
duplicateLinesPerEquivalenceClass(equivClass, num, f) and
duplicateLinesPerEquivalenceClass(equivClass, num, otherFile) and
f != otherFile
)
) and
exists (int n, int product | product = coveredApprox * 100 and n = product / numLines | n > 75)
coveredApprox = strictsum(int num |
exists(int equivClass |
duplicateLinesPerEquivalenceClass(equivClass, num, f) and
duplicateLinesPerEquivalenceClass(equivClass, num, otherFile) and
f != otherFile
)
) and
exists(int n, int product | product = coveredApprox * 100 and n = product / numLines | n > 75)
) and
exists(int notCovered |
notCovered = count(int j | j in [1 .. numLines] and not duplicateLines(f, j)) and
@@ -360,4 +346,4 @@ predicate duplicateFiles(File f, File other, int percent) {
covered * 100 / total = percent and
percent > 70
)
}
}

View File

@@ -10,9 +10,10 @@ import semmle.javascript.Files
*
* For more information, see [LGTM locations](https://lgtm.com/help/ql/locations).
*/
external predicate defectResults(int id, string queryPath,
string file, int startline, int startcol, int endline, int endcol,
string message);
external predicate defectResults(
int id, string queryPath, string file, int startline, int startcol, int endline, int endcol,
string message
);
/**
* A defect query result stored in a dashboard database.
@@ -25,7 +26,9 @@ class DefectResult extends int {
/** Gets the file in which this query result was reported. */
File getFile() {
exists(string path | defectResults(this, _, path, _, _, _, _, _) | result.getAbsolutePath() = path)
exists(string path | defectResults(this, _, path, _, _, _, _, _) |
result.getAbsolutePath() = path
)
}
/** Gets the line on which the location of this query result starts. */
@@ -45,7 +48,7 @@ class DefectResult extends int {
/** Gets the URL corresponding to the location of this query result. */
string getURL() {
result = "file://" + getFile().getAbsolutePath() + ":" +
getStartLine() + ":" + getStartColumn() + ":" + getEndLine() + ":" + getEndColumn()
result = "file://" + getFile().getAbsolutePath() + ":" + getStartLine() + ":" + getStartColumn()
+ ":" + getEndLine() + ":" + getEndColumn()
}
}

View File

@@ -19,8 +19,11 @@ import CodeDuplication
import semmle.javascript.RestrictedLocations
from Function f, Function g, float percent
where duplicateContainers(f, g, percent) and
f.getNumBodyStmt() > 5 and
not duplicateContainers(f.getEnclosingStmt().getContainer(), g.getEnclosingStmt().getContainer(), _)
select (FirstLineOf)f, percent.floor() + "% of statements in " + f.describe() +
" are duplicated in $@.", (FirstLineOf)g, g.describe()
where
duplicateContainers(f, g, percent) and
f.getNumBodyStmt() > 5 and
not duplicateContainers(f.getEnclosingStmt().getContainer(), g.getEnclosingStmt().getContainer(),
_)
select f.(FirstLineOf),
percent.floor() + "% of statements in " + f.describe() + " are duplicated in $@.",
g.(FirstLineOf), g.describe()

View File

@@ -19,6 +19,8 @@ import CodeDuplication
import semmle.javascript.RestrictedLocations
from TopLevel one, TopLevel another, float percent
where duplicateContainers(one, another, percent) and
one.getNumLines() > 5
select (FirstLineOf)one, percent + "% of statements in this script are duplicated in $@.", (FirstLineOf)another, "another script"
where
duplicateContainers(one, another, percent) and
one.getNumLines() > 5
select one.(FirstLineOf), percent + "% of statements in this script are duplicated in $@.",
another.(FirstLineOf), "another script"

View File

@@ -9,55 +9,38 @@ import semmle.javascript.Locations
*/
class ExternalData extends @externalDataElement {
/** Gets the path of the file this data was loaded from. */
string getDataPath() {
externalData(this, result, _, _)
}
string getDataPath() { externalData(this, result, _, _) }
/**
* Gets the path of the file this data was loaded from, with its
* extension replaced by `.ql`.
*/
string getQueryPath() {
result = getDataPath().regexpReplaceAll("\\.[^.]*$", ".ql")
}
string getQueryPath() { result = getDataPath().regexpReplaceAll("\\.[^.]*$", ".ql") }
/** Gets the number of fields in this data item. */
int getNumFields() {
result = 1 + max(int i | externalData(this, _, i, _) | i)
}
int getNumFields() { result = 1 + max(int i | externalData(this, _, i, _) | i) }
/** Gets the value of the `i`th field of this data item. */
string getField(int i) {
externalData(this, _, i, result)
}
string getField(int i) { externalData(this, _, i, result) }
/** Gets the integer value of the `i`th field of this data item. */
int getFieldAsInt(int i) {
result = getField(i).toInt()
}
int getFieldAsInt(int i) { result = getField(i).toInt() }
/** Gets the floating-point value of the `i`th field of this data item. */
float getFieldAsFloat(int i) {
result = getField(i).toFloat()
}
float getFieldAsFloat(int i) { result = getField(i).toFloat() }
/** Gets the value of the `i`th field of this data item, interpreted as a date. */
date getFieldAsDate(int i) {
result = getField(i).toDate()
}
date getFieldAsDate(int i) { result = getField(i).toDate() }
/** Gets a textual representation of this data item. */
string toString() {
result = getQueryPath() + ": " + buildTupleString(0)
}
string toString() { result = getQueryPath() + ": " + buildTupleString(0) }
/** Gets a textual representation of this data item, starting with the `n`th field. */
private string buildTupleString(int n) {
(n = getNumFields() - 1 and result = getField(n))
n = getNumFields() - 1 and result = getField(n)
or
(n < getNumFields() - 1 and result = getField(n) + "," + buildTupleString(n+1))
n < getNumFields() - 1 and result = getField(n) + "," + buildTupleString(n + 1)
}
}
/**
@@ -65,43 +48,29 @@ class ExternalData extends @externalDataElement {
*/
class ExternalError extends ExternalData {
/** Gets the name of the tool that reported the error. */
string getReporter() {
result = getField(0)
}
string getReporter() { result = getField(0) }
/** Gets the absolute path of the file in which the error occurs. */
string getPath() {
result = getField(1)
}
string getPath() { result = getField(1) }
/** Gets the reported line of the error. */
int getLine() {
result = getFieldAsInt(2)
}
int getLine() { result = getFieldAsInt(2) }
/** Gets the reported column of the error. */
int getColumn() {
result = getFieldAsInt(3)
}
int getColumn() { result = getFieldAsInt(3) }
/**
* Gets the error type.
*
* This is tool-specific, but usually either "warning" or "error".
*/
string getType() {
result = getField(4)
}
string getType() { result = getField(4) }
/** Gets the error message. */
string getMessage() {
result = getField(5)
}
string getMessage() { result = getField(5) }
/** Gets the file associated with this error. */
File getFile() {
result.getAbsolutePath() = this.getPath()
}
File getFile() { result.getAbsolutePath() = this.getPath() }
/** Gets the URL associated with this error. */
string getURL() {

View File

@@ -10,9 +10,10 @@ import javascript
*
* For more information, see [LGTM locations](https://lgtm.com/help/ql/locations).
*/
external predicate metricResults(int id, string queryPath,
string file, int startline, int startcol, int endline, int endcol,
float value);
external predicate metricResults(
int id, string queryPath, string file, int startline, int startcol, int endline, int endcol,
float value
);
/**
* A metric query result stored in a dashboard database.
@@ -25,7 +26,9 @@ class MetricResult extends int {
/** Gets the file in which this query result was reported. */
File getFile() {
exists(string path | metricResults(this, _, path, _, _, _, _, _) | result.getAbsolutePath() = path)
exists(string path | metricResults(this, _, path, _, _, _, _, _) |
result.getAbsolutePath() = path
)
}
/** Gets the line on which the location of this query result starts. */
@@ -63,8 +66,7 @@ class MetricResult extends int {
/** Gets the URL corresponding to the location of this query result. */
string getURL() {
result = "file://" + getFile().getAbsolutePath() + ":" +
getStartLine() + ":" + getStartColumn() + ":" + getEndLine() + ":" + getEndColumn()
result = "file://" + getFile().getAbsolutePath() + ":" + getStartLine() + ":" + getStartColumn()
+ ":" + getEndLine() + ":" + getEndColumn()
}
}
}

View File

@@ -19,9 +19,12 @@ import CodeDuplication
import semmle.javascript.RestrictedLocations
from Function f, Function g, float percent
where similarContainers(f, g, percent) and
f.getNumBodyStmt() > 5 and
not duplicateContainers(f, g, _) and
not duplicateContainers(f.getEnclosingStmt().getContainer(), g.getEnclosingStmt().getContainer(), _)
select (FirstLineOf)f, percent.floor() + "% of statements in " + f.describe() +
" are similar to statements in $@.", (FirstLineOf)g, g.describe()
where
similarContainers(f, g, percent) and
f.getNumBodyStmt() > 5 and
not duplicateContainers(f, g, _) and
not duplicateContainers(f.getEnclosingStmt().getContainer(), g.getEnclosingStmt().getContainer(),
_)
select f.(FirstLineOf),
percent.floor() + "% of statements in " + f.describe() + " are similar to statements in $@.",
g.(FirstLineOf), g.describe()

View File

@@ -19,9 +19,10 @@ import CodeDuplication
import semmle.javascript.RestrictedLocations
from TopLevel one, TopLevel another, float percent
where similarContainers(one, another, percent) and
one.getNumChildStmt() > 5 and
not duplicateContainers(one, another, _)
select (FirstLineOf)one, percent.floor() + "% of statements in this script are similar to statements in $@.",
(FirstLineOf)another,
"another script"
where
similarContainers(one, another, percent) and
one.getNumChildStmt() > 5 and
not duplicateContainers(one, another, _)
select one.(FirstLineOf),
percent.floor() + "% of statements in this script are similar to statements in $@.",
another.(FirstLineOf), "another script"