-
-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
prefer-reflect-apply
rule (#239)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
850b8a1
commit 0b9c0fe
Showing
5 changed files
with
204 additions
and
0 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,30 @@ | ||
# Prefer `Reflect.apply()` over `Function#apply()` | ||
|
||
[`Reflect.apply)()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/apply) is arguably less verbose and easier to understand. In addition, when you accept arbitrary methods, it's not safe to assume `.apply()` exists or is not overridden. | ||
|
||
This rule is fixable. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
function foo() {} | ||
|
||
foo.apply(null, [42]); | ||
Function.prototype.apply.call(foo, null, [42]); | ||
foo.apply(this, [42]); | ||
Function.prototype.apply.call(foo, this, [42]); | ||
foo.apply(null, arguments); | ||
Function.prototype.apply.call(foo, null, arguments); | ||
foo.apply(this, arguments); | ||
Function.prototype.apply.call(foo, this, arguments); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
function foo() {} | ||
|
||
Reflect.apply(foo, null, [42]); | ||
``` |
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,86 @@ | ||
'use strict'; | ||
const astUtils = require('eslint-ast-utils'); | ||
const getDocsUrl = require('./utils/get-docs-url'); | ||
|
||
const isApplySignature = (argument1, argument2) => ( | ||
((argument1.type === 'Literal' && argument1.raw === 'null') || | ||
argument1.type === 'ThisExpression') && | ||
(argument2.type === 'ArrayExpression' || | ||
(argument2.type === 'Identifier' && | ||
argument2.name === 'arguments')) | ||
); | ||
|
||
const getReflectApplyCall = (sourceCode, func, receiver, args) => ( | ||
`Reflect.apply(${sourceCode.getText(func)}, ${sourceCode.getText(receiver)}, ${sourceCode.getText(args)})` | ||
); | ||
|
||
const fixDirectApplyCall = (node, sourceCode) => { | ||
if ( | ||
astUtils.getPropertyName(node.callee) === 'apply' && | ||
node.arguments.length === 2 && | ||
isApplySignature(node.arguments[0], node.arguments[1]) | ||
) { | ||
return fixer => ( | ||
fixer.replaceText( | ||
node, | ||
getReflectApplyCall(sourceCode, node.callee.object, node.arguments[0], node.arguments[1]) | ||
) | ||
); | ||
} | ||
}; | ||
|
||
const fixFunctionPrototypeCall = (node, sourceCode) => { | ||
if ( | ||
astUtils.getPropertyName(node.callee) === 'call' && | ||
astUtils.getPropertyName(node.callee.object) === 'apply' && | ||
astUtils.getPropertyName(node.callee.object.object) === 'prototype' && | ||
node.callee.object.object.object && | ||
node.callee.object.object.object.type === 'Identifier' && | ||
node.callee.object.object.object.name === 'Function' && | ||
node.arguments.length === 3 && | ||
isApplySignature(node.arguments[1], node.arguments[2]) | ||
) { | ||
return fixer => ( | ||
fixer.replaceText( | ||
node, | ||
getReflectApplyCall(sourceCode, node.arguments[0], node.arguments[1], node.arguments[2]) | ||
) | ||
); | ||
} | ||
}; | ||
|
||
const create = context => { | ||
return { | ||
CallExpression: node => { | ||
if ( | ||
!( | ||
node.callee.type === 'MemberExpression' && | ||
!['Literal', 'ArrayExpression', 'ObjectExpression'].includes(node.callee.object.type) | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
const sourceCode = context.getSourceCode(); | ||
const fix = fixDirectApplyCall(node, sourceCode) || fixFunctionPrototypeCall(node, sourceCode); | ||
if (fix) { | ||
context.report({ | ||
node, | ||
message: 'Prefer `Reflect.apply()` over `Function#apply()`.', | ||
fix | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocsUrl(__filename) | ||
}, | ||
fixable: 'code' | ||
} | ||
}; |
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,85 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/prefer-reflect-apply'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
const errors = [ | ||
{ | ||
ruleId: 'prefer-reflect-apply' | ||
} | ||
]; | ||
|
||
ruleTester.run('prefer-reflect-apply', rule, { | ||
valid: [ | ||
'foo.apply();', | ||
'foo.apply(null);', | ||
'foo.apply(this);', | ||
'foo.apply(null, 42);', | ||
'foo.apply(this, 42);', | ||
'foo.apply(bar, arguments);', | ||
'[].apply(null, [42]);', | ||
'foo.apply(bar);', | ||
'foo.apply(bar, []);', | ||
'foo.apply;', | ||
'apply;', | ||
'Reflect.apply(foo, null);', | ||
'Reflect.apply(foo, null, [bar]);' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'foo.apply(null, [42]);', | ||
output: 'Reflect.apply(foo, null, [42]);', | ||
errors | ||
}, | ||
{ | ||
code: 'foo.bar.apply(null, [42]);', | ||
output: 'Reflect.apply(foo.bar, null, [42]);', | ||
errors | ||
}, | ||
{ | ||
code: 'Function.prototype.apply.call(foo, null, [42]);', | ||
output: 'Reflect.apply(foo, null, [42]);', | ||
errors | ||
}, | ||
{ | ||
code: 'Function.prototype.apply.call(foo.bar, null, [42]);', | ||
output: 'Reflect.apply(foo.bar, null, [42]);', | ||
errors | ||
}, | ||
{ | ||
code: 'foo.apply(null, arguments);', | ||
output: 'Reflect.apply(foo, null, arguments);', | ||
errors | ||
}, | ||
{ | ||
code: 'Function.prototype.apply.call(foo, null, arguments);', | ||
output: 'Reflect.apply(foo, null, arguments);', | ||
errors | ||
}, | ||
{ | ||
code: 'foo.apply(this, [42]);', | ||
output: 'Reflect.apply(foo, this, [42]);', | ||
errors | ||
}, | ||
{ | ||
code: 'Function.prototype.apply.call(foo, this, [42]);', | ||
output: 'Reflect.apply(foo, this, [42]);', | ||
errors | ||
}, | ||
{ | ||
code: 'foo.apply(this, arguments);', | ||
output: 'Reflect.apply(foo, this, arguments);', | ||
errors | ||
}, | ||
{ | ||
code: 'Function.prototype.apply.call(foo, this, arguments);', | ||
output: 'Reflect.apply(foo, this, arguments);', | ||
errors | ||
} | ||
] | ||
}); |