Skip to content

Commit 8b8b352

Browse files
authored
Merge pull request #715 from xiemaisi/js/autoformat
Approved by asger-semmle
2 parents 0b19f41 + 31bb39a commit 8b8b352

File tree

380 files changed

+9951
-13917
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

380 files changed

+9951
-13917
lines changed

javascript/ql/src/AlertSuppression.ql

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,80 +12,70 @@ import javascript
1212
*/
1313
class SuppressionComment extends Locatable {
1414
string text;
15+
1516
string annotation;
1617

1718
SuppressionComment() {
1819
(
19-
text = this.(LineComment).getText() or
20-
text = this.(HTML::CommentNode).getText()
21-
)
22-
and
20+
text = this.(LineComment).getText() or
21+
text = this.(HTML::CommentNode).getText()
22+
) and
2323
(
24-
// match `lgtm[...]` anywhere in the comment
25-
annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
26-
or
27-
// match `lgtm` at the start of the comment and after semicolon
28-
annotation = text.regexpFind("(?i)(?<=^|;)\\s*lgtm(?!\\B|\\s*\\[)", _, _).trim()
24+
// match `lgtm[...]` anywhere in the comment
25+
annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
26+
or
27+
// match `lgtm` at the start of the comment and after semicolon
28+
annotation = text.regexpFind("(?i)(?<=^|;)\\s*lgtm(?!\\B|\\s*\\[)", _, _).trim()
2929
)
3030
}
3131

3232
/** Gets the text of this suppression comment, not including delimiters. */
33-
string getText() {
34-
result = text
35-
}
33+
string getText() { result = text }
3634

3735
/** Gets the suppression annotation in this comment. */
38-
string getAnnotation() {
39-
result = annotation
40-
}
36+
string getAnnotation() { result = annotation }
4137

4238
/**
43-
* Holds if this comment applies to the range from column `startcolumn` of line `startline`
44-
* to column `endcolumn` of line `endline` in file `filepath`.
45-
*/
39+
* Holds if this comment applies to the range from column `startcolumn` of line `startline`
40+
* to column `endcolumn` of line `endline` in file `filepath`.
41+
*/
4642
predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
4743
this.getLocation().hasLocationInfo(filepath, startline, _, endline, endcolumn) and
4844
startcolumn = 1
4945
}
5046

5147
/** Gets the scope of this suppression. */
52-
SuppressionScope getScope() {
53-
this = result.getSuppressionComment()
54-
}
48+
SuppressionScope getScope() { this = result.getSuppressionComment() }
5549
}
5650

