Skip to content

Commit a0800ce

Browse files
authored
Merge pull request github#3386 from erik-krogh/lessJQueryChaining
Approved by asgerf
2 parents 8e9e3c8 + efbd74a commit a0800ce

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,50 @@ private class OrdinaryJQueryObject extends JQueryObjectInternal {
3737
OrdinaryJQueryObject() {
3838
exists(JQuery::MethodCall jq |
3939
this.flow().getALocalSource() = jq and
40-
// `jQuery.val()` does _not_ return a jQuery object
41-
jq.getMethodName() != "val"
40+
returnsAJQueryObject(jq, jq.getMethodName())
4241
)
4342
}
4443
}
4544

45+
/**
46+
* Holds if the jQuery method call `call`, with name `methodName`, returns a JQuery object.
47+
*
48+
* The `call` parameter has type `DataFlow::CallNode` instead of `JQuery::MethodCall` to avoid non-monotonic recursion.
49+
* The not is placed inside the predicate to avoid non-monotonic recursion.
50+
*/
51+
bindingset[methodName, call]
52+
private predicate returnsAJQueryObject(DataFlow::CallNode call, string methodName) {
53+
not (
54+
neverReturnsJQuery(methodName)
55+
or
56+
methodName = "val" and call.getNumArgument() = 0 // `jQuery.val()`
57+
or
58+
methodName = ["html", "text"] and call.getNumArgument() = 0 // `jQuery.html()`/`jQuery.text()`
59+
or
60+
// `jQuery.attr(key)`/`jQuery.prop(key)`
61+
methodName = ["attr", "prop"] and
62+
call.getNumArgument() = 1 and
63+
call.getArgument(0).mayHaveStringValue(_)
64+
)
65+
}
66+
67+
/**
68+
* Holds if a jQuery method named `name` never returns a JQuery object.
69+
*/
70+
private predicate neverReturnsJQuery(string name) {
71+
forex(ExternalMemberDecl decl |
72+
decl.getBaseName() = "jQuery" and
73+
decl.getName() = name
74+
|
75+
not decl
76+
.getDocumentation()
77+
.getATagByTitle("return")
78+
.getType()
79+
.getAnUnderlyingType()
80+
.hasQualifiedName("jQuery")
81+
)
82+
}
83+
4684
/**
4785
* DEPRECATED. Use `JQuery::MethodCall` instead.
4886
*

javascript/ql/test/library-tests/frameworks/jQuery/JQueryMethodCall.expected

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@ WARNING: Type JQueryMethodCall has been deprecated and may be removed in future
1717
| tst.js:5:1:5:15 | window.jQuery() | $ |
1818
| tst.js:6:1:6:32 | angular ... .attr() | attr |
1919
| tst.js:7:1:7:14 | $("<br>", doc) | $ |
20+
| tst.js:11:1:11:8 | $("foo") | $ |
21+
| tst.js:11:1:11:15 | $("foo").html() | html |
22+
| tst.js:12:1:12:8 | $("foo") | $ |
23+
| tst.js:12:1:12:20 | $("foo").html("foo") | html |
24+
| tst.js:12:1:12:31 | $("foo" ... query() | isJquery |
25+
| tst.js:13:1:13:8 | $("foo") | $ |
26+
| tst.js:13:1:13:17 | $("foo").data({}) | data |
27+
| tst.js:13:1:13:28 | $("foo" ... query() | isJquery |
28+
| tst.js:15:1:15:8 | $("foo") | $ |
29+
| tst.js:15:1:15:20 | $("foo").attr("bar") | attr |
30+
| tst.js:17:1:17:8 | $.trim() | trim |
31+
| tst.js:18:1:18:10 | $.ajax({}) | ajax |

javascript/ql/test/library-tests/frameworks/jQuery/interpretsArgumentAsHtml.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ WARNING: Type JQueryMethodCall has been deprecated and may be removed in future
88
| tst2.js:2:1:2:7 | jq("a") | tst2.js:2:4:2:6 | "a" |
99
| tst.js:3:1:3:9 | $("<a/>") | tst.js:3:3:3:8 | "<a/>" |
1010
| tst.js:7:1:7:14 | $("<br>", doc) | tst.js:7:3:7:8 | "<br>" |
11+
| tst.js:11:1:11:8 | $("foo") | tst.js:11:3:11:7 | "foo" |
12+
| tst.js:12:1:12:8 | $("foo") | tst.js:12:3:12:7 | "foo" |
13+
| tst.js:12:1:12:20 | $("foo").html("foo") | tst.js:12:15:12:19 | "foo" |
14+
| tst.js:13:1:13:8 | $("foo") | tst.js:13:3:13:7 | "foo" |
15+
| tst.js:15:1:15:8 | $("foo") | tst.js:15:3:15:7 | "foo" |
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2017 The Closure Compiler Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* @fileoverview Externs for jQuery 3.1
19+
*
20+
* Note that some functions use different return types depending on the number
21+
* of parameters passed in. In these cases, you may need to annotate the type
22+
* of the result in your code, so the JSCompiler understands which type you're
23+
* expecting. For example:
24+
* <code>var elt = /** @type {Element} * / (foo.get(0));</code>
25+
*
26+
* @see http://api.jquery.com/
27+
* @externs
28+
*/
29+
30+
/**
31+
* @typedef {(Window|Document|Element|Array<Element>|string|jQuery|
32+
* NodeList)}
33+
*/
34+
var jQuerySelector;
35+
36+
/**
37+
* @constructor
38+
* @param {(jQuerySelector|Object|function())=} arg1
39+
* @param {(Element|jQuery|Document|
40+
* Object<string, (string|function(!jQuery.Event))>)=} arg2
41+
* @throws {Error} on invalid selector
42+
* @return {!jQuery}
43+
* @implements {Iterable}
44+
*/
45+
function jQuery(arg1, arg2) { };
46+
47+
/**
48+
* @const
49+
*/
50+
var $ = jQuery;
51+
52+
/**
53+
* @param {(string|jQueryAjaxSettings|Object<string,*>)} arg1
54+
* @param {(jQueryAjaxSettings|Object<string, *>)=} settings
55+
* @return {!jQuery.jqXHR}
56+
*/
57+
jQuery.ajax = function (arg1, settings) { };
58+
59+
/**
60+
* @param {string} str
61+
* @return {string}
62+
* @nosideeffects
63+
*/
64+
jQuery.trim = function (str) { };
65+

javascript/ql/test/library-tests/frameworks/jQuery/tst.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,14 @@ window.$();
55
window.jQuery();
66
angular.element("<div/>").attr()
77
$("<br>", doc);
8+
9+
10+
11+
$("foo").html().doesNotReturnJquery();
12+
$("foo").html("foo").isJquery();
13+
$("foo").data({}).isJquery();
14+
15+
$("foo").attr("bar").doesNotReturnJquery();
16+
17+
$.trim().doesNotReturnJquery();
18+
$.ajax({}).doesNotReturnJquery()

0 commit comments

Comments
 (0)