-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathEmptyArrayInit.ql
More file actions
48 lines (43 loc) · 1.53 KB
/
EmptyArrayInit.ql
File metadata and controls
48 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @name Omitted array element
* @description Omitted elements in array literals are easy to miss and should not be used.
* @kind problem
* @problem.severity recommendation
* @id js/omitted-array-element
* @tags maintainability
* readability
* language-features
* @precision high
*/
import javascript
/**
* An initial omitted element in an array expression.
*
* This is represented by the corresponding array expression, with a special
* `hasLocationInfo` implementation that assigns it a location covering the
* first omitted array element.
*/
class OmittedArrayElement extends ArrayExpr {
int idx;
OmittedArrayElement() {
idx = min(int i | elementIsOmitted(i))
}
/**
* Holds if this element is at the specified location.
* The location spans column `startcolumn` of line `startline` to
* column `endcolumn` of line `endline` in file `filepath`.
* For more information, see
* [LGTM locations](https://lgtm.com/help/ql/locations).
*/
predicate hasLocationInfo(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
exists (Token pre, Location before, Location after |
idx = 0 and pre = getFirstToken() or
pre = getElement(idx-1).getLastToken().getNextToken() |
before = pre.getLocation() and after = pre.getNextToken().getLocation() and
before.hasLocationInfo(filepath, startline, startcolumn, _, _) and
after.hasLocationInfo(_, _, _, endline, endcolumn)
)
}
}
from OmittedArrayElement ae
select ae, "Avoid omitted array elements."