Skip to content

Commit

Permalink
Add prefer-object-from-entries rule (#1308)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jul 29, 2021
1 parent 73c0dfd commit 4a14187
Show file tree
Hide file tree
Showing 10 changed files with 1,644 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/rules/prefer-array-flat.md
Expand Up @@ -74,7 +74,7 @@ Type: `string[]`

You can also check custom functions that flatten arrays.

`_.flatten()`, `lodash.flatten()`, and `underscore.flatten()` are checked by default.
`_.flatten()`, `lodash.flatten()`, and `underscore.flatten()` are always checked.

Example:

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/prefer-at.md
Expand Up @@ -94,7 +94,7 @@ Type: `string[]`

You can also check custom functions that get last element of objects.

`_.last()`, `lodash.last()`, and `underscore.last()` are checked by default.
`_.last()`, `lodash.last()`, and `underscore.last()` are always checked.

Example:

Expand Down
80 changes: 80 additions & 0 deletions docs/rules/prefer-object-from-entries.md
@@ -0,0 +1,80 @@
# Prefer using `Object.fromEntries(…)` to transform a list of key-value pairs into an object

When transforming a list of key-value pairs into an object, [`Object.fromEntries(…)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries) should be preferred.

This rule is fixable for simple cases.

## Fail

```js
const object = pairs.reduce(
(object, [key, value]) => ({...object, [key]: value}),
{}
);
```

```js
const object = pairs.reduce(
(object, [key, value]) => ({...object, [key]: value}),
Object.create(null)
);
```

```js
const object = pairs.reduce(
(object, [key, value]) => Object.assign(object, {[key]: value}),
{}
);
```

```js
const object = pairs.reduce(addPairToObject, {});
```

```js
const object = _.fromPairs(pairs);
```

## Pass

```js
const object = Object.fromEntries(pairs);
```

```js
const object = new Map(pairs);
```

## Options

Type: `object`

### functions

Type: `string[]`

You can also check custom functions that transforms pairs.

`lodash.fromPairs()` and `_.fromPairs()` are always checked.

Example:

```js
{
'unicorn/prefer-object-from-entries': [
'error',
{
functions: [
'getObjectFromKeyValue',
'utils.fromPairs'
]
}
]
}
```

```js
// eslint unicorn/prefer-object-from-entries: ["error", {"functions": ["utils.fromPairs"]}]
const object = utils.fromPairs(pairs); // Fails
```

2 changes: 1 addition & 1 deletion docs/rules/prefer-object-has-own.md
Expand Up @@ -34,7 +34,7 @@ Type: `string[]`

You can also check custom functions that indicating the object has the specified property as its own property.

`_.has()`, `lodash.has()`, and `underscore.has()` are checked by default.
`_.has()`, `lodash.has()`, and `underscore.has()` are always checked.

Example:

Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -105,6 +105,7 @@ module.exports = {
'unicorn/prefer-negative-index': 'error',
'unicorn/prefer-node-protocol': 'error',
'unicorn/prefer-number-properties': 'error',
'unicorn/prefer-object-from-entries': 'error',
// TODO: Enable this by default when targeting a Node.js version that supports `Object.hasOwn`.
'unicorn/prefer-object-has-own': 'off',
'unicorn/prefer-optional-catch-binding': 'error',
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -101,6 +101,7 @@ Configure it in `package.json`.
"unicorn/prefer-negative-index": "error",
"unicorn/prefer-node-protocol": "error",
"unicorn/prefer-number-properties": "error",
"unicorn/prefer-object-from-entries": "error",
"unicorn/prefer-object-has-own": "off",
"unicorn/prefer-optional-catch-binding": "error",
"unicorn/prefer-prototype-methods": "error",
Expand Down Expand Up @@ -218,6 +219,7 @@ Each rule has emojis denoting:
| [prefer-negative-index](docs/rules/prefer-negative-index.md) | Prefer negative index over `.length - index` for `{String,Array,TypedArray}#slice()`, `Array#splice()` and `Array#at()`. || 🔧 | |
| [prefer-node-protocol](docs/rules/prefer-node-protocol.md) | Prefer using the `node:` protocol when importing Node.js builtin modules. || 🔧 | |
| [prefer-number-properties](docs/rules/prefer-number-properties.md) | Prefer `Number` static properties over global ones. || 🔧 | 💡 |
| [prefer-object-from-entries](docs/rules/prefer-object-from-entries.md) | Prefer using `Object.fromEntries(…)` to transform a list of key-value pairs into an object. || 🔧 | |
| [prefer-object-has-own](docs/rules/prefer-object-has-own.md) | Prefer `Object.hasOwn(…)` over `Object.prototype.hasOwnProperty.call(…)`. | | 🔧 | |
| [prefer-optional-catch-binding](docs/rules/prefer-optional-catch-binding.md) | Prefer omitting the `catch` binding parameter. || 🔧 | |
| [prefer-prototype-methods](docs/rules/prefer-prototype-methods.md) | Prefer borrowing methods from the prototype instead of the instance. || 🔧 | |
Expand Down

0 comments on commit 4a14187

Please sign in to comment.