@putout/plugin-remove-duplicate-keys 2.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) coderaiser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # @putout/plugin-remove-duplicate-keys [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL]
2
+
3
+ [NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-remove-duplicate-keys.svg?style=flat&longCache=true
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
+
8
+ `putout` plugin adds ability to find and remove duplecate keys.
9
+
10
+ ## Install
11
+
12
+ ```
13
+ npm i @putout/plugin-remove-duplicate-keys
14
+ ```
15
+
16
+ ## Rule
17
+
18
+ ```json
19
+ {
20
+ "rules": {
21
+ "remove-duplicate-keys": "on"
22
+ }
23
+ }
24
+ ```
25
+
26
+ ## ❌ Incorrect code example
27
+
28
+ ```js
29
+ const a = {
30
+ x: 'hello',
31
+ ...z,
32
+ x: 'world',
33
+ };
34
+ ```
35
+
36
+ ## ✅ Correct code Example
37
+
38
+ ```js
39
+ const a = {
40
+ ...z,
41
+ x: 'world',
42
+ };
43
+ ```
44
+
45
+ ## License
46
+
47
+ MIT
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ const {types} = require('putout');
4
+ const fullstore = require('fullstore');
5
+ const {
6
+ isSpreadElement,
7
+ isIdentifier,
8
+ isObjectProperty,
9
+ isStringLiteral,
10
+ } = types;
11
+
12
+ const isSpreadId = (name) => (a) => isSpreadElement(a) && isIdentifier(a.argument, {name});
13
+ const isObjectPropertyId = (name, computed) => (a) => isObjectProperty(a, {computed}) && isIdentifier(a.key, {name});
14
+ const isObjectPropertyLiteral = (value) => (a) => isObjectProperty(a) && isStringLiteral(a.key, {value});
15
+
16
+ const store = fullstore([]);
17
+
18
+ module.exports.report = () => 'Duplicate keys should be avoided';
19
+
20
+ module.exports.replace = () => ({
21
+ __object: ({__object}) => {
22
+ __object.properties = store();
23
+ store([]);
24
+
25
+ return __object;
26
+ },
27
+ });
28
+
29
+ module.exports.match = () => ({
30
+ __object: ({__object}) => {
31
+ let is = false;
32
+ const newProperties = [];
33
+ const {properties} = __object;
34
+ const reversed = properties.slice().reverse();
35
+
36
+ for (const prop of reversed) {
37
+ if (isSpreadElement(prop) && isIdentifier(prop.argument)) {
38
+ const {name} = prop.argument;
39
+ const isFirst = checkIfFirst(properties, newProperties, isSpreadId, name);
40
+
41
+ if (!isFirst) {
42
+ is = true;
43
+ continue;
44
+ }
45
+ }
46
+
47
+ if (isObjectProperty(prop) && isIdentifier(prop.key)) {
48
+ const {computed} = prop;
49
+ const {name} = prop.key;
50
+
51
+ if (name !== prop.value.name)
52
+ continue;
53
+
54
+ const isFirst = checkIfFirst(properties, newProperties, isObjectPropertyId, name, {
55
+ computed,
56
+ });
57
+
58
+ if (!isFirst) {
59
+ is = true;
60
+ continue;
61
+ }
62
+ }
63
+
64
+ if (isObjectProperty(prop) && isStringLiteral(prop.key)) {
65
+ const {value} = prop.key;
66
+ const isFirst = checkIfFirst(properties, newProperties, isObjectPropertyLiteral, value);
67
+
68
+ if (!isFirst) {
69
+ is = true;
70
+ continue;
71
+ }
72
+ }
73
+
74
+ newProperties.unshift(prop);
75
+ }
76
+
77
+ if (is)
78
+ store(newProperties);
79
+
80
+ return is;
81
+ },
82
+ });
83
+
84
+ function checkIfFirst(properties, newProperties, isFn, str, {computed} = {}) {
85
+ const oldLength = properties.filter(isFn(str, computed)).length;
86
+ const newLength = newProperties.filter(isFn(str, computed)).length;
87
+
88
+ return !newLength || oldLength <= 1;
89
+ }
90
+
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@putout/plugin-remove-duplicate-keys",
3
+ "version": "2.1.0",
4
+ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
+ "description": "putout plugin adds ability to find and remove process.exit",
6
+ "homepage": "http://github.com/coderaiser/putout",
7
+ "main": "lib/remove-duplicate-keys.js",
8
+ "release": false,
9
+ "tag": false,
10
+ "changelog": false,
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git://github.com/coderaiser/putout.git"
14
+ },
15
+ "scripts": {
16
+ "test": "madrun test",
17
+ "watch:test": "madrun watch:test",
18
+ "lint": "madrun lint",
19
+ "fresh:lint": "madrun fresh:lint",
20
+ "lint:fresh": "madrun lint:fresh",
21
+ "fix:lint": "madrun fix:lint",
22
+ "coverage": "madrun coverage",
23
+ "report": "madrun report"
24
+ },
25
+ "dependencies": {
26
+ "fullstore": "^3.0.0"
27
+ },
28
+ "keyswords": [
29
+ "putout",
30
+ "putout-plugin",
31
+ "putout-plugin-remove",
32
+ "plugin",
33
+ "duplicate",
34
+ "key"
35
+ ],
36
+ "devDependencies": {
37
+ "@putout/test": "^3.0.0",
38
+ "c8": "^7.5.0",
39
+ "eslint": "^8.0.0-beta.0",
40
+ "eslint-plugin-node": "^11.0.0",
41
+ "eslint-plugin-putout": "^10.0.0",
42
+ "lerna": "^4.0.0",
43
+ "madrun": "^8.0.1",
44
+ "nodemon": "^2.0.1"
45
+ },
46
+ "peerDependencies": {
47
+ "putout": ">=9"
48
+ },
49
+ "license": "MIT",
50
+ "engines": {
51
+ "node": ">=10"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public"
55
+ }
56
+ }