@putout/plugin-printer 8.13.0 → 8.15.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,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
+ - ✅ [reverse-comparison-in-type-checker](#reverse-comparison-in-type-checker);
37
38
 
38
39
  ## Config
39
40
 
@@ -59,7 +60,8 @@ npm i @putout/plugin-printer -D
59
60
  "printer/remove-useless-spaces-from-type-checker": "on",
60
61
  "printer/remove-useless-colon-from-type-checker": "on",
61
62
  "printer/remove-useless-not-from-type-checker": "on",
62
- "printer/remove-useless-tuples-from-type-checker": "on"
63
+ "printer/remove-useless-tuples-from-type-checker": "on",
64
+ "printer/reverse-comparison-in-type-checker": "on"
63
65
  }
64
66
  }
65
67
  ```
@@ -483,6 +485,32 @@ export const isNewlineAfterComma = createTypeChecker([
483
485
  ]);
484
486
  ```
485
487
 
488
+ ## reverse-comparison-in-type-checker
489
+
490
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/ae2335d32022de240e918ce077144767/f25c5a11f746fe532f81adede991286cb77d1b11).
491
+
492
+ ### ❌ Example of incorrect code
493
+
494
+ ```js
495
+ const isMoreThenMaxElementLengthInOneLine = createTypeChecker([
496
+ ['-: node.elements.length -> !', '<', 2],
497
+ ['-: node.elements.length -> !', '>', 2],
498
+ ['-: node.elements.length -> !', '>=', 2],
499
+ ['-: node.elements.length -> !', '<=', 2],
500
+ ]);
501
+ ```
502
+
503
+ ### ✅ Example of correct code
504
+
505
+ ```js
506
+ const isMoreThenMaxElementLengthInOneLine = createTypeChecker([
507
+ ['-: node.elements.length', '>=', 2],
508
+ ['-: node.elements.length', '<=', 2],
509
+ ['-: node.elements.length', '<', 2],
510
+ ['-: node.elements.length', '>', 2],
511
+ ]);
512
+ ```
513
+
486
514
  ## remove-legacy-test-declaration
487
515
 
488
516
  ```diff
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as reverseComparisonInTypeChecker from './reverse-comparison-in-type-checker/index.js';
1
2
  import * as removeUselessNotFromTypeChecker from './remove-useless-not-from-type-checker/index.js';
2
3
  import * as removeUselessColonFromTypeChecker from './remove-useless-colon-from-type-checker/index.js';
3
4
  import * as addMissingColonToTypeChecker from './add-missing-colon-to-type-checker/index.js';
@@ -42,4 +43,5 @@ export const rules = {
42
43
  'add-missing-colon-to-type-checker': addMissingColonToTypeChecker,
43
44
  'remove-useless-colon-from-type-checker': removeUselessColonFromTypeChecker,
44
45
  'remove-useless-not-from-type-checker': removeUselessNotFromTypeChecker,
46
+ 'reverse-comparison-in-type-checker': reverseComparisonInTypeChecker,
45
47
  };
@@ -40,11 +40,13 @@ export const traverse = ({push}) => ({
40
40
  },
41
41
  });
42
42
 
43
- const isTwoElementTuple = (a) => {
43
+ const isCountElementsTuple = (count) => (a) => {
44
44
  if (!isArrayExpression(a))
45
45
  return false;
46
46
 
47
- return a.node.elements.length === 2;
47
+ const {length} = a.node.elements;
48
+
49
+ return length === count;
48
50
  };
49
51
 
50
52
  function isConsistent(elements) {
@@ -58,5 +60,8 @@ function isConsistent(elements) {
58
60
  if (elements.length === identifiersCount)
59
61
  return true;
60
62
 
61
- return elements.filter(isTwoElementTuple).length;
63
+ if (elements.filter(isCountElementsTuple(3)).length)
64
+ return true;
65
+
66
+ return elements.filter(isCountElementsTuple(2)).length;
62
67
  }
@@ -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.13.0",
3
+ "version": "8.15.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",