-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
es-roikoren/no-function-prototype-bind
rule
- Loading branch information
1 parent
2238048
commit 0c05740
Showing
10 changed files
with
240 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-es-roikoren': patch | ||
--- | ||
|
||
feat: add `es-roikoren/no-function-prototype-bind` rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# es-roikoren/no-function-prototype-bind | ||
> disallow the `Function.prototype.bind` method. | ||
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-es5` and `plugin:es-roikoren/restrict-to-es3` | ||
|
||
This rule reports ES5 `Function.prototype.bind` method as errors. | ||
|
||
## Examples | ||
|
||
⛔ Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint es-roikoren/no-function-prototype-bind: error */ | ||
foo.bind(this); | ||
|
||
var foo = (function() { | ||
return this.bar; | ||
}).bind(this); | ||
``` | ||
|
||
## 📚 References | ||
|
||
- [Rule source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.3/src/rules/no-function-prototype-bind.ts) | ||
- [Test source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.3/tests/src/rules/no-function-prototype-bind.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createRule } from '../util/create-rule'; | ||
import { definePrototypeMethodHandler, schema } from '../util/define-prototype-method-handler'; | ||
import type { IAggressive } from '../util/define-prototype-method-handler'; | ||
|
||
export const category = 'ES5'; | ||
export default createRule<[options: IAggressive], 'forbidden'>({ | ||
name: 'no-function-prototype-bind', | ||
meta: { | ||
type: 'problem', | ||
docs: { description: 'disallow the `Function.prototype.bind` method.', recommended: false }, | ||
schema, | ||
messages: { forbidden: "ES5 '{{name}}' method is forbidden." }, | ||
}, | ||
defaultOptions: [{}], | ||
create(context) { | ||
return definePrototypeMethodHandler(context, { Function: ['bind'] }); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import path from 'path'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/types'; | ||
|
||
import { RuleTester } from '../../tester'; | ||
import rule from '../../../src/rules/no-function-prototype-bind'; | ||
|
||
const ruleId = 'no-function-prototype-bind'; | ||
const error = { | ||
messageId: 'forbidden' as const, | ||
line: 1, | ||
column: 1, | ||
type: AST_NODE_TYPES.MemberExpression, | ||
data: { name: 'Function.prototype.bind' }, | ||
}; | ||
|
||
new RuleTester().run(ruleId, rule, { | ||
valid: [ | ||
'bind(this)', | ||
'foo.bind(this)', | ||
'(function fn(){}).name', | ||
'(()=>{}).name', | ||
{ code: 'bind(this)', settings: { es: { aggressive: true } } }, | ||
{ code: '(function fn(){}).name', settings: { es: { aggressive: true } } }, | ||
{ code: '(()=>{}).name', settings: { es: { aggressive: true } } }, | ||
{ code: 'foo.bind(this)', options: [{ aggressive: false }], settings: { es: { aggressive: true } } }, | ||
{ code: '(function fn(){}).name', options: [{ aggressive: false }], settings: { es: { aggressive: true } } }, | ||
{ code: '(()=>{}).name', options: [{ aggressive: false }], settings: { es: { aggressive: true } } }, | ||
], | ||
invalid: [ | ||
{ code: '(function fn(){}).bind(this)', errors: [{ ...error, column: 1 }] }, | ||
{ code: '(()=>{}).bind(this)', errors: [{ ...error, column: 1 }] }, | ||
{ code: 'foo.bind(this)', errors: [{ ...error, column: 1 }], settings: { es: { aggressive: true } } }, | ||
{ code: '(function fn(){}).bind(this)', errors: [{ ...error, column: 1 }], settings: { es: { aggressive: true } } }, | ||
{ code: '(()=>{}).bind(this)', errors: [{ ...error, column: 1 }], settings: { es: { aggressive: true } } }, | ||
{ | ||
code: 'foo.bind(this)', | ||
options: [{ aggressive: true }], | ||
errors: [{ ...error, column: 1 }], | ||
settings: { es: { aggressive: false } }, | ||
}, | ||
{ | ||
code: '(function fn(){}).bind(this)', | ||
options: [{ aggressive: true }], | ||
errors: [{ ...error, column: 1 }], | ||
settings: { es: { aggressive: false } }, | ||
}, | ||
{ | ||
code: '(()=>{}).bind(this)', | ||
options: [{ aggressive: true }], | ||
errors: [{ ...error, column: 1 }], | ||
settings: { es: { aggressive: false } }, | ||
}, | ||
], | ||
}); | ||
|
||
// ----------------------------------------------------------------------------- | ||
// TypeScript | ||
// ----------------------------------------------------------------------------- | ||
const parser = require.resolve('@typescript-eslint/parser'); | ||
const tsconfigRootDir = path.resolve(__dirname, '../../fixtures'); | ||
const project = 'tsconfig.json'; | ||
const filename = path.join(tsconfigRootDir, 'test.ts'); | ||
|
||
new RuleTester({ parser }).run(`${ruleId} TS`, rule, { | ||
valid: [ | ||
'bind(this)', | ||
'foo.bind(this)', | ||
'(function fn(){}).name', | ||
'(()=>{}).name', | ||
'let foo = {}; foo.bind(this)', | ||
{ code: 'bind(this)', settings: { es: { aggressive: true } } }, | ||
|
||
// `Function` is unknown type if tsconfig.json is not configured. | ||
'Object.assign.bind(this)', | ||
'let foo = Function(); foo.bind(this)', | ||
'let foo = String; foo.bind(this)', | ||
], | ||
invalid: [ | ||
{ code: '(function fn(){}).bind(this)', errors: [{ ...error, column: 1 }] }, | ||
{ code: '(()=>{}).bind(this)', errors: [{ ...error, column: 1 }] }, | ||
{ code: 'let foo = function () {}; foo.bind(this)', errors: [{ ...error, column: 27 }] }, | ||
{ code: 'let foo = () => {}; foo.bind(this)', errors: [{ ...error, column: 21 }] }, | ||
{ code: 'function foo () {}; foo.bind(this)', errors: [{ ...error, column: 21 }] }, | ||
{ code: 'function f(a: () => number) { a.bind(this) }', errors: [{ ...error, column: 31 }] }, | ||
{ code: 'let foo = { fn () {} }; foo.fn.bind(this)', errors: [{ ...error, column: 25 }] }, | ||
{ code: 'class Foo {fn()}; const foo = new Foo(); foo.fn.bind(this)', errors: [{ ...error, column: 42 }] }, | ||
{ code: 'function f<T extends ((a: any) => T)>(a: T) { a.bind(this) }', errors: [{ ...error, column: 47 }] }, | ||
{ | ||
code: "function f<T extends ((a: any) => T) | 'union'>(a: T) { a.bind(this) }", | ||
errors: [{ ...error, column: 57 }], | ||
}, | ||
{ code: 'Object.assign.bind(this)', errors: [{ ...error, column: 1 }], settings: { es: { aggressive: true } } }, | ||
{ | ||
code: 'let foo = Function(); foo.bind(this)', | ||
errors: [{ ...error, column: 23 }], | ||
settings: { es: { aggressive: true } }, | ||
}, | ||
{ | ||
code: 'let foo = String; foo.bind(this)', | ||
errors: [{ ...error, column: 19 }], | ||
settings: { es: { aggressive: true } }, | ||
}, | ||
{ code: 'foo.bind(this)', errors: [{ ...error, column: 1 }], settings: { es: { aggressive: true } } }, | ||
], | ||
}); | ||
|
||
new RuleTester({ parser, parserOptions: { tsconfigRootDir, project } }).run(`${ruleId} TS Full Types`, rule, { | ||
valid: [ | ||
{ filename, code: 'bind(this)' }, | ||
{ filename, code: 'foo.bind(this)' }, | ||
{ filename, code: '(function fn(){}).name' }, | ||
{ filename, code: '(()=>{}).name' }, | ||
{ filename, code: 'let foo = {}; foo.bind(this)' }, | ||
{ filename, code: 'bind(this)', settings: { es: { aggressive: true } } }, | ||
], | ||
invalid: [ | ||
{ filename, code: '(function fn(){}).bind(this)', errors: [{ ...error, column: 1 }] }, | ||
{ filename, code: '(()=>{}).bind(this)', errors: [{ ...error, column: 1 }] }, | ||
{ filename, code: 'let foo = function () {}; foo.bind(this)', errors: [{ ...error, column: 27 }] }, | ||
{ filename, code: 'let foo = () => {}; foo.bind(this)', errors: [{ ...error, column: 21 }] }, | ||
{ filename, code: 'function foo () {}; foo.bind(this)', errors: [{ ...error, column: 21 }] }, | ||
{ filename, code: 'function f(a: () => number) { a.bind(this) }', errors: [{ ...error, column: 31 }] }, | ||
{ filename, code: 'let foo = { fn () {} }; foo.fn.bind(this)', errors: [{ ...error, column: 25 }] }, | ||
{ filename, code: 'Object.assign.bind(this)', errors: [{ ...error, column: 1 }] }, | ||
{ filename, code: 'class Foo {fn()}; const foo = new Foo(); foo.fn.bind(this)', errors: [{ ...error, column: 42 }] }, | ||
{ filename, code: 'let foo = Function(); foo.bind(this)', errors: [{ ...error, column: 23 }] }, | ||
{ filename, code: 'let foo = String; foo.bind(this)', errors: [{ ...error, column: 19 }] }, | ||
{ | ||
filename, | ||
code: 'function f<T extends ((a: any) => T)>(a: T) { a.bind(this) }', | ||
errors: [{ ...error, column: 47 }], | ||
}, | ||
{ | ||
filename, | ||
code: "function f<T extends ((a: any) => T) | 'union'>(a: T) { a.bind(this) }", | ||
errors: [{ ...error, column: 57 }], | ||
}, | ||
{ filename, code: 'foo.bind(this)', errors: [{ ...error, column: 1 }], settings: { es: { aggressive: true } } }, | ||
], | ||
}); |