Skip to content

Commit bcd6dc5

Browse files
committed
feature: @putout/plugin-printer: merge-tuples-of-type-checkers
1 parent d15ec30 commit bcd6dc5

15 files changed

+204
-46
lines changed

packages/plugin-printer/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ npm i @putout/plugin-printer -D
2323
-[apply-types](#apply-types);
2424
-[check-type-passed-to-type-checker](#check-type-passed-to-type-checker);
2525
-[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);
2627
-[declare](#declare);
2728
-[remove-args](#remove-args);
2829
-[remove-useless-maybe](#remove-useless-maybe);
@@ -46,6 +47,7 @@ npm i @putout/plugin-printer -D
4647
"printer/check-type-passed-to-type-checker": "on",
4748
"printer/check-if-success-possible-in-type-checker": "on",
4849
"printer/declare": "on",
50+
"printer/merge-tuple-of-type-checker": "on",
4951
"printer/remove-args": "on",
5052
"printer/remove-useless-maybe": "on",
5153
"printer/remove-trailing-spaces-from-type-checker": "on",
@@ -384,6 +386,30 @@ test('', (t) => {
384386
});
385387
```
386388
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+
387413
## remove-legacy-test-declaration
388414
389415
```diff

packages/plugin-printer/lib/check-type-passed-to-type-checker/index.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {types} from 'putout';
2+
import {isTypeExists} from '#types';
23

34
const addQuotes = (a) => `'${a}'`;
45
const {
@@ -12,14 +13,6 @@ const ARROW_LENGTH = ARROW.length;
1213
const ARROW_NOT_LENGTH = ARROW_NOT.length;
1314
const NOT = '!';
1415

15-
const TYPES_EXISTS = new Set([
16-
'CommentBlock',
17-
]);
18-
19-
const TYPES_NOT_EXISTS = new Set([
20-
'ExportDeclaration',
21-
]);
22-
2316
const INSTEAD = new Map([
2417
['ExportDeclaration', [
2518
'ExportAllDeclaration',
@@ -53,7 +46,7 @@ export const traverse = ({push}) => ({
5346
const {value} = path.node;
5447

5548
if (!value.includes(ARROW)) {
56-
if (/^[a-zA-Z].+/.test(value) && !isExists(value))
49+
if (/^[a-zA-Z].+/.test(value) && !isTypeExists(value))
5750
push({
5851
path,
5952
type: value,
@@ -68,7 +61,7 @@ export const traverse = ({push}) => ({
6861

6962
const type = value.slice(typeNotIndex, value.length);
7063

71-
if (!isExists(type))
64+
if (!isTypeExists(type))
7265
push({
7366
path,
7467
type,
@@ -82,20 +75,10 @@ export const traverse = ({push}) => ({
8275

8376
const type = value.slice(typeIndex, value.length);
8477

85-
if (!isExists(type))
78+
if (!isTypeExists(type))
8679
push({
8780
path,
8881
type,
8982
});
9083
},
9184
});
92-
93-
function isExists(type) {
94-
if (TYPES_NOT_EXISTS.has(type))
95-
return false;
96-
97-
if (types[`is${type}`])
98-
return true;
99-
100-
return TYPES_EXISTS.has(type);
101-
}

packages/plugin-printer/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as mergeTuplesOfTypeCheckers from './merge-tuples-of-type-checkers/index.js';
12
import * as checkIfSuccessPossibleInTypeChecker from './check-if-success-possible-in-type-checker/index.js';
23
import * as removeUselessTuplesFromTypeChecker from './remove-useless-tuples-from-type-checker/index.js';
34
import * as addMissingTuplesToTypeChecker from './add-missing-tuples-to-type-checker/index.js';
@@ -34,4 +35,5 @@ export const rules = {
3435
'add-missing-tuples-to-type-checker': addMissingTuplesToTypeChecker,
3536
'remove-useless-tuples-from-type-checker': removeUselessTuplesFromTypeChecker,
3637
'check-if-success-possible-in-type-checker': checkIfSuccessPossibleInTypeChecker,
38+
'merge-tuples-of-type-checkers': mergeTuplesOfTypeCheckers,
3739
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const isNewlineAfterComma = createTypeChecker([
2+
['+: -> !ObjectExpression'],
3+
['+: -> StringLiteral'],
4+
['+: StringLiteral'],
5+
['+', 3],
6+
['+', Boolean],
7+
'+: -> StringLiteral',
8+
]);
9+
10+
export const is = create([
11+
['+', Boolean],
12+
]);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const isNewlineAfterComma = createTypeChecker([
2+
['+: -> !', isObjectExpression],
3+
['+: ->', isStringLiteral],
4+
['+', isStringLiteral],
5+
['+', 3],
6+
['+', Boolean],
7+
'+: -> StringLiteral',
8+
]);
9+
10+
export const is = create([
11+
['+', Boolean],
12+
]);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['merge-tuples-of-type-checkers', plugin],
7+
],
8+
});
9+
10+
test('printer: merge-tuples-of-type-checkers: report', (t) => {
11+
t.report('merge-tuples-of-type-checkers', `Merge tuple of type checker`);
12+
t.end();
13+
});
14+
15+
test('printer: merge-tuples-of-type-checkers: transform', (t) => {
16+
t.transform('merge-tuples-of-type-checkers');
17+
t.end();
18+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

packages/plugin-printer/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"description": "🐊Putout plugin adds support of transformations for @putout/printer",
77
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-printer#readme",
88
"main": "lib/index.js",
9+
"imports": {
10+
"#types": "./lib/types.js"
11+
},
912
"release": false,
1013
"tag": false,
1114
"changelog": false,

packages/plugin-printer/test/fixture/add-missing-tuples-to-type-checker-fix.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import {types} from '@putout/babel';
22

33
const {isExpressionStatement} = types;
4-
const {isBlockStatement} = types;
54

65
export const beforeIf = createTypeChecker([
7-
['-: -> !StringLiteral'],
8-
['-: -> BlockStatement'],
9-
['-: -> WrongType'],
10-
['-: ->', isBlockStatement],
11-
['-', isBlockStatement],
6+
'-: -> !StringLiteral',
7+
'-: -> BlockStatement',
8+
'-: -> WrongType',
9+
'-: -> BlockStatement',
10+
'-: BlockStatement',
1211
]);
1312

1413
export const allTuples = createTypeChecker([
15-
['-: ->', isBlockStatement],
16-
['-', isBlockStatement],
14+
'-: -> BlockStatement',
15+
'-: BlockStatement',
1716
]);
1817

1918
export const allStrings = createTypeChecker([

0 commit comments

Comments
 (0)