Skip to content

Commit 4dfb778

Browse files
committed
feature: @putout/plugin-remove-duplicate-keys: argument name clash support
1 parent 208422a commit 4dfb778

File tree

8 files changed

+55
-9
lines changed

8 files changed

+55
-9
lines changed

docs/syntax-errors.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,12 @@ function get() {
329329
```
330330

331331
</details>
332+
333+
<details><summary>Argument name clash</summary>
334+
335+
```diff
336+
-const a = ({b, b, ...c}) => {};
337+
+const a = ({b, ...c}) => {};
338+
```
339+
340+
</details>

packages/plugin-remove-duplicate-keys/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ const a = {
4444
};
4545
```
4646

47+
> SyntaxError: Duplicate parameter name not allowed in this context
48+
>
49+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Duplicate_parameter)
50+
51+
Argument name clash:
52+
53+
```diff
54+
-const a = ({b, b, ...c}) => {};
55+
+const a = ({b, ...c}) => {};
56+
```
57+
4758
## License
4859

4960
MIT

packages/plugin-remove-duplicate-keys/lib/remove-duplicate-keys.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
const {types, operator} = require('putout');
44
const fullstore = require('fullstore');
5+
const {
6+
compare,
7+
traverseProperties,
8+
extract,
9+
} = operator;
510

611
const {
712
isSpreadElement,
813
isIdentifier,
914
isMemberExpression,
1015
isObjectProperty,
11-
isObjectPattern,
1216
isStringLiteral,
17+
isObjectPattern,
1318
} = types;
1419

15-
const {
16-
traverseProperties,
17-
extract,
18-
} = operator;
19-
2020
const isSpreadId = (name) => (a) => isSpreadElement(a) && isIdentifier(a.argument, {
2121
name,
2222
});
@@ -54,14 +54,14 @@ module.exports.match = () => ({
5454
const newProperties = [];
5555
const {properties} = __object;
5656

57-
if (isObjectPattern(__object))
58-
return false;
59-
6057
const reversed = properties
6158
.slice()
6259
.reverse();
6360

6461
for (const prop of reversed) {
62+
if (isObjectPattern(path) && !compare(prop.key, prop.value))
63+
continue;
64+
6565
if (isSpreadElement(prop) && isIdentifier(prop.argument)) {
6666
const {name} = prop.argument;
6767
const isFirst = checkIfFirst(properties, newProperties, isSpreadId, name);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const fix = ({ast, source}) => {};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const {
2+
a: {
3+
b,
4+
},
5+
a: {
6+
b: x,
7+
},
8+
} = c;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const {b, b: x} = a;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const fix = ({ast, source, source}) => {};

packages/plugin-remove-duplicate-keys/test/remove-duplicate-keys.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,18 @@ test('remove duplicate-keys: transform: array', (t) => {
6363
t.transform('array');
6464
t.end();
6565
});
66+
67+
test('remove duplicate-keys: transform: object-pattern', (t) => {
68+
t.transform('object-pattern');
69+
t.end();
70+
});
71+
72+
test('remove duplicate-keys: no report: object-pattern-rename', (t) => {
73+
t.noReport('object-pattern-rename');
74+
t.end();
75+
});
76+
77+
test('remove duplicate-keys: no report: object-pattern-nested', (t) => {
78+
t.noReport('object-pattern-nested');
79+
t.end();
80+
});

0 commit comments

Comments
 (0)