Skip to content

Commit 39d764a

Browse files
committed
feature: @putout/plugin-printer: add-missing-tuple-to-type-checker: add
1 parent f93aa25 commit 39d764a

12 files changed

Lines changed: 206 additions & 6 deletions

packages/plugin-printer/README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ npm i @putout/plugin-printer -D
1515

1616
-[add-args](#add-args);
1717
-[add-missing-spacess-to-type-checker](#add-missing-spacess-to-type-checker);
18+
-[add-missing-tuple-to-type-checker](#add-missing-tuple-to-type-checker);
1819
-[apply-breakline](#apply-breakline);
1920
-[apply-computed-print](#apply-computed-print);
2021
-[apply-create-test-url](#apply-create-test-url);
@@ -34,7 +35,7 @@ npm i @putout/plugin-printer -D
3435
{
3536
"rules": {
3637
"printer/add-args": "on",
37-
"printer/add-missing-spacess-to-type-checker": "on",
38+
"printer/add-missing-tuple-to-type-checker": "on",
3839
"printer/apply-breakline": "on",
3940
"printer/apply-linebreak": "on",
4041
"printer/apply-computed-print": "on",
@@ -124,6 +125,34 @@ export const beforeIf = createTypeChecker([
124125
]);
125126
```
126127

128+
## add-missing-tuple-to-type-checker
129+
130+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9fdfda423df3af9b0f08541710706299/0259bba117997861fe7573bad5477282f4df8554).
131+
132+
### ❌ Example of incorrect code
133+
134+
```js
135+
export const beforeIf = createTypeChecker([
136+
'- : -> !StringLiteral',
137+
'- : -> BlockStatement',
138+
'- : -> WrongType',
139+
['- : ->', isBlockStatement],
140+
['-', isBlockStatement],
141+
]);
142+
```
143+
144+
### ✅ Example of correct code
145+
146+
```js
147+
export const beforeIf = createTypeChecker([
148+
['- : -> !StringLiteral'],
149+
['- : -> BlockStatement'],
150+
['- : -> WrongType'],
151+
['- : ->', isBlockStatement],
152+
['-', isBlockStatement],
153+
]);
154+
```
155+
127156
## apply-computed-print
128157

129158
### ❌ Example of incorrect code
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const beforeIf = createTypeChecker([
2+
['- : -> !StringLiteral'],
3+
['- : -> BlockStatement'],
4+
['- : -> WrongType'],
5+
['- : ->', isBlockStatement],
6+
['-', isBlockStatement],
7+
]);
8+
9+
export const allTuples = createTypeChecker([
10+
['- : ->', isBlockStatement],
11+
['-', isBlockStatement],
12+
]);
13+
14+
export const allStrings = createTypeChecker([
15+
'- : -> BlockStatement',
16+
'- : -> WrongType',
17+
]);
18+
19+
export const allIdentifiers = createTypeChecker([
20+
isExpressionStatement,
21+
]);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const beforeIf = createTypeChecker([
2+
'- : -> !StringLiteral',
3+
'- : -> BlockStatement',
4+
'- : -> WrongType',
5+
['- : ->', isBlockStatement],
6+
['-', isBlockStatement],
7+
]);
8+
9+
export const allTuples = createTypeChecker([
10+
['- : ->', isBlockStatement],
11+
['-', isBlockStatement],
12+
]);
13+
14+
export const allStrings = createTypeChecker([
15+
'- : -> BlockStatement',
16+
'- : -> WrongType',
17+
]);
18+
19+
export const allIdentifiers = createTypeChecker([
20+
isExpressionStatement,
21+
]);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {operator, types} from 'putout';
2+
3+
const {
4+
isStringLiteral,
5+
isArrayExpression,
6+
isIdentifier,
7+
isCallExpression,
8+
arrayExpression,
9+
} = types;
10+
11+
const {replaceWith} = operator;
12+
13+
export const report = (path) => {
14+
return `Add missing tuple around: ${path}`;
15+
};
16+
17+
export const fix = (path) => {
18+
replaceWith(path, arrayExpression([path.node]));
19+
};
20+
21+
export const traverse = ({push}) => ({
22+
ArrayExpression(path) {
23+
const {parentPath} = path;
24+
25+
if (!isCallExpression(parentPath))
26+
return;
27+
28+
if (!isIdentifier(parentPath.node.callee, {name: 'createTypeChecker'}))
29+
return;
30+
31+
const elements = path.get('elements');
32+
33+
if (isConsistent(elements))
34+
return;
35+
36+
for (const element of elements) {
37+
if (!isArrayExpression(element))
38+
push(element);
39+
}
40+
},
41+
});
42+
43+
function isConsistent(elements) {
44+
const arraysCount = elements.filter(isArrayExpression).length;
45+
46+
if (elements.length === arraysCount)
47+
return true;
48+
49+
const stringsCount = elements.filter(isStringLiteral).length;
50+
51+
if (elements.length === stringsCount)
52+
return true;
53+
54+
const identifiersCount = elements.filter(isIdentifier).length;
55+
56+
return elements.length === identifiersCount;
57+
}
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+
['add-missing-tuples-to-type-checker', plugin],
7+
],
8+
});
9+
10+
test('printer: add-missing-tuples-to-type-checker: report', (t) => {
11+
t.report('add-missing-tuples-to-type-checker', `Add missing tuple around: '- : -> !StringLiteral'`);
12+
t.end();
13+
});
14+
15+
test('printer: add-missing-tuples-to-type-checker: transform', (t) => {
16+
t.transform('add-missing-tuples-to-type-checker');
17+
t.end();
18+
});

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 addMissingTuplesToTypeChecker from './add-missing-tuples-to-type-checker/index.js';
12
import * as checkTypePassedToTypeChecker from './check-type-passed-to-type-checker/index.js';
23
import * as removeUselessSpacesFromTypeChecker from './remove-useless-spaces-from-type-checker/index.js';
34
import * as addMissingSpacesToTypeChecker from './add-missing-spaces-to-type-checker/index.js';
@@ -28,4 +29,5 @@ export const rules = {
2829
'add-missing-spaces-to-type-checker': addMissingSpacesToTypeChecker,
2930
'remove-useless-spaces-from-type-checker': removeUselessSpacesFromTypeChecker,
3031
'check-type-passed-to-type-checker': checkTypePassedToTypeChecker,
32+
'add-missing-tuples-to-type-checker': addMissingTuplesToTypeChecker,
3133
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const beforeIf = createTypeChecker([
22
['-: -> !', isInsideArray],
33
['-: parentPath ->', isCoupleLines],
4-
isIdentifierAndIdentifier,
5-
isStringAndIdentifierInsideOneElementArray,
4+
[isIdentifierAndIdentifier],
5+
[isStringAndIdentifierInsideOneElementArray],
66
]);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {types} from '@putout/babel';
2+
3+
const {isExpressionStatement} = types;
4+
const {isBlockStatement} = types;
5+
6+
export const beforeIf = createTypeChecker([
7+
['-: -> !StringLiteral'],
8+
['-: -> BlockStatement'],
9+
['-: -> 🧨 WrongType'],
10+
['-: ->', isBlockStatement],
11+
['-', isBlockStatement],
12+
]);
13+
14+
export const allTuples = createTypeChecker([
15+
['-: ->', isBlockStatement],
16+
['-', isBlockStatement],
17+
]);
18+
19+
export const allStrings = createTypeChecker([
20+
'-: -> BlockStatement',
21+
'-: -> 🧨 WrongType',
22+
]);
23+
24+
export const allIdentifiers = createTypeChecker([
25+
isExpressionStatement,
26+
]);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const beforeIf = createTypeChecker([
2+
'- : -> !StringLiteral',
3+
'- : -> BlockStatement',
4+
'- : -> WrongType',
5+
['- : ->', isBlockStatement],
6+
['-', isBlockStatement],
7+
]);
8+
9+
export const allTuples = createTypeChecker([
10+
['- : ->', isBlockStatement],
11+
['-', isBlockStatement],
12+
]);
13+
14+
export const allStrings = createTypeChecker([
15+
'- : -> BlockStatement',
16+
'- : -> WrongType',
17+
]);
18+
19+
export const allIdentifiers = createTypeChecker([
20+
isExpressionStatement,
21+
]);

packages/plugin-printer/test/fixture/check-type-passed-to-type-checker-fix.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {types} from '@putout/babel';
33
const {isBlockStatement} = types;
44

55
export const beforeIf = createTypeChecker([
6-
'-: -> !StringLiteral',
6+
['-: -> !StringLiteral'],
77
['-: -> BlockStatement'],
88
['-: -> 🧨 WrongType'],
99
['-: ->', isBlockStatement],

0 commit comments

Comments
 (0)