Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-useless-undefined
rule (#718)
- Loading branch information
Showing
9 changed files
with
460 additions
and
8 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,100 @@ | ||
# Disallow useless `undefined` | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
let foo = undefined; | ||
``` | ||
|
||
```js | ||
const {foo = undefined} = bar; | ||
``` | ||
|
||
```js | ||
const noop = () => undefined; | ||
``` | ||
|
||
```js | ||
function foo() { | ||
return undefined; | ||
} | ||
``` | ||
|
||
```js | ||
function* foo() { | ||
yield undefined; | ||
} | ||
``` | ||
|
||
```js | ||
function foo(bar = undefined) { | ||
} | ||
``` | ||
|
||
```js | ||
function foo({bar = undefined}) { | ||
} | ||
``` | ||
|
||
```js | ||
foo(undefined); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
let foo; | ||
``` | ||
|
||
```js | ||
const {foo} = bar; | ||
``` | ||
|
||
```js | ||
const noop = () => {}; | ||
``` | ||
|
||
```js | ||
function foo() { | ||
return; | ||
} | ||
``` | ||
|
||
```js | ||
function* foo() { | ||
yield; | ||
} | ||
``` | ||
|
||
```js | ||
function foo(bar) { | ||
} | ||
``` | ||
|
||
```js | ||
function foo({bar}) { | ||
} | ||
``` | ||
|
||
```js | ||
foo(); | ||
``` | ||
|
||
## Conflict with ESLint `array-callback-return` rule | ||
|
||
We recommend setting the ESLint [`array-callback-return`](https://eslint.org/docs/rules/array-callback-return#top) rule option [`allowImplicit`](https://eslint.org/docs/rules/array-callback-return#options) to `true`: | ||
|
||
```json | ||
{ | ||
"rules": { | ||
"array-callback-return": [ | ||
"error", | ||
{ | ||
"allowImplicit": true | ||
} | ||
] | ||
} | ||
} | ||
``` |
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
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,126 @@ | ||
'use strict'; | ||
const {isCommaToken} = require('eslint-utils'); | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
|
||
const messageId = 'no-useless-undefined'; | ||
|
||
const getSelector = (parent, property) => | ||
`${parent} > Identifier.${property}[name="undefined"]`; | ||
|
||
// `return undefined` | ||
const returnSelector = getSelector('ReturnStatement', 'argument'); | ||
|
||
// `yield undefined` | ||
const yieldSelector = getSelector('YieldExpression[delegate=false]', 'argument'); | ||
|
||
// `() => undefined` | ||
const arrowFunctionSelector = getSelector('ArrowFunctionExpression', 'body'); | ||
|
||
// `let foo = undefined` / `var foo = undefined` | ||
const variableInitSelector = getSelector( | ||
[ | ||
'VariableDeclaration', | ||
'[kind!="const"]', | ||
'>', | ||
'VariableDeclarator' | ||
].join(''), | ||
'init' | ||
); | ||
|
||
// `const {foo = undefined} = {}` | ||
const assignmentPatternSelector = getSelector('AssignmentPattern', 'right'); | ||
|
||
const isUndefined = node => node && node.type === 'Identifier' && node.name === 'undefined'; | ||
|
||
const create = context => { | ||
const listener = fix => node => { | ||
context.report({ | ||
node, | ||
messageId, | ||
fix: fixer => fix(node, fixer) | ||
}); | ||
}; | ||
|
||
const code = context.getSourceCode().text; | ||
|
||
const removeNodeAndLeadingSpace = (node, fixer) => { | ||
const textBefore = code.slice(0, node.range[0]); | ||
return fixer.removeRange([ | ||
node.range[0] - (textBefore.length - textBefore.trim().length), | ||
node.range[1] | ||
]); | ||
}; | ||
|
||
return { | ||
[returnSelector]: listener(removeNodeAndLeadingSpace), | ||
[yieldSelector]: listener(removeNodeAndLeadingSpace), | ||
[arrowFunctionSelector]: listener( | ||
(node, fixer) => fixer.replaceText(node, '{}') | ||
), | ||
[variableInitSelector]: listener( | ||
(node, fixer) => fixer.removeRange([node.parent.id.range[1], node.range[1]]) | ||
), | ||
[assignmentPatternSelector]: listener( | ||
(node, fixer) => fixer.removeRange([node.parent.left.range[1], node.range[1]]) | ||
), | ||
CallExpression: node => { | ||
const argumentNodes = node.arguments; | ||
const undefinedArguments = []; | ||
for (let index = argumentNodes.length - 1; index >= 0; index--) { | ||
const node = argumentNodes[index]; | ||
if (isUndefined(node)) { | ||
undefinedArguments.unshift(node); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
if (undefinedArguments.length === 0) { | ||
return; | ||
} | ||
|
||
const firstUndefined = undefinedArguments[0]; | ||
const lastUndefined = undefinedArguments[undefinedArguments.length - 1]; | ||
|
||
context.report({ | ||
messageId, | ||
loc: { | ||
start: firstUndefined.loc.start, | ||
end: lastUndefined.loc.end | ||
}, | ||
fix: fixer => { | ||
let start = firstUndefined.range[0]; | ||
let end = lastUndefined.range[1]; | ||
|
||
const previousArgument = argumentNodes[argumentNodes.length - undefinedArguments.length - 1]; | ||
|
||
if (previousArgument) { | ||
start = previousArgument.range[1]; | ||
} else { | ||
// If all arguments removed, and there is trailing comma, we need remove it. | ||
const tokenAfter = context.getTokenAfter(lastUndefined); | ||
if (isCommaToken(tokenAfter)) { | ||
end = tokenAfter.range[1]; | ||
} | ||
} | ||
|
||
return fixer.removeRange([start, end]); | ||
} | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
messages: { | ||
[messageId]: 'Do not use useless `undefined`.' | ||
}, | ||
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
Oops, something went wrong.