Skip to content

Commit e0ac4ef

Browse files
committed
feature: @putout/plugin-printer: remove-useless-tuples-from-type-checker: add
1 parent 5e131ea commit e0ac4ef

11 files changed

Lines changed: 230 additions & 5 deletions

packages/plugin-printer/README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ npm i @putout/plugin-printer -D
1414
## Rules
1515

1616
-[add-args](#add-args);
17-
-[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);
17+
-[add-missing-spaces-to-type-checker](#add-missing-spaces-to-type-checker);
18+
-[add-missing-tuples-to-type-checker](#add-missing-tuples-to-type-checker);
1919
-[apply-breakline](#apply-breakline);
2020
-[apply-computed-print](#apply-computed-print);
2121
-[apply-create-test-url](#apply-create-test-url);
@@ -28,14 +28,15 @@ npm i @putout/plugin-printer -D
2828
-[remove-legacy-test-declaration](#remove-legacy-test-declaration);
2929
-[remove-trailing-spaces-from-type-checker](#remove-trailing-spaces-from-type-checker)
3030
-[remove-useless-spaces-from-type-checker](#remove-useless-spaces-from-type-checker)
31+
-[remove-useless-tuples-from-type-checker](#remove-useless-tuples-from-type-checker)
3132

3233
## Config
3334

3435
```json
3536
{
3637
"rules": {
3738
"printer/add-args": "on",
38-
"printer/add-missing-tuple-to-type-checker": "on",
39+
"printer/add-missing-tuples-to-type-checker": "on",
3940
"printer/apply-breakline": "on",
4041
"printer/apply-linebreak": "on",
4142
"printer/apply-computed-print": "on",
@@ -46,7 +47,8 @@ npm i @putout/plugin-printer -D
4647
"printer/remove-args": "on",
4748
"printer/remove-useless-maybe": "on",
4849
"printer/remove-trailing-spaces-from-type-checker": "on",
49-
"printer/remove-useless-spaces-from-type-checker": "on"
50+
"printer/remove-useless-spaces-from-type-checker": "on",
51+
"printer/remove-useless-tuples-from-type-checker": "on"
5052
}
5153
}
5254
```
@@ -103,7 +105,7 @@ module.exports = {
103105
};
104106
```
105107

106-
## add-missing-spacess-to-type-checker
108+
## add-missing-spaces-to-type-checker
107109

108110
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/2451cd51ebd3dfd20c32b6d22ca176b7/bd593860ff4bb2d66e06cf95bbe103cb1203129d).
109111

@@ -239,6 +241,28 @@ export const beforeIf = createTypeChecker([
239241
]);
240242
```
241243
244+
## remove-useless-tuples-from-type-checker
245+
246+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/f53f386b607115e0a96f7e294e34761e/cefd72aba4fcade59bbf8faa59739bac5c2fe057).
247+
248+
### ❌ Example of incorrect code
249+
250+
```js
251+
export const allStrings = createTypeChecker([
252+
['- : -> BlockStatement'],
253+
['- : -> WrongType'],
254+
]);
255+
```
256+
257+
### ✅ Example of correct code
258+
259+
```js
260+
export const allStrings = createTypeChecker([
261+
'- : -> BlockStatement',
262+
'- : -> WrongType',
263+
]);
264+
```
265+
242266
## remove-trailing-spaces-from-type-checker
243267
244268
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/91d33e39485c31e76b534ad8447ba4db/abca8d21ad6edc1f6fed23edb05fa166677f11d6).

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 removeUselessTuplesFromTypeChecker from './remove-useless-tuples-from-type-checker/index.js';
12
import * as addMissingTuplesToTypeChecker from './add-missing-tuples-to-type-checker/index.js';
23
import * as checkTypePassedToTypeChecker from './check-type-passed-to-type-checker/index.js';
34
import * as removeUselessSpacesFromTypeChecker from './remove-useless-spaces-from-type-checker/index.js';
@@ -30,4 +31,5 @@ export const rules = {
3031
'remove-useless-spaces-from-type-checker': removeUselessSpacesFromTypeChecker,
3132
'check-type-passed-to-type-checker': checkTypePassedToTypeChecker,
3233
'add-missing-tuples-to-type-checker': addMissingTuplesToTypeChecker,
34+
'remove-useless-tuples-from-type-checker': removeUselessTuplesFromTypeChecker,
3335
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const beforeIf = createTypeChecker([
2+
'- : -> !StringLiteral',
3+
'- : -> BlockStatement',
4+
'- : -> WrongType',
5+
]);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const beforeIf = createTypeChecker([
2+
'- : -> !StringLiteral',
3+
'- : -> BlockStatement',
4+
['- : -> WrongType'],
5+
]);
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
]);
22+
23+
export const wrong = wrongFn([
24+
['-', isBlockStatement],
25+
]);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
]);
22+
23+
export const wrong = wrongFn([
24+
['-', isBlockStatement],
25+
]);
26+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {operator, types} from 'putout';
2+
3+
const {
4+
isStringLiteral,
5+
isArrayExpression,
6+
isIdentifier,
7+
isCallExpression,
8+
} = types;
9+
10+
const {replaceWith} = operator;
11+
12+
export const report = (path) => {
13+
return `Remove useless tuple from: ${path}`;
14+
};
15+
16+
export const fix = (path) => {
17+
const [element] = path.node.elements;
18+
replaceWith(path, element);
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+
const isTwoElementTuple = (a) => {
44+
if (!isArrayExpression(a))
45+
return false;
46+
47+
return a.node.elements.length === 2;
48+
};
49+
50+
function isConsistent(elements) {
51+
const stringsCount = elements.filter(isStringLiteral).length;
52+
53+
if (elements.length === stringsCount)
54+
return true;
55+
56+
const identifiersCount = elements.filter(isIdentifier).length;
57+
58+
if (elements.length === identifiersCount)
59+
return true;
60+
61+
return elements.filter(isTwoElementTuple).length;
62+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['remove-useless-tuples-from-type-checker', plugin],
7+
],
8+
});
9+
10+
test('printer: remove-useless-tuples-from-type-checker: report', (t) => {
11+
t.report('remove-useless-tuples-from-type-checker', `Remove useless tuple from: ['- : -> BlockStatement']`);
12+
t.end();
13+
});
14+
15+
test('printer: remove-useless-tuples-from-type-checker: transform', (t) => {
16+
t.transform('remove-useless-tuples-from-type-checker');
17+
t.end();
18+
});
19+
20+
test('printer: remove-useless-tuples-from-type-checker: transform: one-element-tuple', (t) => {
21+
t.transform('one-element-tuple');
22+
t.end();
23+
});
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+
]);

0 commit comments

Comments
 (0)