Skip to content

Commit 4a14187

Browse files
authored
Add prefer-object-from-entries rule (#1308)
1 parent 73c0dfd commit 4a14187

10 files changed

+1644
-3
lines changed

docs/rules/prefer-array-flat.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Type: `string[]`
7474

7575
You can also check custom functions that flatten arrays.
7676

77-
`_.flatten()`, `lodash.flatten()`, and `underscore.flatten()` are checked by default.
77+
`_.flatten()`, `lodash.flatten()`, and `underscore.flatten()` are always checked.
7878

7979
Example:
8080

docs/rules/prefer-at.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Type: `string[]`
9494

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

97-
`_.last()`, `lodash.last()`, and `underscore.last()` are checked by default.
97+
`_.last()`, `lodash.last()`, and `underscore.last()` are always checked.
9898

9999
Example:
100100

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Prefer using `Object.fromEntries(…)` to transform a list of key-value pairs into an object
2+
3+
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.
4+
5+
This rule is fixable for simple cases.
6+
7+
## Fail
8+
9+
```js
10+
const object = pairs.reduce(
11+
(object, [key, value]) => ({...object, [key]: value}),
12+
{}
13+
);
14+
```
15+
16+
```js
17+
const object = pairs.reduce(
18+
(object, [key, value]) => ({...object, [key]: value}),
19+
Object.create(null)
20+
);
21+
```
22+
23+
```js
24+
const object = pairs.reduce(
25+
(object, [key, value]) => Object.assign(object, {[key]: value}),
26+
{}
27+
);
28+
```
29+
30+
```js
31+
const object = pairs.reduce(addPairToObject, {});
32+
```
33+
34+
```js
35+
const object = _.fromPairs(pairs);
36+
```
37+
38+
## Pass
39+
40+
```js
41+
const object = Object.fromEntries(pairs);
42+
```
43+
44+
```js
45+
const object = new Map(pairs);
46+
```
47+
48+
## Options
49+
50+
Type: `object`
51+
52+
### functions
53+
54+
Type: `string[]`
55+
56+
You can also check custom functions that transforms pairs.
57+
58+
`lodash.fromPairs()` and `_.fromPairs()` are always checked.
59+
60+
Example:
61+
62+
```js
63+
{
64+
'unicorn/prefer-object-from-entries': [
65+
'error',
66+
{
67+
functions: [
68+
'getObjectFromKeyValue',
69+
'utils.fromPairs'
70+
]
71+
}
72+
]
73+
}
74+
```
75+
76+
```js
77+
// eslint unicorn/prefer-object-from-entries: ["error", {"functions": ["utils.fromPairs"]}]
78+
const object = utils.fromPairs(pairs); // Fails
79+
```
80+

docs/rules/prefer-object-has-own.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Type: `string[]`
3434

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

37-
`_.has()`, `lodash.has()`, and `underscore.has()` are checked by default.
37+
`_.has()`, `lodash.has()`, and `underscore.has()` are always checked.
3838

3939
Example:
4040

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ module.exports = {
105105
'unicorn/prefer-negative-index': 'error',
106106
'unicorn/prefer-node-protocol': 'error',
107107
'unicorn/prefer-number-properties': 'error',
108+
'unicorn/prefer-object-from-entries': 'error',
108109
// TODO: Enable this by default when targeting a Node.js version that supports `Object.hasOwn`.
109110
'unicorn/prefer-object-has-own': 'off',
110111
'unicorn/prefer-optional-catch-binding': 'error',

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Configure it in `package.json`.
101101
"unicorn/prefer-negative-index": "error",
102102
"unicorn/prefer-node-protocol": "error",
103103
"unicorn/prefer-number-properties": "error",
104+
"unicorn/prefer-object-from-entries": "error",
104105
"unicorn/prefer-object-has-own": "off",
105106
"unicorn/prefer-optional-catch-binding": "error",
106107
"unicorn/prefer-prototype-methods": "error",
@@ -218,6 +219,7 @@ Each rule has emojis denoting:
218219
| [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()`. || 🔧 | |
219220
| [prefer-node-protocol](docs/rules/prefer-node-protocol.md) | Prefer using the `node:` protocol when importing Node.js builtin modules. || 🔧 | |
220221
| [prefer-number-properties](docs/rules/prefer-number-properties.md) | Prefer `Number` static properties over global ones. || 🔧 | 💡 |
222+
| [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. || 🔧 | |
221223
| [prefer-object-has-own](docs/rules/prefer-object-has-own.md) | Prefer `Object.hasOwn(…)` over `Object.prototype.hasOwnProperty.call(…)`. | | 🔧 | |
222224
| [prefer-optional-catch-binding](docs/rules/prefer-optional-catch-binding.md) | Prefer omitting the `catch` binding parameter. || 🔧 | |
223225
| [prefer-prototype-methods](docs/rules/prefer-prototype-methods.md) | Prefer borrowing methods from the prototype instead of the instance. || 🔧 | |

0 commit comments

Comments
 (0)