@putout/plugin-remove-duplicate-keys 2.1.0 β 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -6
- package/lib/remove-duplicate-keys.js +34 -16
- package/package.json +14 -13
package/README.md
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
# @putout/plugin-remove-duplicate-keys [![NPM version][NPMIMGURL]][NPMURL]
|
|
1
|
+
# @putout/plugin-remove-duplicate-keys [![NPM version][NPMIMGURL]][NPMURL]
|
|
2
2
|
|
|
3
3
|
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-remove-duplicate-keys.svg?style=flat&longCache=true
|
|
4
4
|
[NPMURL]: https://npmjs.org/package/@putout/plugin-remove-duplicate-keys"npm"
|
|
5
|
-
[DependencyStatusURL]: https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-duplicate-keys
|
|
6
|
-
[DependencyStatusIMGURL]: https://david-dm.org/coderaiser/putout.svg?path=packages/plugin-remove-duplicate-keys
|
|
7
5
|
|
|
8
|
-
|
|
6
|
+
> An object initializer is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces (`{}`).
|
|
7
|
+
>
|
|
8
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer)
|
|
9
|
+
|
|
10
|
+
π[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to find and remove duplecate keys.
|
|
9
11
|
|
|
10
12
|
## Install
|
|
11
13
|
|
|
@@ -23,7 +25,7 @@ npm i @putout/plugin-remove-duplicate-keys
|
|
|
23
25
|
}
|
|
24
26
|
```
|
|
25
27
|
|
|
26
|
-
## β
|
|
28
|
+
## β Example of incorrect code
|
|
27
29
|
|
|
28
30
|
```js
|
|
29
31
|
const a = {
|
|
@@ -33,7 +35,7 @@ const a = {
|
|
|
33
35
|
};
|
|
34
36
|
```
|
|
35
37
|
|
|
36
|
-
## β
|
|
38
|
+
## β
Example of correct code
|
|
37
39
|
|
|
38
40
|
```js
|
|
39
41
|
const a = {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {types} = require('putout');
|
|
3
|
+
const {types, operator} = require('putout');
|
|
4
4
|
const fullstore = require('fullstore');
|
|
5
|
+
|
|
5
6
|
const {
|
|
6
7
|
isSpreadElement,
|
|
7
8
|
isIdentifier,
|
|
@@ -9,13 +10,25 @@ const {
|
|
|
9
10
|
isStringLiteral,
|
|
10
11
|
} = types;
|
|
11
12
|
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const
|
|
13
|
+
const {traverseProperties} = operator;
|
|
14
|
+
|
|
15
|
+
const isSpreadId = (name) => (a) => isSpreadElement(a) && isIdentifier(a.argument, {
|
|
16
|
+
name,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const isObjectPropertyId = (name, computed) => (a) => isObjectProperty(a, {
|
|
20
|
+
computed,
|
|
21
|
+
}) && isIdentifier(a.key, {
|
|
22
|
+
name,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const isObjectPropertyLiteral = (value) => (a) => isObjectProperty(a) && isStringLiteral(a.key, {
|
|
26
|
+
value,
|
|
27
|
+
});
|
|
15
28
|
|
|
16
29
|
const store = fullstore([]);
|
|
17
30
|
|
|
18
|
-
module.exports.report = () => '
|
|
31
|
+
module.exports.report = () => 'Avoid duplicate keys';
|
|
19
32
|
|
|
20
33
|
module.exports.replace = () => ({
|
|
21
34
|
__object: ({__object}) => {
|
|
@@ -27,11 +40,14 @@ module.exports.replace = () => ({
|
|
|
27
40
|
});
|
|
28
41
|
|
|
29
42
|
module.exports.match = () => ({
|
|
30
|
-
__object: ({__object}) => {
|
|
43
|
+
__object: ({__object}, path) => {
|
|
31
44
|
let is = false;
|
|
32
45
|
const newProperties = [];
|
|
33
46
|
const {properties} = __object;
|
|
34
|
-
|
|
47
|
+
|
|
48
|
+
const reversed = properties
|
|
49
|
+
.slice()
|
|
50
|
+
.reverse();
|
|
35
51
|
|
|
36
52
|
for (const prop of reversed) {
|
|
37
53
|
if (isSpreadElement(prop) && isIdentifier(prop.argument)) {
|
|
@@ -48,16 +64,19 @@ module.exports.match = () => ({
|
|
|
48
64
|
const {computed} = prop;
|
|
49
65
|
const {name} = prop.key;
|
|
50
66
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const isFirst = checkIfFirst(properties, newProperties, isObjectPropertyId, name, {
|
|
55
|
-
computed,
|
|
67
|
+
const props = traverseProperties(path, name, {
|
|
68
|
+
firstLevel: true,
|
|
56
69
|
});
|
|
57
70
|
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
71
|
+
if (props.length > 1) {
|
|
72
|
+
const isFirst = checkIfFirst(properties, newProperties, isObjectPropertyId, name, {
|
|
73
|
+
computed,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
if (!isFirst) {
|
|
77
|
+
is = true;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
61
80
|
}
|
|
62
81
|
}
|
|
63
82
|
|
|
@@ -87,4 +106,3 @@ function checkIfFirst(properties, newProperties, isFn, str, {computed} = {}) {
|
|
|
87
106
|
|
|
88
107
|
return !newLength || oldLength <= 1;
|
|
89
108
|
}
|
|
90
|
-
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-remove-duplicate-keys",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"type": "commonjs",
|
|
4
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
5
|
-
"description": "
|
|
6
|
-
"homepage": "
|
|
6
|
+
"description": "πPutout plugin adds ability to find and remove duplicate keys",
|
|
7
|
+
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-duplicate-keys#readme",
|
|
7
8
|
"main": "lib/remove-duplicate-keys.js",
|
|
8
9
|
"release": false,
|
|
9
10
|
"tag": false,
|
|
@@ -34,21 +35,21 @@
|
|
|
34
35
|
"key"
|
|
35
36
|
],
|
|
36
37
|
"devDependencies": {
|
|
37
|
-
"@putout/test": "^
|
|
38
|
-
"c8": "^
|
|
39
|
-
"eslint": "^8.0.
|
|
40
|
-
"eslint-plugin-
|
|
41
|
-
"eslint-plugin-putout": "^
|
|
42
|
-
"lerna": "^
|
|
43
|
-
"madrun": "^
|
|
44
|
-
"nodemon": "^
|
|
38
|
+
"@putout/test": "^8.0.0",
|
|
39
|
+
"c8": "^8.0.0",
|
|
40
|
+
"eslint": "^8.0.1",
|
|
41
|
+
"eslint-plugin-n": "^16.0.0",
|
|
42
|
+
"eslint-plugin-putout": "^22.0.0",
|
|
43
|
+
"lerna": "^6.0.1",
|
|
44
|
+
"madrun": "^10.0.0",
|
|
45
|
+
"nodemon": "^3.0.1"
|
|
45
46
|
},
|
|
46
47
|
"peerDependencies": {
|
|
47
|
-
"putout": ">=
|
|
48
|
+
"putout": ">=34"
|
|
48
49
|
},
|
|
49
50
|
"license": "MIT",
|
|
50
51
|
"engines": {
|
|
51
|
-
"node": ">=
|
|
52
|
+
"node": ">=18"
|
|
52
53
|
},
|
|
53
54
|
"publishConfig": {
|
|
54
55
|
"access": "public"
|