Skip to content

Commit c672afc

Browse files
committed
feature: @putout/plugin-apply-shorthand-properties: rename
1 parent 199682d commit c672afc

File tree

9 files changed

+111
-19
lines changed

9 files changed

+111
-19
lines changed

packages/plugin-apply-shorthand-properties/.eslintrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
2+
"overrides": [{
3+
"files": ["*.md{js}"],
4+
"rules": {
5+
"no-useless-rename": "off"
6+
}
7+
}],
28
"extends": [
39
"plugin:n/recommended",
410
"plugin:putout/recommended"

packages/plugin-apply-shorthand-properties/README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ npm i @putout/plugin-apply-shorthand-properties -D
1717
{
1818
"rules": {
1919
"apply-shorthand-properties": ["on", {
20-
"ignore": []
20+
"ignore": [],
21+
"rename": false
2122
}]
2223
},
2324
"plugins": [
@@ -26,7 +27,23 @@ npm i @putout/plugin-apply-shorthand-properties -D
2627
}
2728
```
2829

29-
## ❌ Example of incorrect code
30+
## With default options
31+
32+
### ❌ Example of incorrect code
33+
34+
```js
35+
const {a: a} = b;
36+
```
37+
38+
### ✅ Example of correct code
39+
40+
```js
41+
const {a} = b;
42+
```
43+
44+
## When `rename` enabled
45+
46+
### ❌ Example of incorrect code
3047

3148
```js
3249
const AUTH_SESSION = 'xx';
@@ -37,7 +54,7 @@ export const setSession = (session) => ({
3754
});
3855
```
3956

40-
## ✅ Example of correct code
57+
### ✅ Example of correct code
4158

4259
```js
4360
const type = 'xx';
@@ -48,6 +65,13 @@ export const setSession = (payload) => ({
4865
});
4966
```
5067

68+
## Comparison
69+
70+
Linter | Rule | Fix
71+
--------|-------|------------|
72+
🐊 **Putout** | [`apply-shorthand-properties`](https://github.com/coderaiser/putout/tree/master/packages/plugin-apply-shorthand-properties#readme) | ✅
73+
**ESLint** | [`no-useless-rename`](https://eslint.org/docs/rules/no-useless-rename) | ❌
74+
5175
## License
5276

5377
MIT

packages/plugin-apply-shorthand-properties/lib/apply-shorthand-properties.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const {findBinding, rename} = operator;
44

55
export const report = () => `Use shorthand properties`;
66

7-
export const fix = ({path, from, to}) => {
8-
rename(path, from, to);
7+
export const fix = ({path, from, to, toRename}) => {
8+
if (toRename)
9+
rename(path, from, to);
10+
911
path.node.shorthand = true;
1012
};
1113

@@ -20,7 +22,7 @@ export const traverse = ({push, options}) => ({
2022
const valuePath = propPath.get('value');
2123
const keyPath = propPath.get('key');
2224

23-
const {ignore = []} = options;
25+
const {rename, ignore = []} = options;
2426

2527
const from = getName(valuePath);
2628
const to = getName(keyPath);
@@ -34,6 +36,17 @@ export const traverse = ({push, options}) => ({
3436
if (/^[A-Z].+$/.test(from))
3537
continue;
3638

39+
if (!rename) {
40+
if (from === to)
41+
push({
42+
path: propPath,
43+
from,
44+
to,
45+
});
46+
47+
continue;
48+
}
49+
3750
const bindingFrom = findBinding(propPath, from);
3851
const bindingTo = findBinding(propPath, to);
3952

@@ -52,6 +65,7 @@ export const traverse = ({push, options}) => ({
5265
path: propPath,
5366
from,
5467
to,
68+
toRename: true,
5569
});
5670
}
5771
},

packages/plugin-apply-shorthand-properties/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@
2828
"keywords": [
2929
"putout",
3030
"putout-plugin",
31-
"putout-plugin-remove",
32-
"plugin",
33-
"remove",
34-
"only"
31+
"putout-plugin-apply",
32+
"apply-shorthand-properties"
3533
],
3634
"devDependencies": {
3735
"@putout/plugin-remove-unused-variables": "*",

packages/plugin-apply-shorthand-properties/test/apply-shorthand-properties.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ const test = createTest(import.meta.url, {
99
],
1010
});
1111

12+
const testWithRemove = createTest(import.meta.url, {
13+
printer: 'putout',
14+
plugins: [
15+
['apply-shorthand-properties', applyShorthandProperties],
16+
['rm-unused-vars', removeUnusedVariables],
17+
],
18+
});
19+
1220
test('plugin-apply-shorthand-properties: report', (t) => {
13-
t.report('object', 'Use shorthand properties');
21+
t.reportWithOptions('object', 'Use shorthand properties', {
22+
rename: true,
23+
});
1424
t.end();
1525
});
1626

@@ -20,7 +30,19 @@ test('plugin-apply-shorthand-properties: no report: shorthand', (t) => {
2030
});
2131

2232
test('plugin-apply-shorthand-properties: transform', (t) => {
23-
t.transform('object');
33+
t.transformWithOptions('object', {
34+
rename: true,
35+
});
36+
t.end();
37+
});
38+
39+
test('plugin-apply-shorthand-properties: transform: destructuring', (t) => {
40+
t.transform('destructuring');
41+
t.end();
42+
});
43+
44+
test('plugin-apply-shorthand-properties: transform: rename-and-destructuring', (t) => {
45+
t.transform('rename-and-destructuring');
2446
t.end();
2547
});
2648

@@ -42,22 +64,30 @@ test('plugin-apply-shorthand-properties: no transform: not-valid', (t) => {
4264
});
4365

4466
test('plugin-apply-shorthand-properties: no transform: destructuring', (t) => {
45-
t.noTransform('destr');
67+
t.noTransformWithOptions('destr', {
68+
rename: true,
69+
});
4670
t.end();
4771
});
4872

4973
test('plugin-apply-shorthand-properties: no transform: import', (t) => {
50-
t.noTransform('import');
74+
t.noTransformWithOptions('import', {
75+
rename: true,
76+
});
5177
t.end();
5278
});
5379

5480
test('plugin-apply-shorthand-properties: no transform: name exists', (t) => {
55-
t.noTransform('name-exists');
81+
t.noTransformWithOptions('name-exists', {
82+
rename: true,
83+
});
5684
t.end();
5785
});
5886

5987
test('plugin-apply-shorthand-properties: no transform: names overlap', (t) => {
60-
t.noTransform('overlap');
88+
t.noTransformWithOptions('overlap', {
89+
rename: true,
90+
});
6191
t.end();
6292
});
6393

@@ -71,9 +101,9 @@ test('plugin-apply-shorthand-properties: no transform: import declaration', (t)
71101
t.end();
72102
});
73103

74-
test('plugin-apply-shorthand-properties: transform: assign', (t) => {
75-
t.transform('assign', {
76-
'rm-unused-vars': removeUnusedVariables,
104+
testWithRemove('plugin-apply-shorthand-properties: transform: assign', (t) => {
105+
t.transformWithOptions('assign', {
106+
rename: true,
77107
});
78108
t.end();
79109
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function x({b}) {
2+
return b;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function x({b: b}) {
2+
return b;
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const authSession = 'xx';
2+
3+
export const setSession = (session) => ({
4+
type: authSession,
5+
payload: session,
6+
a,
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const authSession = 'xx';
2+
3+
export const setSession = (session) => ({
4+
type: authSession,
5+
payload: session,
6+
a: a,
7+
});

0 commit comments

Comments
 (0)