5751
/**
5852
* The scope of an alert suppression comment.
5953
*/
6054
class SuppressionScope extends @locatable {
61-
SuppressionScope() {
62-
this instanceof SuppressionComment
63-
}
55+
SuppressionScope() { this instanceof SuppressionComment }
6456

6557
/** Gets a suppression comment with this scope. */
66-
SuppressionComment getSuppressionComment() {
67-
result = this
68-
}
58+
SuppressionComment getSuppressionComment() { result = this }
6959

7060
/**
71-
* Holds if this element is at the specified location.
72-
* The location spans column `startcolumn` of line `startline` to
73-
* column `endcolumn` of line `endline` in file `filepath`.
74-
* For more information, see
75-
* [LGTM locations](https://lgtm.com/help/ql/locations).
76-
*/
77-
predicate hasLocationInfo(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
61+
* Holds if this element is at the specified location.
62+
* The location spans column `startcolumn` of line `startline` to
63+
* column `endcolumn` of line `endline` in file `filepath`.
64+
* For more information, see
65+
* [LGTM locations](https://lgtm.com/help/ql/locations).
66+
*/
67+
predicate hasLocationInfo(
68+
string filepath, int startline, int startcolumn, int endline, int endcolumn
69+
) {
7870
this.(SuppressionComment).covers(filepath, startline, startcolumn, endline, endcolumn)
7971
}
8072

8173
/** Gets a textual representation of this element. */
82-
string toString() {
83-
result = "suppression range"
84-
}
74+
string toString() { result = "suppression range" }
8575
}
8676

8777
from SuppressionComment c
88-
select c, // suppression comment
89-
c.getText(), // text of suppression comment (excluding delimiters)
90-
c.getAnnotation(), // text of suppression annotation
91-
c.getScope() // scope of suppression
78+
select c, // suppression comment
79+
c.getText(), // text of suppression comment (excluding delimiters)
80+
c.getAnnotation(), // text of suppression annotation
81+
c.getScope() // scope of suppression

javascript/ql/src/AngularJS/DeadAngularJSEventListener.ql

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,23 @@ import javascript
1717
predicate isABuiltinEventName(string name) {
1818
// $rootScope.Scope
1919
name = "$destroy" or
20-
2120
// $location
2221
name = "$locationChangeStart" or
2322
name = "$locationChangeSuccess" or
24-
2523
// ngView
2624
name = "$viewContentLoaded" or
27-
2825
// angular-ui/ui-router
2926
name = "$stateChangeStart" or
3027
name = "$stateNotFound" or
3128
name = "$stateChangeSuccess" or
3229
name = "$stateChangeError" or
3330
name = "$viewContentLoading " or
3431
name = "$viewContentLoaded " or
35-
3632
// $route
3733
name = "$routeChangeStart" or
3834
name = "$routeChangeSuccess" or
3935
name = "$routeChangeError" or
4036
name = "$routeUpdate" or
41-
4237
// ngInclude
4338
name = "$includeContentRequested" or
4439
name = "$includeContentLoaded" or
@@ -49,20 +44,21 @@ predicate isABuiltinEventName(string name) {
4944
* Holds if user code emits or broadcasts an event named `name`.
5045
*/
5146
predicate isAUserDefinedEventName(string name) {
52-
exists (string methodName, MethodCallExpr mce |
53-
methodName = "$emit" or methodName = "$broadcast" |
47+
exists(string methodName, MethodCallExpr mce | methodName = "$emit" or methodName = "$broadcast" |
5448
mce.getArgument(0).mayHaveStringValue(name) and
5549
(
5650
// dataflow based scope resolution
57-
mce = any(AngularJS::ScopeServiceReference scope).getAMethodCall(methodName) or
51+
mce = any(AngularJS::ScopeServiceReference scope).getAMethodCall(methodName)
52+
or
5853
// heuristic scope resolution: assume parameters like `$scope` or `$rootScope` are AngularJS scope objects
5954
exists(SimpleParameter param |
6055
param.getName() = any(AngularJS::ScopeServiceReference scope).getName() and
6156
mce.getReceiver().mayReferToParameter(param) and
6257
mce.getMethodName() = methodName
63-
) or
58+
)
59+
or
6460
// a call in an AngularJS expression
65-
exists (AngularJS::NgCallExpr call |
61+
exists(AngularJS::NgCallExpr call |
6662
call.getCallee().(AngularJS::NgVarExpr).getName() = methodName and
6763
call.getArgument(0).(AngularJS::NgString).getStringValue() = name
6864
)
@@ -71,14 +67,16 @@ predicate isAUserDefinedEventName(string name) {
7167
}
7268

7369
from AngularJS::ScopeServiceReference scope, MethodCallExpr mce, string eventName
74-
where mce = scope.getAMethodCall("$on") and
75-
mce.getArgument(0).mayHaveStringValue(eventName) and
76-
not (
77-
isAUserDefinedEventName(eventName) or
78-
isABuiltinEventName(eventName) or
79-
// external, namespaced
80-
eventName.regexpMatch(".*[.:].*") or
81-
// from other event system (DOM: onClick et al)
82-
eventName.regexpMatch("on[A-Z][a-zA-Z]+") // camelCased with 'on'-prefix
83-
)
84-
select mce.getArgument(1), "This event listener is dead, the event '" + eventName + "' is not emitted anywhere."
70+
where
71+
mce = scope.getAMethodCall("$on") and
72+
mce.getArgument(0).mayHaveStringValue(eventName) and
73+
not (
74+
isAUserDefinedEventName(eventName) or
75+
isABuiltinEventName(eventName) or
76+
// external, namespaced
77+
eventName.regexpMatch(".*[.:].*") or
78+
// from other event system (DOM: onClick et al)
79+
eventName.regexpMatch("on[A-Z][a-zA-Z]+") // camelCased with 'on'-prefix
80+
)
81+
select mce.getArgument(1),
82+
"This event listener is dead, the event '" + eventName + "' is not emitted anywhere."

javascript/ql/src/AngularJS/DependencyMismatch.ql

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
import javascript
1616

1717
from AngularJS::InjectableFunction f, SimpleParameter p, string msg
18-
where p = f.asFunction().getAParameter() and
19-
(
20-
not p = f.getDependencyParameter(_) and
21-
msg = "This parameter has no injected dependency."
22-
or
23-
exists (string n | p = f.getDependencyParameter(n) |
24-
p.getName() != n and
25-
exists(f.getDependencyParameter(p.getName())) and
26-
msg = "This parameter is named '" + p.getName() + "', " +
27-
"but actually refers to dependency '" + n + "'."
28-
)
29-
)
30-
select p, msg
18+
where
19+
p = f.asFunction().getAParameter() and
20+
(
21+
not p = f.getDependencyParameter(_) and
22+
msg = "This parameter has no injected dependency."
23+
or
24+
exists(string n | p = f.getDependencyParameter(n) |
25+
p.getName() != n and
26+
exists(f.getDependencyParameter(p.getName())) and
27+
msg = "This parameter is named '" + p.getName() + "', " +
28+
"but actually refers to dependency '" + n + "'."
29+
)
30+
)
31+
select p, msg

javascript/ql/src/AngularJS/DisablingSce.ql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import javascript
1414

1515
from MethodCallExpr mce, AngularJS::BuiltinServiceReference service
16-
where service.getName() ="$sceProvider" and
17-
mce = service.getAMethodCall( "enabled") and
18-
mce.getArgument(0).mayHaveBooleanValue(false)
16+
where
17+
service.getName() = "$sceProvider" and
18+
mce = service.getAMethodCall("enabled") and
19+
mce.getArgument(0).mayHaveBooleanValue(false)
1920
select mce, "Disabling SCE is strongly discouraged."

javascript/ql/src/AngularJS/DoubleCompilation.ql

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
import javascript
1414

1515
from AngularJS::ServiceReference compile, SimpleParameter elem, CallExpr c
16-
where compile.getName() = "$compile" and
17-
elem = any(AngularJS::CustomDirective d).getALinkFunction().(AngularJS::LinkFunction).getElementParameter() and
18-
c = compile.getACall() and
19-
c.getArgument(0).mayReferToParameter(elem) and
20-
// don't flag $compile calls that specify a `maxPriority`
21-
c.getNumArgument() < 3
16+
where
17+
compile.getName() = "$compile" and
18+
elem = any(AngularJS::CustomDirective d)
19+
.getALinkFunction()
20+
.(AngularJS::LinkFunction)
21+
.getElementParameter() and
22+
c = compile.getACall() and
23+
c.getArgument(0).mayReferToParameter(elem) and
24+
// don't flag $compile calls that specify a `maxPriority`
25+
c.getNumArgument() < 3
2226
select c, "This call to $compile may cause double compilation of '" + elem + "'."

javascript/ql/src/AngularJS/DuplicateDependency.ql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import javascript
1313
import semmle.javascript.RestrictedLocations
1414

1515
predicate isRepeatedDependency(AngularJS::InjectableFunction f, string name, ASTNode location) {
16-
exists(int i, int j | i < j and
16+
exists(int i, int j |
17+
i < j and
1718
exists(f.getDependencyDeclaration(i, name)) and
1819
location = f.getDependencyDeclaration(j, name)
1920
)
2021
}
22+
2123
from AngularJS::InjectableFunction f, ASTNode node, string name
22-
where isRepeatedDependency(f, name, node) and
23-
not count(f.asFunction().getParameterByName(name)) > 1 // avoid duplicating reports from js/duplicate-parameter-name
24-
select (FirstLineOf)f.asFunction(), "This function has a duplicate dependency '$@'.", node, name
24+
where
25+
isRepeatedDependency(f, name, node) and
26+
not count(f.asFunction().getParameterByName(name)) > 1 // avoid duplicating reports from js/duplicate-parameter-name
27+
select f.asFunction().(FirstLineOf), "This function has a duplicate dependency '$@'.", node, name

0 commit comments

Comments
 (0)