mirror of
https://github.com/github/codeql.git
synced 2026-04-28 10:15:14 +02:00
JavaScript: Remove deprecated queries.
These queries have all been deprecated since 1.17 (released in July 2018). I think it's time to say goodbye.
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
<!DOCTYPE qhelp PUBLIC
|
||||
"-//Semmle//qhelp//EN"
|
||||
"qhelp.dtd">
|
||||
<qhelp>
|
||||
<overview>
|
||||
<p>
|
||||
Builtin functions and objects defined in the JavaScript standard library can be shadowed or redefined in user code.
|
||||
This is confusing and makes code hard to understand, so it should be avoided.
|
||||
</p>
|
||||
|
||||
</overview>
|
||||
<recommendation>
|
||||
|
||||
<p>
|
||||
Refactor the code to avoid shadowing or redefinition. For example, if a local variable has the same name as a standard
|
||||
library builtin, it should be renamed.
|
||||
</p>
|
||||
|
||||
</recommendation>
|
||||
<example>
|
||||
|
||||
<p>
|
||||
In the following example, the user-defined function <code>eval</code> shadows the builtin function <code>eval</code>
|
||||
defined in the standard library. It could be renamed <code>evaluate</code> to avoid confusion.
|
||||
</p>
|
||||
|
||||
<sample src="examples/BuiltinRedefined.js" />
|
||||
|
||||
</example>
|
||||
<references>
|
||||
|
||||
|
||||
<li>Ecma International, <i>ECMAScript Language Definition</i>, 5.1 Edition, Section 15. ECMA, 2011.</li>
|
||||
|
||||
|
||||
</references>
|
||||
</qhelp>
|
||||
@@ -1,50 +0,0 @@
|
||||
/**
|
||||
* @name Builtin redefined
|
||||
* @description Standard library functions can be redefined, but this should be avoided
|
||||
* since it makes code hard to read and maintain.
|
||||
* @kind problem
|
||||
* @problem.severity recommendation
|
||||
* @id js/builtin-redefinition
|
||||
* @tags maintainability
|
||||
* @precision medium
|
||||
* @deprecated This query is prone to false positives. Deprecated since 1.17.
|
||||
*/
|
||||
|
||||
import javascript
|
||||
import Definitions
|
||||
|
||||
/**
|
||||
* Holds if `id` is a redefinition of a standard library function that is considered
|
||||
* acceptable since it merely introduces a local alias to the standard function of
|
||||
* the same name.
|
||||
*/
|
||||
predicate acceptableRedefinition(Identifier id) {
|
||||
// function(x, y, undefined) { ... }(23, 42)
|
||||
id.getName() = "undefined" and
|
||||
exists(ImmediatelyInvokedFunctionExpr iife |
|
||||
id = iife.getParameter(iife.getInvocation().getNumArgument())
|
||||
)
|
||||
or
|
||||
// Date = global.Date
|
||||
exists(AssignExpr assgn |
|
||||
id = assgn.getTarget() and
|
||||
id.getName() = assgn.getRhs().getUnderlyingValue().(PropAccess).getPropertyName()
|
||||
)
|
||||
or
|
||||
// var Date = global.Date
|
||||
exists(VariableDeclarator decl |
|
||||
id = decl.getBindingPattern() and
|
||||
id.getName() = decl.getInit().getUnderlyingValue().(PropAccess).getPropertyName()
|
||||
)
|
||||
}
|
||||
|
||||
from DefiningIdentifier id, string name
|
||||
where
|
||||
not id.inExternsFile() and
|
||||
name = id.getName() and
|
||||
name
|
||||
.regexpMatch("Object|Function|Array|String|Boolean|Number|Math|Date|RegExp|Error|" +
|
||||
"NaN|Infinity|undefined|eval|parseInt|parseFloat|isNaN|isFinite|" +
|
||||
"decodeURI|decodeURIComponent|encodeURI|encodeURIComponent") and
|
||||
not acceptableRedefinition(id)
|
||||
select id, "Redefinition of " + name + "."
|
||||
@@ -1,46 +0,0 @@
|
||||
<!DOCTYPE qhelp PUBLIC
|
||||
"-//Semmle//qhelp//EN"
|
||||
"qhelp.dtd">
|
||||
<qhelp>
|
||||
<overview>
|
||||
<p>
|
||||
Defining a method by assigning a closure to a property of the receiver object in the constructor
|
||||
is inefficient, since a new closure is created for every instance. This wastes heap space and may
|
||||
interfere with JIT compilation.
|
||||
</p>
|
||||
|
||||
</overview>
|
||||
<recommendation>
|
||||
|
||||
<p>
|
||||
Assign the function to a property of the prototype object instead. That way, all instances share
|
||||
the same closure.
|
||||
</p>
|
||||
|
||||
</recommendation>
|
||||
<example>
|
||||
|
||||
<p>
|
||||
In the following example, constructor <code>Point</code> defines method <code>move</code> by creating
|
||||
a new closure and storing it in the <code>move</code> property of each new instance. Consequently,
|
||||
<code>p.move</code> and <code>q.move</code> are different methods.
|
||||
</p>
|
||||
|
||||
<sample src="examples/InefficientMethodDefinition.js" />
|
||||
|
||||
<p>
|
||||
It is better to instead define <code>move</code> on the prototype object <code>Point.prototype</code>
|
||||
like this:
|
||||
</p>
|
||||
|
||||
<sample src="examples/InefficientMethodDefinitionGood.js" />
|
||||
|
||||
</example>
|
||||
<references>
|
||||
|
||||
|
||||
<li>Mozilla Developer Network: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain">Inheritance and the prototype chain</a>.</li>
|
||||
|
||||
|
||||
</references>
|
||||
</qhelp>
|
||||
@@ -1,41 +0,0 @@
|
||||
/**
|
||||
* @name Inefficient method definition
|
||||
* @description Defining methods in the constructor (as opposed to adding them to the
|
||||
* prototype object) is inefficient.
|
||||
* @kind problem
|
||||
* @problem.severity recommendation
|
||||
* @id js/method-definition-in-constructor
|
||||
* @tags efficiency
|
||||
* maintainability
|
||||
* @precision medium
|
||||
* @deprecated This query is prone to false positives. Deprecated since 1.17.
|
||||
*/
|
||||
|
||||
import javascript
|
||||
import semmle.javascript.RestrictedLocations
|
||||
|
||||
/**
|
||||
* Holds if `stmt` is of the form `this.<name> = <method>;`.
|
||||
*/
|
||||
predicate methodDefinition(ExprStmt stmt, string name, Function method) {
|
||||
exists(AssignExpr assgn, PropAccess pacc |
|
||||
assgn = stmt.getExpr() and
|
||||
pacc = assgn.getLhs() and
|
||||
pacc.getBase() instanceof ThisExpr and
|
||||
name = pacc.getPropertyName() and
|
||||
method = assgn.getRhs()
|
||||
)
|
||||
}
|
||||
|
||||
from Function ctor, ExprStmt defn, string name, Function method
|
||||
where
|
||||
not ctor instanceof ImmediatelyInvokedFunctionExpr and
|
||||
defn = ctor.getABodyStmt() and
|
||||
methodDefinition(defn, name, method) and
|
||||
// if the method captures a local variable of the constructor, it cannot
|
||||
// easily be moved to the constructor object
|
||||
not exists(Variable v | v.getScope() = ctor.getScope() |
|
||||
v.getAnAccess().getContainer().getEnclosingContainer*() = method
|
||||
)
|
||||
select defn.(FirstLineOf),
|
||||
name + " should be added to the prototype object rather than to each instance."
|
||||
Reference in New Issue
Block a user