Skip to content

Commit f130427

Browse files
committed
feature: @putout/plugin-printer: apply-types: add
1 parent 13dda23 commit f130427

File tree

10 files changed

+146
-0
lines changed

10 files changed

+146
-0
lines changed

packages/plugin-printer/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ npm i @putout/plugin-printer -D
1313

1414
## Rules
1515

16+
-[add-args](#add-args);
17+
-[apply-breakline](#apply-breakline);
18+
-[apply-linebreak](#apply-linebreak);
19+
-[apply-types](#apply-types);
20+
-[apply-computed-print](#apply-computed-print);
21+
-[declare](#declare);
22+
-[remove-args](#remove-args);
23+
24+
## Config
25+
1626
```json
1727
{
1828
"rules": {
@@ -42,6 +52,66 @@ print.breakline();
4252
print.linebreak();
4353
```
4454

55+
## apply-types
56+
57+
```diff
58+
-const {isIdentifier} = require('@babel/types');
59+
+const {types} = require('@babel/types');
60+
+const {isIdentifier} = types;
61+
```
62+
63+
## add-args
64+
65+
### ❌ Example of incorrect code
66+
67+
```js
68+
module.exports = {
69+
TSPropertySignature(path) {
70+
const {optional} = path.node;
71+
print('__key');
72+
maybe.print(optional, '?');
73+
},
74+
};
75+
```
76+
77+
### ✅ Example of correct code
78+
79+
```js
80+
module.exports = {
81+
TSPropertySignature(path, {print, maybe}) {
82+
const {optional} = path.node;
83+
print('__key');
84+
maybe.print(optional, '?');
85+
},
86+
};
87+
```
88+
89+
## add-args
90+
91+
### ❌ Example of incorrect code
92+
93+
```js
94+
module.exports = {
95+
TSPropertySignature(path) {
96+
const {optional} = path.node;
97+
print('__key');
98+
maybe.print(optional, '?');
99+
},
100+
};
101+
```
102+
103+
### ✅ Example of correct code
104+
105+
```js
106+
module.exports = {
107+
TSPropertySignature(path, {print, maybe}) {
108+
const {optional} = path.node;
109+
print('__key');
110+
maybe.print(optional, '?');
111+
},
112+
};
113+
```
114+
45115
## add-args
46116

47117
### ❌ Example of incorrect code
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
const {types} = require('@putout/babel');
3+
const {isIdentifier} = types;
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const {isIdentifier} = require('@putout/babel');
2+
3+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const a = require('@putout/babel');
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const {types} = require('putout');
4+
const {isObjectPattern} = types;
5+
6+
const {keys} = Object;
7+
const TYPES = keys(types);
8+
9+
module.exports.report = () => `require: ('@putout/babel') -> ('putout/babel').types`;
10+
11+
module.exports.match = () => ({
12+
'const __a = require("@putout/babel")': ({__a}) => {
13+
if (!isObjectPattern(__a))
14+
return false;
15+
16+
const [first] = __a.properties;
17+
18+
return TYPES.includes(first.value.name);
19+
},
20+
});
21+
22+
module.exports.replace = () => ({
23+
'const __a = require("@putout/babel")': 'const __a = require("@putout/babel").types',
24+
'const __a = require("@putout/babel").types': `{
25+
const {types} = require("@putout/babel");
26+
const __a = types;
27+
}`,
28+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
printer: 'putout',
8+
plugins: [
9+
['apply-types', plugin],
10+
],
11+
});
12+
13+
test('printer: apply-types: report', (t) => {
14+
t.report('apply-types', `require: ('@putout/babel') -> ('putout/babel').types`);
15+
t.end();
16+
});
17+
18+
test('printer: apply-types: transform', (t) => {
19+
t.transform('apply-types');
20+
t.end();
21+
});
22+
23+
test('printer: apply-types: no report: no pattern', (t) => {
24+
t.noReport('no-pattern');
25+
t.end();
26+
});

packages/plugin-printer/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const applyLinebreak = require('./apply-linebreak');
66
const applyComputedPrint = require('./apply-computed-print');
77
const addArgs = require('./add-args');
88
const declare = require('./declare');
9+
const applyTypes = require('./apply-types');
910

1011
module.exports.rules = {
1112
'remove-args': removeArgs,
@@ -14,4 +15,5 @@ module.exports.rules = {
1415
'apply-computed-print': applyComputedPrint,
1516
'add-args': addArgs,
1617
declare,
18+
'apply-types': applyTypes,
1719
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
const {types} = require('@putout/babel');
3+
const {isIdentifier} = types;
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const {isIdentifier} = require('@putout/babel');
2+
3+

packages/plugin-printer/test/printer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ test('plugin-printer: transform: declare', (t) => {
3939
t.transform('declare');
4040
t.end();
4141
});
42+
43+
test('plugin-printer: transform: apply-types', (t) => {
44+
t.transform('apply-types');
45+
t.end();
46+
});

0 commit comments

Comments
 (0)