@putout/plugin-printer 8.7.1 → 8.8.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
@@ -23,6 +23,7 @@ npm i @putout/plugin-printer -D
23
23
  - ✅ [apply-types](#apply-types);
24
24
  - ✅ [check-type-passed-to-type-checker](#check-type-passed-to-type-checker);
25
25
  - ✅ [check-if-success-possible-in-type-checker](#check-if-success-possible-in-type-checker);
26
+ - ✅ [merge-tuple-of-type-checker](#merge-tuple-of-type-checker);
26
27
  - ✅ [declare](#declare);
27
28
  - ✅ [remove-args](#remove-args);
28
29
  - ✅ [remove-useless-maybe](#remove-useless-maybe);
@@ -46,6 +47,7 @@ npm i @putout/plugin-printer -D
46
47
  "printer/check-type-passed-to-type-checker": "on",
47
48
  "printer/check-if-success-possible-in-type-checker": "on",
48
49
  "printer/declare": "on",
50
+ "printer/merge-tuple-of-type-checker": "on",
49
51
  "printer/remove-args": "on",
50
52
  "printer/remove-useless-maybe": "on",
51
53
  "printer/remove-trailing-spaces-from-type-checker": "on",
@@ -384,6 +386,30 @@ test('', (t) => {
384
386
  });
385
387
  ```
386
388
 
389
+ ## merge-tuple-of-type-checker
390
+
391
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/7351b8359ac3da967e9b4b80d49b51da/74de1543f6e129f6e22f38e2eb597abce6c122a6).
392
+
393
+ ### ❌ Example of incorrect code
394
+
395
+ ```js
396
+ export const isNewlineAfterComma = createTypeChecker([
397
+ ['+: -> !', isObjectExpression],
398
+ ['+: ->', isStringLiteral],
399
+ ['+', isStringLiteral],
400
+ ]);
401
+ ```
402
+
403
+ ### ✅ Example of correct code
404
+
405
+ ```js
406
+ export const isNewlineAfterComma = createTypeChecker([
407
+ ['+: -> !ObjectExpression'],
408
+ ['+: -> StringLiteral'],
409
+ ['+: StringLiteral'],
410
+ ]);
411
+ ```
412
+
387
413
  ## remove-legacy-test-declaration
388
414
 
389
415
  ```diff
@@ -1,5 +1,7 @@
1
1
  import {types} from 'putout';
2
+ import {isTypeExists} from '#types';
2
3
 
4
+ const addQuotes = (a) => `'${a}'`;
3
5
  const {
4
6
  isIdentifier,
5
7
  isCallExpression,
@@ -11,12 +13,22 @@ const ARROW_LENGTH = ARROW.length;
11
13
  const ARROW_NOT_LENGTH = ARROW_NOT.length;
12
14
  const NOT = '!';
13
15
 
14
- const TYPES = new Set([
15
- 'CommentBlock',
16
+ const INSTEAD = new Map([
17
+ ['ExportDeclaration', [
18
+ 'ExportAllDeclaration',
19
+ 'ExportDefaultDeclaration',
20
+ 'ExportNamedDeclaration',
21
+ ]],
16
22
  ]);
17
23
 
18
24
  export const report = ({type}) => {
19
- return `Unknown type detected: '${type}'`;
25
+ if (!INSTEAD.has(type))
26
+ return `Unknown type detected: '${type}'`;
27
+
28
+ const instead = INSTEAD.get(type).map(addQuotes)
29
+ .join(', ');
30
+
31
+ return `Unknown type detected: '${type}'. Use ${instead} instead`;
20
32
  };
21
33
 
22
34
  export const fix = () => {};
@@ -34,7 +46,7 @@ export const traverse = ({push}) => ({
34
46
  const {value} = path.node;
35
47
 
36
48
  if (!value.includes(ARROW)) {
37
- if (/^[a-zA-Z].+/.test(value) && !isExists(value))
49
+ if (/^[a-zA-Z].+/.test(value) && !isTypeExists(value))
38
50
  push({
39
51
  path,
40
52
  type: value,
@@ -49,7 +61,7 @@ export const traverse = ({push}) => ({
49
61
 
50
62
  const type = value.slice(typeNotIndex, value.length);
51
63
 
52
- if (!isExists(type))
64
+ if (!isTypeExists(type))
53
65
  push({
54
66
  path,
55
67
  type,
@@ -63,17 +75,10 @@ export const traverse = ({push}) => ({
63
75
 
64
76
  const type = value.slice(typeIndex, value.length);
65
77
 
66
- if (!isExists(type))
78
+ if (!isTypeExists(type))
67
79
  push({
68
80
  path,
69
81
  type,
70
82
  });
71
83
  },
72
84
  });
73
-
74
- function isExists(type) {
75
- if (types[`is${type}`])
76
- return true;
77
-
78
- return TYPES.has(type);
79
- }
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as mergeTuplesOfTypeCheckers from './merge-tuples-of-type-checkers/index.js';
1
2
  import * as checkIfSuccessPossibleInTypeChecker from './check-if-success-possible-in-type-checker/index.js';
2
3
  import * as removeUselessTuplesFromTypeChecker from './remove-useless-tuples-from-type-checker/index.js';
3
4
  import * as addMissingTuplesToTypeChecker from './add-missing-tuples-to-type-checker/index.js';
@@ -34,4 +35,5 @@ export const rules = {
34
35
  'add-missing-tuples-to-type-checker': addMissingTuplesToTypeChecker,
35
36
  'remove-useless-tuples-from-type-checker': removeUselessTuplesFromTypeChecker,
36
37
  'check-if-success-possible-in-type-checker': checkIfSuccessPossibleInTypeChecker,
38
+ 'merge-tuples-of-type-checkers': mergeTuplesOfTypeCheckers,
37
39
  };
@@ -0,0 +1,70 @@
1
+ import {types, operator} from 'putout';
2
+ import {isTypeExists} from '#types';
3
+
4
+ const {
5
+ isArrayExpression,
6
+ isIdentifier,
7
+ isCallExpression,
8
+ } = types;
9
+
10
+ const {setLiteralValue} = operator;
11
+ const cutIS = (a) => a.slice(2);
12
+
13
+ const prepareValue = (a) => {
14
+ if (/^[+-]$/.test(a))
15
+ return `${a}: `;
16
+
17
+ if (a.endsWith('!'))
18
+ return a;
19
+
20
+ return `${a} `;
21
+ };
22
+
23
+ export const report = () => `Merge tuple of type checker`;
24
+
25
+ export const fix = ({path, name}) => {
26
+ const [first] = path.node.elements;
27
+ const {value} = first;
28
+
29
+ setLiteralValue(first, `${prepareValue(value)}${name}`);
30
+ path.node.elements.pop();
31
+ };
32
+
33
+ export const traverse = ({push}) => ({
34
+ ArrayExpression(path) {
35
+ const {parentPath} = path;
36
+
37
+ if (!isCallExpression(parentPath))
38
+ return;
39
+
40
+ const {callee} = parentPath.node;
41
+
42
+ if (!isIdentifier(callee, {name: 'createTypeChecker'}))
43
+ return;
44
+
45
+ const elements = path.get('elements');
46
+
47
+ for (const element of elements) {
48
+ if (!isArrayExpression(element))
49
+ continue;
50
+
51
+ const [, checker] = element.get('elements');
52
+
53
+ if (!isIdentifier(checker))
54
+ continue;
55
+
56
+ const checkerName = checker.node.name;
57
+
58
+ if (!checkerName.startsWith('is'))
59
+ return;
60
+
61
+ const name = cutIS(checkerName);
62
+
63
+ if (isTypeExists(name))
64
+ push({
65
+ path: element,
66
+ name,
67
+ });
68
+ }
69
+ },
70
+ });
package/lib/types.js ADDED
@@ -0,0 +1,19 @@
1
+ import {types} from 'putout';
2
+
3
+ const TYPES_EXISTS = new Set([
4
+ 'CommentBlock',
5
+ ]);
6
+
7
+ const TYPES_NOT_EXISTS = new Set([
8
+ 'ExportDeclaration',
9
+ ]);
10
+
11
+ export function isTypeExists(type) {
12
+ if (TYPES_NOT_EXISTS.has(type))
13
+ return false;
14
+
15
+ if (types[`is${type}`])
16
+ return true;
17
+
18
+ return TYPES_EXISTS.has(type);
19
+ }
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@putout/plugin-printer",
3
- "version": "8.7.1",
3
+ "version": "8.8.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",
7
7
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-printer#readme",
8
8
  "main": "lib/index.js",
9
+ "imports": {
10
+ "#types": "./lib/types.js"
11
+ },
9
12
  "release": false,
10
13
  "tag": false,
11
14
  "changelog": false,