Skip to content

Commit

Permalink
Add no-useless-fallback-in-spread rule (#1481)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Aug 16, 2021
1 parent 4162145 commit 1675118
Show file tree
Hide file tree
Showing 7 changed files with 566 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/rules/no-useless-fallback-in-spread.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Forbid useless fallback when spreading in object literals

Spreading [falsy values](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) in object literals won't add any unexpected properties, so it's unnecessary to add an empty object as fallback.

This rule is fixable.

## Fail

```js
const object = {...(foo || {})};
```

```js
const object = {...(foo ?? {})};
```
## Pass
```js
const object = {...foo};
```
```js
const object = {...(foo && {})};
```
```js
const array = [...(foo || [])];
```
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module.exports = {
'unicorn/no-unreadable-array-destructuring': 'error',
'unicorn/no-unsafe-regex': 'off',
'unicorn/no-unused-properties': 'off',
'unicorn/no-useless-fallback-in-spread': 'error',
'unicorn/no-useless-length-check': 'error',
'unicorn/no-useless-spread': 'error',
'unicorn/no-useless-undefined': 'error',
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Configure it in `package.json`.
"unicorn/no-unreadable-array-destructuring": "error",
"unicorn/no-unsafe-regex": "off",
"unicorn/no-unused-properties": "off",
"unicorn/no-useless-fallback-in-spread": "error",
"unicorn/no-useless-length-check": "error",
"unicorn/no-useless-spread": "error",
"unicorn/no-useless-undefined": "error",
Expand Down Expand Up @@ -194,6 +195,7 @@ Each rule has emojis denoting:
| [no-unreadable-array-destructuring](docs/rules/no-unreadable-array-destructuring.md) | Disallow unreadable array destructuring. || 🔧 | |
| [no-unsafe-regex](docs/rules/no-unsafe-regex.md) | Disallow unsafe regular expressions. | | | |
| [no-unused-properties](docs/rules/no-unused-properties.md) | Disallow unused object properties. | | | |
| [no-useless-fallback-in-spread](docs/rules/no-useless-fallback-in-spread.md) | Forbid useless fallback when spreading in object literals. || 🔧 | |
| [no-useless-length-check](docs/rules/no-useless-length-check.md) | Disallow useless array length check. || 🔧 | |
| [no-useless-spread](docs/rules/no-useless-spread.md) | Disallow unnecessary spread. || 🔧 | |
| [no-useless-undefined](docs/rules/no-useless-undefined.md) | Disallow useless `undefined`. || 🔧 | |
Expand Down
76 changes: 76 additions & 0 deletions rules/no-useless-fallback-in-spread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';
const {matches} = require('./selectors/index.js');
const {
isParenthesized,
getParenthesizedRange,
getParentheses,
} = require('./utils/parentheses.js');
const shouldAddParenthesesToSpreadElementArgument = require('./utils/should-add-parentheses-to-spread-element-argument.js');

const MESSAGE_ID = 'no-useless-fallback-in-spread';
const messages = {
[MESSAGE_ID]: 'The empty object is useless.',
};

const selector = [
'ObjectExpression',
' > ',
'SpreadElement.properties',
' > ',
'LogicalExpression.argument',
matches([
'[operator="||"]',
'[operator="??"]',
]),
' > ',
'ObjectExpression[properties.length=0].right',
].join('');

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => ({
[selector](emptyObject) {
return {
node: emptyObject,
messageId: MESSAGE_ID,
/** @param {import('eslint').Rule.RuleFixer} fixer */
* fix(fixer) {
const sourceCode = context.getSourceCode();
const logicalExpression = emptyObject.parent;
const {left} = logicalExpression;
const isLeftObjectParenthesized = isParenthesized(left, sourceCode);
const [, start] = isLeftObjectParenthesized
? getParenthesizedRange(left, sourceCode)
: left.range;
const [, end] = logicalExpression.range;

yield fixer.removeRange([start, end]);

if (
isLeftObjectParenthesized
|| !shouldAddParenthesesToSpreadElementArgument(left)
) {
const parentheses = getParentheses(logicalExpression, sourceCode);

for (const token of parentheses) {
yield fixer.remove(token);
}
}
},
};
},
});

const schema = [];

module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Forbid useless fallback when spreading in object literals.',
},
fixable: 'code',
schema,
messages,
},
};
53 changes: 53 additions & 0 deletions test/no-useless-fallback-in-spread.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {getTester} from './utils/test.mjs';

const {test} = getTester(import.meta);

test.snapshot({
valid: [
'const array = [...(foo || [])]',
'const array = [...(foo || {})]',
'const array = [...(foo && {})]',
'const object = {...(foo && {})}',
'const object = {...({} || foo)}',
'const object = {...({} && foo)}',
'const object = {...({} ?? foo)}',
'const object = {...(foo ? foo : {})}',
'const object = {...foo}',
'const object = {...(foo ?? ({} || {}))}',
'const {...foo} = object',
'function foo({...bar}){}',
'const object = {...(foo || {}).toString()}',
'const object = {...fn(foo || {})}',
'const object = call({}, ...(foo || {}))',
'const object = {...(foo || {not: "empty"})}',
'const object = {...(foo || {...{}})}',
],
invalid: [
'const object = {...(foo || {})}',
'const object = {...(foo ?? {})}',
'const object = {...(foo ?? (( {} )))}',
'const object = {...((( foo )) ?? (( {} )))}',
'const object = {...(( (( foo )) ?? (( {} )) ))}',
'async ()=> ({...((await foo) || {})})',
'const object = {...(0 || {})}',
'const object = {...((-0) || {})}',
'const object = {...(.0 || {})}',
'const object = {...(0n || {})}',
'const object = {...(false || {})}',
'const object = {...(null || {})}',
'const object = {...(undefined || {})}',
'const object = {...((a && b) || {})}',
'const object = {...(NaN || {})}',
'const object = {...("" || {})}',
'const object = {...([] || {})}',
'const object = {...({} || {})}',
'const object = {...(foo || {}),}',
'const object = {...((foo ?? {}) || {})}',
'const object = {...((foo && {}) || {})}',
'const object = {...(foo && {} || {})}',
'const object = {...({...(foo || {})})}',
'function foo(a = {...(bar || {})}){}',
// The only case we'll break, but we should not care about it.
'const object = {...(document.all || {})}',
],
});

0 comments on commit 1675118

Please sign in to comment.