@putout/plugin-printer 8.15.0 → 8.16.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 +24 -0
- package/lib/index.js +2 -0
- package/lib/remove-useless-path-from-type-checker/index.js +70 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,6 +34,7 @@ npm i @putout/plugin-printer -D
|
|
|
34
34
|
- ✅ [remove-useless-colon-from-type-checker](#remove-useless-colon-from-type-checker);
|
|
35
35
|
- ✅ [remove-useless-tuples-from-type-checker](#remove-useless-tuples-from-type-checker);
|
|
36
36
|
- ✅ [remove-useless-not-from-type-checker](#remove-useless-not-from-type-checker);
|
|
37
|
+
- ✅ [remove-useless-path-from-type-checker](#remove-useless-path-from-type-checker);
|
|
37
38
|
- ✅ [reverse-comparison-in-type-checker](#reverse-comparison-in-type-checker);
|
|
38
39
|
|
|
39
40
|
## Config
|
|
@@ -60,6 +61,7 @@ npm i @putout/plugin-printer -D
|
|
|
60
61
|
"printer/remove-useless-spaces-from-type-checker": "on",
|
|
61
62
|
"printer/remove-useless-colon-from-type-checker": "on",
|
|
62
63
|
"printer/remove-useless-not-from-type-checker": "on",
|
|
64
|
+
"printer/remove-useless-path-from-type-checker": "on",
|
|
63
65
|
"printer/remove-useless-tuples-from-type-checker": "on",
|
|
64
66
|
"printer/reverse-comparison-in-type-checker": "on"
|
|
65
67
|
}
|
|
@@ -320,6 +322,28 @@ export const beforeIf = createTypeChecker([
|
|
|
320
322
|
]);
|
|
321
323
|
```
|
|
322
324
|
|
|
325
|
+
## remove-useless-path-from-type-checker
|
|
326
|
+
|
|
327
|
+
Selector always starts from `path`, so it can be `parentPath.`, or `node.`, but never `path.`, sinse it is self reference.
|
|
328
|
+
|
|
329
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/ef8e1dc1036a4a2fcc99b32c3dc661e2/bc43a082dc9cb5ed8a8afe6964174f0d1be2e1c1).
|
|
330
|
+
|
|
331
|
+
### ❌ Example of incorrect code
|
|
332
|
+
|
|
333
|
+
```js
|
|
334
|
+
export const beforeIf = createTypeChecker([
|
|
335
|
+
['-: path.parentPath -> ', isCoupleLines],
|
|
336
|
+
]);
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### ✅ Example of correct code
|
|
340
|
+
|
|
341
|
+
```js
|
|
342
|
+
export const beforeIf = createTypeChecker([
|
|
343
|
+
['-: parentPath -> ', isCoupleLines],
|
|
344
|
+
]);
|
|
345
|
+
```
|
|
346
|
+
|
|
323
347
|
## remove-useless-tuples-from-type-checker
|
|
324
348
|
|
|
325
349
|
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/f53f386b607115e0a96f7e294e34761e/cefd72aba4fcade59bbf8faa59739bac5c2fe057).
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as removeUselessPathFromTypeChecker from './remove-useless-path-from-type-checker/index.js';
|
|
1
2
|
import * as reverseComparisonInTypeChecker from './reverse-comparison-in-type-checker/index.js';
|
|
2
3
|
import * as removeUselessNotFromTypeChecker from './remove-useless-not-from-type-checker/index.js';
|
|
3
4
|
import * as removeUselessColonFromTypeChecker from './remove-useless-colon-from-type-checker/index.js';
|
|
@@ -44,4 +45,5 @@ export const rules = {
|
|
|
44
45
|
'remove-useless-colon-from-type-checker': removeUselessColonFromTypeChecker,
|
|
45
46
|
'remove-useless-not-from-type-checker': removeUselessNotFromTypeChecker,
|
|
46
47
|
'reverse-comparison-in-type-checker': reverseComparisonInTypeChecker,
|
|
48
|
+
'remove-useless-path-from-type-checker': removeUselessPathFromTypeChecker,
|
|
47
49
|
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {operator, types} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
isIdentifier,
|
|
5
|
+
isCallExpression,
|
|
6
|
+
} = types;
|
|
7
|
+
|
|
8
|
+
const {setLiteralValue} = operator;
|
|
9
|
+
|
|
10
|
+
export const report = ({path, where}) => {
|
|
11
|
+
const {value} = path.node;
|
|
12
|
+
const withoutNot = removePath(value, where);
|
|
13
|
+
|
|
14
|
+
return `Avoid useless 'path': '${value}' -> '${withoutNot}'`;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const fix = ({path, where}) => {
|
|
18
|
+
const {value} = path.node;
|
|
19
|
+
setLiteralValue(path, removePath(value, where));
|
|
20
|
+
};
|
|
21
|
+
export const traverse = ({push}) => ({
|
|
22
|
+
StringLiteral(path) {
|
|
23
|
+
const call = path.find(isCallExpression);
|
|
24
|
+
|
|
25
|
+
if (!call)
|
|
26
|
+
return;
|
|
27
|
+
|
|
28
|
+
if (!isIdentifier(call.node.callee, {name: 'createTypeChecker'}))
|
|
29
|
+
return;
|
|
30
|
+
|
|
31
|
+
const {value} = path.node;
|
|
32
|
+
const where = createWhere(value);
|
|
33
|
+
|
|
34
|
+
if (where.length)
|
|
35
|
+
push({
|
|
36
|
+
path,
|
|
37
|
+
where,
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
function createWhere(value) {
|
|
43
|
+
const where = [];
|
|
44
|
+
const includes = value.includes.bind(value);
|
|
45
|
+
const endsWith = value.endsWith.bind(value);
|
|
46
|
+
const push = where.push.bind(where);
|
|
47
|
+
|
|
48
|
+
const tools = {
|
|
49
|
+
push,
|
|
50
|
+
includes,
|
|
51
|
+
endsWith,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
if (includes('path.'))
|
|
55
|
+
tracePath(tools);
|
|
56
|
+
|
|
57
|
+
return where;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function tracePath({push, includes}) {
|
|
61
|
+
if (includes('path.'))
|
|
62
|
+
push('path');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function removePath(value, where) {
|
|
66
|
+
if (where.includes('path'))
|
|
67
|
+
value = value.replace('path.', '');
|
|
68
|
+
|
|
69
|
+
return value;
|
|
70
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-printer",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.16.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",
|