Skip to content

Commit efbd74a

Browse files
committed
remove more spurious jQuery objects by using externs
1 parent 2a1095a commit efbd74a

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,33 @@ private class OrdinaryJQueryObject extends JQueryObjectInternal {
5151
bindingset[methodName, call]
5252
private predicate returnsAJQueryObject(DataFlow::CallNode call, string methodName) {
5353
not (
54-
methodName = "val" // `jQuery.val()`
54+
neverReturnsJQuery(methodName)
55+
or
56+
methodName = "val" and call.getNumArgument() = 0 // `jQuery.val()`
5557
or
5658
methodName = ["html", "text"] and call.getNumArgument() = 0 // `jQuery.html()`/`jQuery.text()`
5759
or
5860
// `jQuery.attr(key)`/`jQuery.prop(key)`
5961
methodName = ["attr", "prop"] and
6062
call.getNumArgument() = 1 and
6163
call.getArgument(0).mayHaveStringValue(_)
62-
or
63-
// `jQuery.data()`
64-
methodName = "data" and call.getNumArgument() = 0
65-
or
66-
// `jQuery.data(key)`
67-
methodName = "data" and call.getNumArgument() = 1 and call.getArgument(0).mayHaveStringValue(_)
68-
or
69-
methodName = ["Event", "Deferred"] // $.Event / $.Deferred
70-
or
71-
methodName = "trim" // $.trim()
72-
or
73-
// `$.ajax`, and related methods.
74-
// note: there are 2 different `get` methods, and none of them return a jQuery object.
75-
methodName = ["ajax", "get", "getJSON", "getScript", "post", "load"]
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")
7681
)
7782
}
7883

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+

0 commit comments

Comments
 (0)