Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add es/no-function-prototype-bind rule #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ There are multiple configs that enable all rules in this category: `plugin:es/no
| [es/no-array-prototype-reduceright](./no-array-prototype-reduceright.md) | disallow the `Array.prototype.reduceRight` method. | |
| [es/no-array-prototype-some](./no-array-prototype-some.md) | disallow the `Array.prototype.some` method. | |
| [es/no-date-now](./no-date-now.md) | disallow the `Date.now` method. | |
| [es/no-function-prototype-bind](./no-function-prototype-bind.md) | disallow the `Function.prototype.bind` method. | |
| [es/no-json](./no-json.md) | disallow the `JSON` class. | |
| [es/no-keyword-properties](./no-keyword-properties.md) | disallow reserved words as property names. | |
| [es/no-object-defineproperties](./no-object-defineproperties.md) | disallow the `Object.defineProperties` method. | |
Expand Down
39 changes: 39 additions & 0 deletions docs/rules/no-function-prototype-bind.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# es/no-function-prototype-bind
> disallow the `Function.prototype.bind` method

- ✅ The following configurations enable this rule: `plugin:es/no-new-in-es5` and `plugin:es/restrict-to-es3`

This rule reports ES5 `Function.prototype.bind` method as errors.

This rule is silent by default because it's hard to know types. You need to configure [the aggressive mode](../#the-aggressive-mode) or TypeScript in order to enable this rule.

## 💡 Examples

⛔ Examples of **incorrect** code for this rule:

<eslint-playground type="bad" code="/*eslint es/no-function-prototype-bind: [error, { aggressive: true }] */
foo.bind(this);

var foo = (function() {
return this.bar
}).bind(this)
" />

## 🔧 Options

This rule has an option.

```yml
rules:
es/no-function-prototype-bind: [error, { aggressive: false }]
```

### aggressive: boolean

Configure the aggressive mode for only this rule.
This is prior to the `settings.es.aggressive` setting.

## 📚 References

- [Rule source](https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/lib/rules/no-function-prototype-bind.js)
- [Test source](https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/tests/lib/rules/no-function-prototype-bind.js)
1 change: 1 addition & 0 deletions lib/configs/no-new-in-es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
"es/no-array-prototype-reduceright": "error",
"es/no-array-prototype-some": "error",
"es/no-date-now": "error",
"es/no-function-prototype-bind": "error",
"es/no-json": "error",
"es/no-keyword-properties": "error",
"es/no-object-defineproperties": "error",
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module.exports = {
"no-exponential-operators": require("./rules/no-exponential-operators"),
"no-export-ns-from": require("./rules/no-export-ns-from"),
"no-for-of-loops": require("./rules/no-for-of-loops"),
"no-function-prototype-bind": require("./rules/no-function-prototype-bind"),
"no-generators": require("./rules/no-generators"),
"no-global-this": require("./rules/no-global-this"),
"no-import-meta": require("./rules/no-import-meta"),
Expand Down
40 changes: 40 additions & 0 deletions lib/rules/no-function-prototype-bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @author Yosuke Ota <https://github.com/ota-meshi>
* See LICENSE file in root directory for full license.
*/
"use strict"

const {
definePrototypeMethodHandler,
} = require("../util/define-prototype-method-handler")

module.exports = {
meta: {
docs: {
description: "disallow the `Function.prototype.bind` method.",
category: "ES5",
Copy link
Contributor Author

@ota-meshi ota-meshi Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you release it in a minor version, we need to change it to undefined so that it doesn't affect the ruleset.
Let me know if you need to change it.

recommended: false,
url:
"http://mysticatea.github.io/eslint-plugin-es/rules/no-function-prototype-bind.html",
},
fixable: null,
messages: {
forbidden: "ES5 '{{name}}' method is forbidden.",
},
schema: [
{
type: "object",
properties: {
aggressive: { type: "boolean" },
},
additionalProperties: false,
},
],
type: "problem",
},
create(context) {
return definePrototypeMethodHandler(context, {
Function: ["bind"],
})
},
}
59 changes: 56 additions & 3 deletions lib/util/define-prototype-method-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ function definePrototypeMethodHandler(context, nameMap) {
) {
return className === "String"
}
if (
memberAccessNode.object.type === "FunctionExpression" ||
memberAccessNode.object.type === "ArrowFunctionExpression"
) {
return className === "Function"
}

// Test object type.
return isTS
Expand Down Expand Up @@ -125,10 +131,22 @@ function definePrototypeMethodHandler(context, nameMap) {
// )
// .map(([id]) => id)
// .join("|"),
// "symbol.flags": !type.symbol
// ? undefined
// : Object.entries(ts.SymbolFlags)
// .filter(
// ([_id, flag]) =>
// typeof flag === "number" &&
// (type.symbol.flags & flag) === flag,
// )
// .map(([id]) => id)
// .join("|"),
// },
// className,
// )

if (isFunction(type)) {
return className === "Function"
}
if (isAny(type) || isUnknown(type)) {
return aggressive
}
Expand Down Expand Up @@ -159,12 +177,28 @@ function definePrototypeMethodHandler(context, nameMap) {
}

if (isClassOrInterface(type)) {
const name = type.symbol.escapedName
return name === className || name === `Readonly${className}`
return typeSymbolEscapedNameEquals(type, className)
}
return checker.typeToString(type) === className
}

/**
* Check if the symbol.escapedName of the given type is expected or not.
* @param {import("typescript").InterfaceType} type The type to check.
* @param {string} className The expected type name.
* @returns {boolean} `true` if should disallow it.
*/
function typeSymbolEscapedNameEquals(type, className) {
const escapedName = type.symbol.escapedName
return (
escapedName === className ||
// ReadonlyArray, ReadonlyMap, ReadonlySet
escapedName === `Readonly${className}` ||
// CallableFunction
(className === "Function" && escapedName === "CallableFunction")
)
}

/**
* Get the constraint type of a given type parameter type if exists.
*
Expand Down Expand Up @@ -351,4 +385,23 @@ function isUnknown(type) {
return (type.flags & ts.TypeFlags.Unknown) !== 0
}

/**
* Check if a given type is `function` or not.
* @param {import("typescript").Type} type The type to check.
* @returns {boolean} `true` if the type is `function`.
*/
function isFunction(type) {
if (
type.symbol &&
(type.symbol.flags &
(ts.SymbolFlags.Function | ts.SymbolFlags.Method)) !==
0
) {
return true
}

const signatures = type.getCallSignatures()
return signatures.length > 0
}

module.exports = { definePrototypeMethodHandler }
Loading