Skip to content

Commit bf128ff

Browse files
committed
feature: @putout/plugin-apply-destructuring: convert-object-to-array: add
1 parent 7e9a038 commit bf128ff

File tree

10 files changed

+101
-0
lines changed

10 files changed

+101
-0
lines changed

β€Žpackages/plugin-apply-destructuring/README.mdβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ npm i @putout/plugin-apply-destructuring
2020
- βœ… [object](#object);
2121
- βœ… [array](#array);
2222
- βœ… [falsy](#falsy);
23+
- βœ… [convert-object-to-array](#convert-object-to-array);
2324

2425
## Config
2526

@@ -95,6 +96,22 @@ const {maxElementsInOneLine} = {
9596
const {maxElementsInOneLine} = options;
9697
```
9798

99+
## convert-object-to-array
100+
101+
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/a1d26daf8bb83ee3ea1c0b62a6ad3afd/cef9b4d27c9dbb0d413a935b0359a6fe9b50364f).
102+
103+
## ❌ Example of incorrect code
104+
105+
```js
106+
const {0: a, 1: b} = c;
107+
```
108+
109+
## βœ… Example of correct code
110+
111+
```js
112+
const [a, b] = c;
113+
```
114+
98115
## License
99116

100117
MIT
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const [a, b] = c;
2+
3+
const {...x} = c;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const {0: a, 1: b} = c;
2+
3+
const {...x} = c;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = ({rule, plugin, msg, options}, {}) => {};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {types, operator} from 'putout';
2+
3+
const {replaceWith} = operator;
4+
const {
5+
isObjectProperty,
6+
arrayPattern,
7+
} = types;
8+
9+
export const report = () => `Use array destructuring instead of object destructuring`;
10+
11+
export const fix = (path) => {
12+
const elements = [];
13+
14+
for (const {value} of path.node.properties) {
15+
elements.push(value);
16+
}
17+
18+
const array = arrayPattern(elements);
19+
replaceWith(path, array);
20+
};
21+
22+
export const traverse = ({push}) => ({
23+
ObjectPattern(path) {
24+
const properties = path.get('properties');
25+
26+
if (!properties.length)
27+
return;
28+
29+
for (const [i, prop] of properties.entries()) {
30+
if (!isObjectProperty(prop))
31+
return;
32+
33+
const {key} = prop.node;
34+
35+
if (i !== key.value)
36+
return;
37+
}
38+
39+
push(path);
40+
},
41+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['convert-object-to-array', plugin],
7+
],
8+
});
9+
10+
test('apply-destructuring: convert-object-to-array: report', (t) => {
11+
t.report('convert-object-to-array', `Use array destructuring instead of object destructuring`);
12+
t.end();
13+
});
14+
15+
test('apply-destructuring: convert-object-to-array: transform', (t) => {
16+
t.transform('convert-object-to-array');
17+
t.end();
18+
});
19+
20+
test('apply-destructuring: convert-object-to-array: no report: no-indexes', (t) => {
21+
t.noReport('no-indexes');
22+
t.end();
23+
});

β€Žpackages/plugin-apply-destructuring/lib/index.jsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as convertObjectToArray from './convert-object-to-array/index.js';
12
import * as object from './object/index.js';
23
import * as array from './array/index.js';
34
import * as falsy from './falsy/index.js';
@@ -6,4 +7,5 @@ export const rules = {
67
object,
78
array,
89
falsy,
10+
'convert-object-to-array': convertObjectToArray,
911
};

β€Žpackages/plugin-apply-destructuring/test/apply-destructuring.jsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ test('plugin-apply-destructuring: transform: falsy', (t) => {
5151
t.transform('falsy');
5252
t.end();
5353
});
54+
55+
test('plugin-apply-destructuring: transform: convert-object-to-array', (t) => {
56+
t.transform('convert-object-to-array');
57+
t.end();
58+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const [a, b] = c;
2+
3+
const {...x} = c;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const {0: a, 1: b} = c;
2+
3+
const {...x} = c;

0 commit comments

Comments
Β (0)