@putout/plugin-printer 8.4.0 → 8.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/lib/check-type-passed-to-type-checker/index.js +71 -0
- package/lib/index.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ npm i @putout/plugin-printer -D
|
|
|
20
20
|
- ✅ [apply-create-test-url](#apply-create-test-url);
|
|
21
21
|
- ✅ [apply-linebreak](#apply-linebreak);
|
|
22
22
|
- ✅ [apply-types](#apply-types);
|
|
23
|
+
- ✅ [check-type-passed-to-type-checker](#check-type-passed-to-type-checker);
|
|
23
24
|
- ✅ [declare](#declare);
|
|
24
25
|
- ✅ [remove-args](#remove-args);
|
|
25
26
|
- ✅ [remove-useless-maybe](#remove-useless-maybe);
|
|
@@ -39,6 +40,7 @@ npm i @putout/plugin-printer -D
|
|
|
39
40
|
"printer/apply-computed-print": "on",
|
|
40
41
|
"printer/apply-create-test-url": "on",
|
|
41
42
|
"printer/apply-types": "on",
|
|
43
|
+
"printer/check-type-passed-to-type-checker": "on",
|
|
42
44
|
"printer/declare": "on",
|
|
43
45
|
"printer/remove-args": "on",
|
|
44
46
|
"printer/remove-useless-maybe": "on",
|
|
@@ -228,6 +230,26 @@ export const beforeIf = createTypeChecker([
|
|
|
228
230
|
]);
|
|
229
231
|
```
|
|
230
232
|
|
|
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
|
+
|
|
231
253
|
## declare
|
|
232
254
|
|
|
233
255
|
### ❌ Example of incorrect code
|
|
@@ -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
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as checkTypePassedToTypeChecker from './check-type-passed-to-type-checker/index.js';
|
|
1
2
|
import * as removeUselessSpacesFromTypeChecker from './remove-useless-spaces-from-type-checker/index.js';
|
|
2
3
|
import * as addMissingSpacesToTypeChecker from './add-missing-spaces-to-type-checker/index.js';
|
|
3
4
|
import * as removeTrailingSpacesFromTypeChecker from './remove-trailing-spaces-from-type-checker/index.js';
|
|
@@ -26,4 +27,5 @@ export const rules = {
|
|
|
26
27
|
'remove-trailing-spaces-from-type-checker': removeTrailingSpacesFromTypeChecker,
|
|
27
28
|
'add-missing-spaces-to-type-checker': addMissingSpacesToTypeChecker,
|
|
28
29
|
'remove-useless-spaces-from-type-checker': removeUselessSpacesFromTypeChecker,
|
|
30
|
+
'check-type-passed-to-type-checker': checkTypePassedToTypeChecker,
|
|
29
31
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-printer",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "🐊Putout plugin adds support of transformations for @putout/printer",
|