@putout/plugin-printer 8.14.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 CHANGED
@@ -34,6 +34,8 @@ 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);
38
+ - ✅ [reverse-comparison-in-type-checker](#reverse-comparison-in-type-checker);
37
39
 
38
40
  ## Config
39
41
 
@@ -59,7 +61,9 @@ npm i @putout/plugin-printer -D
59
61
  "printer/remove-useless-spaces-from-type-checker": "on",
60
62
  "printer/remove-useless-colon-from-type-checker": "on",
61
63
  "printer/remove-useless-not-from-type-checker": "on",
62
- "printer/remove-useless-tuples-from-type-checker": "on"
64
+ "printer/remove-useless-path-from-type-checker": "on",
65
+ "printer/remove-useless-tuples-from-type-checker": "on",
66
+ "printer/reverse-comparison-in-type-checker": "on"
63
67
  }
64
68
  }
65
69
  ```
@@ -318,6 +322,28 @@ export const beforeIf = createTypeChecker([
318
322
  ]);
319
323
  ```
320
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
+
321
347
  ## remove-useless-tuples-from-type-checker
322
348
 
323
349
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/f53f386b607115e0a96f7e294e34761e/cefd72aba4fcade59bbf8faa59739bac5c2fe057).
@@ -483,6 +509,32 @@ export const isNewlineAfterComma = createTypeChecker([
483
509
  ]);
484
510
  ```
485
511
 
512
+ ## reverse-comparison-in-type-checker
513
+
514
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/ae2335d32022de240e918ce077144767/f25c5a11f746fe532f81adede991286cb77d1b11).
515
+
516
+ ### ❌ Example of incorrect code
517
+
518
+ ```js
519
+ const isMoreThenMaxElementLengthInOneLine = createTypeChecker([
520
+ ['-: node.elements.length -> !', '<', 2],
521
+ ['-: node.elements.length -> !', '>', 2],
522
+ ['-: node.elements.length -> !', '>=', 2],
523
+ ['-: node.elements.length -> !', '<=', 2],
524
+ ]);
525
+ ```
526
+
527
+ ### ✅ Example of correct code
528
+
529
+ ```js
530
+ const isMoreThenMaxElementLengthInOneLine = createTypeChecker([
531
+ ['-: node.elements.length', '>=', 2],
532
+ ['-: node.elements.length', '<=', 2],
533
+ ['-: node.elements.length', '<', 2],
534
+ ['-: node.elements.length', '>', 2],
535
+ ]);
536
+ ```
537
+
486
538
  ## remove-legacy-test-declaration
487
539
 
488
540
  ```diff
package/lib/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import * as removeUselessPathFromTypeChecker from './remove-useless-path-from-type-checker/index.js';
2
+ import * as reverseComparisonInTypeChecker from './reverse-comparison-in-type-checker/index.js';
1
3
  import * as removeUselessNotFromTypeChecker from './remove-useless-not-from-type-checker/index.js';
2
4
  import * as removeUselessColonFromTypeChecker from './remove-useless-colon-from-type-checker/index.js';
3
5
  import * as addMissingColonToTypeChecker from './add-missing-colon-to-type-checker/index.js';
@@ -42,4 +44,6 @@ export const rules = {
42
44
  'add-missing-colon-to-type-checker': addMissingColonToTypeChecker,
43
45
  'remove-useless-colon-from-type-checker': removeUselessColonFromTypeChecker,
44
46
  'remove-useless-not-from-type-checker': removeUselessNotFromTypeChecker,
47
+ 'reverse-comparison-in-type-checker': reverseComparisonInTypeChecker,
48
+ 'remove-useless-path-from-type-checker': removeUselessPathFromTypeChecker,
45
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
+ }
@@ -0,0 +1,32 @@
1
+ import {operator} from 'putout';
2
+
3
+ const CMP = {
4
+ '<': '>=',
5
+ '>': '<=',
6
+ '<=': '>',
7
+ '>=': '<',
8
+ };
9
+
10
+ const OPERATORS = Object.keys(CMP);
11
+
12
+ const {setLiteralValue} = operator;
13
+
14
+ export const report = () => `Reverse comparison`;
15
+
16
+ export const match = () => ({
17
+ '["__a", "__b", __c]': ({__a, __b}) => {
18
+ if (!OPERATORS.includes(__b.value))
19
+ return false;
20
+
21
+ return __a.value.endsWith('!');
22
+ },
23
+ });
24
+
25
+ export const replace = () => ({
26
+ '["__a", "__b", __c]': ({__a, __b}, path) => {
27
+ setLiteralValue(__a, __a.value.slice(0, -5));
28
+ setLiteralValue(__b, CMP[__b.value]);
29
+
30
+ return path;
31
+ },
32
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-printer",
3
- "version": "8.14.0",
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",