@putout/plugin-printer 8.12.1 → 8.14.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
@@ -29,10 +29,11 @@ npm i @putout/plugin-printer -D
29
29
  - ✅ [remove-args](#remove-args);
30
30
  - ✅ [remove-useless-maybe](#remove-useless-maybe);
31
31
  - ✅ [remove-legacy-test-declaration](#remove-legacy-test-declaration);
32
- - ✅ [remove-trailing-spaces-from-type-checker](#remove-trailing-spaces-from-type-checker)
33
- - ✅ [remove-useless-spaces-from-type-checker](#remove-useless-spaces-from-type-checker)
34
- - ✅ [remove-useless-colon-from-type-checker](#remove-useless-colon-from-type-checker)
35
- - ✅ [remove-useless-tuples-from-type-checker](#remove-useless-tuples-from-type-checker)
32
+ - ✅ [remove-trailing-spaces-from-type-checker](#remove-trailing-spaces-from-type-checker);
33
+ - ✅ [remove-useless-spaces-from-type-checker](#remove-useless-spaces-from-type-checker);
34
+ - ✅ [remove-useless-colon-from-type-checker](#remove-useless-colon-from-type-checker);
35
+ - ✅ [remove-useless-tuples-from-type-checker](#remove-useless-tuples-from-type-checker);
36
+ - ✅ [remove-useless-not-from-type-checker](#remove-useless-not-from-type-checker);
36
37
 
37
38
  ## Config
38
39
 
@@ -57,6 +58,7 @@ npm i @putout/plugin-printer -D
57
58
  "printer/remove-trailing-spaces-from-type-checker": "on",
58
59
  "printer/remove-useless-spaces-from-type-checker": "on",
59
60
  "printer/remove-useless-colon-from-type-checker": "on",
61
+ "printer/remove-useless-not-from-type-checker": "on",
60
62
  "printer/remove-useless-tuples-from-type-checker": "on"
61
63
  }
62
64
  }
@@ -294,6 +296,28 @@ export const beforeIf = createTypeChecker([
294
296
  ]);
295
297
  ```
296
298
 
299
+ ## remove-useless-not-from-type-checker
300
+
301
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/051b981021fd1e048b675106f0850211/2022b9797125016d4e5c5e6b5d4e066ab2e103ad).
302
+
303
+ ### ❌ Example of incorrect code
304
+
305
+ ```js
306
+ export const beforeIf = createTypeChecker([
307
+ ['-: -> !+'],
308
+ ['+: -> !-'],
309
+ ]);
310
+ ```
311
+
312
+ ### ✅ Example of correct code
313
+
314
+ ```js
315
+ export const beforeIf = createTypeChecker([
316
+ ['-: -> -'],
317
+ ['+: -> +'],
318
+ ]);
319
+ ```
320
+
297
321
  ## remove-useless-tuples-from-type-checker
298
322
 
299
323
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/f53f386b607115e0a96f7e294e34761e/cefd72aba4fcade59bbf8faa59739bac5c2fe057).
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as removeUselessNotFromTypeChecker from './remove-useless-not-from-type-checker/index.js';
1
2
  import * as removeUselessColonFromTypeChecker from './remove-useless-colon-from-type-checker/index.js';
2
3
  import * as addMissingColonToTypeChecker from './add-missing-colon-to-type-checker/index.js';
3
4
  import * as mergeTuplesOfTypeCheckers from './merge-tuples-of-type-checkers/index.js';
@@ -40,4 +41,5 @@ export const rules = {
40
41
  'merge-tuples-of-type-checkers': mergeTuplesOfTypeCheckers,
41
42
  'add-missing-colon-to-type-checker': addMissingColonToTypeChecker,
42
43
  'remove-useless-colon-from-type-checker': removeUselessColonFromTypeChecker,
44
+ 'remove-useless-not-from-type-checker': removeUselessNotFromTypeChecker,
43
45
  };
@@ -0,0 +1,79 @@
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 = removeNot(value, where);
13
+
14
+ return `Avoid useless '!': '${value}' -> '${withoutNot}'`;
15
+ };
16
+
17
+ export const fix = ({path, where}) => {
18
+ const {value} = path.node;
19
+ setLiteralValue(path, removeNot(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('!+'))
55
+ traceNot(tools);
56
+
57
+ if (includes('!-'))
58
+ traceNot(tools);
59
+
60
+ return where;
61
+ }
62
+
63
+ function traceNot({push, includes}) {
64
+ if (includes('!+'))
65
+ push('before-plus');
66
+
67
+ if (includes('!-'))
68
+ push('before-minus');
69
+ }
70
+
71
+ function removeNot(value, where) {
72
+ if (where.includes('before-plus'))
73
+ value = value.replace('!+', '-');
74
+
75
+ if (where.includes('before-minus'))
76
+ value = value.replace('!-', '+');
77
+
78
+ return value;
79
+ }
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-printer",
3
- "version": "8.12.1",
3
+ "version": "8.14.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",