Skip to content

Commit 06e325e

Browse files
committed
feature: @putout/plugin-printer: check-type-passed-to-type-checker
1 parent dd3603b commit 06e325e

9 files changed

Lines changed: 152 additions & 0 deletions

File tree

packages/plugin-printer/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ npm i @putout/plugin-printer -D
2020
-[apply-create-test-url](#apply-create-test-url);
2121
-[apply-linebreak](#apply-linebreak);
2222
-[apply-types](#apply-types);
23+
-[check-type-passed-to-type-checker](#check-type-passed-to-type-checker);
2324
-[declare](#declare);
2425
-[remove-args](#remove-args);
2526
-[remove-useless-maybe](#remove-useless-maybe);
@@ -39,6 +40,7 @@ npm i @putout/plugin-printer -D
3940
"printer/apply-computed-print": "on",
4041
"printer/apply-create-test-url": "on",
4142
"printer/apply-types": "on",
43+
"printer/check-type-passed-to-type-checker": "on",
4244
"printer/declare": "on",
4345
"printer/remove-args": "on",
4446
"printer/remove-useless-maybe": "on",
@@ -228,6 +230,26 @@ export const beforeIf = createTypeChecker([
228230
]);
229231
```
230232
233+
## check-type-passed-to-type-checker
234+
235+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9b35f5f2f23bece832f0f87c929d30e2/b08df8ef8ce6c2a03bceebe6119846c45a78a119).
236+
237+
### ❌ Example of incorrect code
238+
239+
```js
240+
export const beforeIf = createTypeChecker([
241+
['- : -> WrongType'],
242+
]);
243+
```
244+
245+
### ✅ Example of correct code
246+
247+
```js
248+
export const beforeIf = createTypeChecker([
249+
['- : -> 🧨 WrongType'],
250+
]);
251+
```
252+
231253
## declare
232254
233255
### ❌ Example of incorrect code
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const beforeIf = createTypeChecker([
2+
'- : -> !StringLiteral',
3+
'- : -> !🧨 WrongType',
4+
['- : -> BlockStatement'],
5+
['- : -> 🧨 WrongType'],
6+
['- : ->', isBlockStatement],
7+
['-', isBlockStatement],
8+
]);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const beforeIf = createTypeChecker([
2+
'- : -> !StringLiteral',
3+
'- : -> !WrongType',
4+
['- : -> BlockStatement'],
5+
['- : -> WrongType'],
6+
['- : ->', isBlockStatement],
7+
['-', isBlockStatement],
8+
]);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {types, operator} from 'putout';
2+
3+
const {
4+
isIdentifier,
5+
isCallExpression,
6+
} = types;
7+
8+
const {setLiteralValue} = operator;
9+
const ARROW = ' -> ';
10+
const ARROW_NOT = ' -> !';
11+
const ARROW_LENGTH = ARROW.length;
12+
const ARROW_NOT_LENGTH = ARROW_NOT.length;
13+
const MARK = '🧨';
14+
15+
export const report = ({type}) => {
16+
return `Unknown type detected: '${type}'`;
17+
};
18+
19+
export const fix = ({path, type}) => {
20+
const {value} = path.node;
21+
setLiteralValue(path, value.replace(type, `${MARK} ${type}`));
22+
};
23+
24+
export const traverse = ({push}) => ({
25+
StringLiteral(path) {
26+
const call = path.find(isCallExpression);
27+
28+
if (!call)
29+
return;
30+
31+
if (!isIdentifier(call.node.callee, {name: 'createTypeChecker'}))
32+
return;
33+
34+
const {value} = path.node;
35+
36+
if (value.includes(MARK))
37+
return;
38+
39+
if (value.endsWith('->'))
40+
return;
41+
42+
if (value.includes('!')) {
43+
const arrowNotIndex = value.indexOf(ARROW_NOT);
44+
const typeNotIndex = arrowNotIndex + ARROW_NOT_LENGTH;
45+
46+
const type = value.slice(typeNotIndex, value.length);
47+
48+
if (!types[`is${type}`])
49+
push({
50+
path,
51+
type,
52+
});
53+
54+
return;
55+
}
56+
57+
const arrowIndex = value.indexOf(ARROW);
58+
const typeIndex = arrowIndex + ARROW_LENGTH;
59+
60+
const type = value.slice(typeIndex, value.length);
61+
62+
if (!type)
63+
return;
64+
65+
if (!types[`is${type}`])
66+
push({
67+
path,
68+
type,
69+
});
70+
},
71+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['check-type-passed-to-type-checker', plugin],
7+
],
8+
});
9+
10+
test('printer: check-type-passed-to-type-checker: report', (t) => {
11+
t.report('check-type-passed-to-type-checker', `Unknown type detected: 'WrongType'`);
12+
t.end();
13+
});
14+
15+
test('printer: check-type-passed-to-type-checker: transform', (t) => {
16+
t.transform('check-type-passed-to-type-checker');
17+
t.end();
18+
});

packages/plugin-printer/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as checkTypePassedToTypeChecker from './check-type-passed-to-type-checker/index.js';
12
import * as removeUselessSpacesFromTypeChecker from './remove-useless-spaces-from-type-checker/index.js';
23
import * as addMissingSpacesToTypeChecker from './add-missing-spaces-to-type-checker/index.js';
34
import * as removeTrailingSpacesFromTypeChecker from './remove-trailing-spaces-from-type-checker/index.js';
@@ -26,4 +27,5 @@ export const rules = {
2627
'remove-trailing-spaces-from-type-checker': removeTrailingSpacesFromTypeChecker,
2728
'add-missing-spaces-to-type-checker': addMissingSpacesToTypeChecker,
2829
'remove-useless-spaces-from-type-checker': removeUselessSpacesFromTypeChecker,
30+
'check-type-passed-to-type-checker': checkTypePassedToTypeChecker,
2931
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {types} from '@putout/babel';
2+
3+
const {isBlockStatement} = types;
4+
5+
export const beforeIf = createTypeChecker([
6+
'-: -> !StringLiteral',
7+
['-: -> BlockStatement'],
8+
['-: -> 🧨 WrongType'],
9+
['-: ->', isBlockStatement],
10+
['-', isBlockStatement],
11+
]);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const beforeIf = createTypeChecker([
2+
'- : -> !StringLiteral',
3+
['- : -> BlockStatement'],
4+
['- : -> WrongType'],
5+
['- : ->', isBlockStatement],
6+
['-', isBlockStatement],
7+
]);

packages/plugin-printer/test/printer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ test('plugin-printer: transform: remove-useless-spaces-from-type-checker', (t) =
7171
t.transform('remove-useless-spaces-from-type-checker');
7272
t.end();
7373
});
74+
75+
test('plugin-printer: transform: check-type-passed-to-type-checker', (t) => {
76+
t.transform('check-type-passed-to-type-checker');
77+
t.end();
78+
});

0 commit comments

Comments
 (0)