Skip to content

Commit c2706b6

Browse files
committed
feature: @putout/plugin-printer: remove-useless-colon-from-type-checker
1 parent 84e4345 commit c2706b6

File tree

9 files changed

+150
-0
lines changed

9 files changed

+150
-0
lines changed

packages/plugin-printer/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ npm i @putout/plugin-printer -D
3131
-[remove-legacy-test-declaration](#remove-legacy-test-declaration);
3232
-[remove-trailing-spaces-from-type-checker](#remove-trailing-spaces-from-type-checker)
3333
-[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)
3435
-[remove-useless-tuples-from-type-checker](#remove-useless-tuples-from-type-checker)
3536

3637
## Config
@@ -55,6 +56,7 @@ npm i @putout/plugin-printer -D
5556
"printer/remove-useless-maybe": "on",
5657
"printer/remove-trailing-spaces-from-type-checker": "on",
5758
"printer/remove-useless-spaces-from-type-checker": "on",
59+
"printer/remove-useless-colon-from-type-checker": "on",
5860
"printer/remove-useless-tuples-from-type-checker": "on"
5961
}
6062
}
@@ -270,6 +272,28 @@ export const beforeIf = createTypeChecker([
270272
]);
271273
```
272274
275+
## remove-useless-colon-from-type-checker
276+
277+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/49a35399bd43d56bfd0221b3a0ace3b0/3ff3bdc8cb5f2a06786e9b616252d1b5f506dd73).
278+
279+
### ❌ Example of incorrect code
280+
281+
```js
282+
export const beforeIf = createTypeChecker([
283+
['+:', isInsideArray],
284+
['-:', isCoupleLines],
285+
]);
286+
```
287+
288+
### ✅ Example of correct code
289+
290+
```js
291+
export const beforeIf = createTypeChecker([
292+
['+', isInsideArray],
293+
['-', isCoupleLines],
294+
]);
295+
```
296+
273297
## remove-useless-tuples-from-type-checker
274298
275299
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/f53f386b607115e0a96f7e294e34761e/cefd72aba4fcade59bbf8faa59739bac5c2fe057).

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 removeUselessColonFromTypeChecker from './remove-useless-colon-from-type-checker/index.js';
12
import * as addMissingColonToTypeChecker from './add-missing-colon-to-type-checker/index.js';
23
import * as mergeTuplesOfTypeCheckers from './merge-tuples-of-type-checkers/index.js';
34
import * as checkIfSuccessPossibleInTypeChecker from './check-if-success-possible-in-type-checker/index.js';
@@ -38,4 +39,5 @@ export const rules = {
3839
'check-if-success-possible-in-type-checker': checkIfSuccessPossibleInTypeChecker,
3940
'merge-tuples-of-type-checkers': mergeTuplesOfTypeCheckers,
4041
'add-missing-colon-to-type-checker': addMissingColonToTypeChecker,
42+
'remove-useless-colon-from-type-checker': removeUselessColonFromTypeChecker,
4143
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const beforeIf = createTypeChecker([
2+
['+', isInsideArray],
3+
['-', isCoupleLines],
4+
]);
5+
6+
export const x = create([
7+
['+ -> !', isInsideArray],
8+
['- parentPath ->', isCoupleLines],
9+
]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const beforeIf = createTypeChecker([
2+
['+:', isInsideArray],
3+
['-:', isCoupleLines],
4+
]);
5+
6+
export const x = create([
7+
['+ -> !', isInsideArray],
8+
['- parentPath ->', isCoupleLines],
9+
]);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 withSpaces = addColon(value, where);
13+
14+
return `Remove useless colon: '${value}' -> '${withSpaces}'`;
15+
};
16+
17+
export const fix = ({path, where}) => {
18+
const {value} = path.node;
19+
setLiteralValue(path, addColon(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+
45+
if (value.length !== 2)
46+
return where;
47+
48+
if (value === '+:')
49+
where.push('after-plus');
50+
51+
if (value === '-:')
52+
where.push('after-minus');
53+
54+
return where;
55+
}
56+
57+
function addColon(value, where) {
58+
if (where.includes('after-plus'))
59+
value = value.replace('+:', '+');
60+
61+
if (where.includes('after-minus'))
62+
value = value.replace('-:', '-');
63+
64+
return value;
65+
}
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+
['remove-useless-colon-from-type-checker', plugin],
7+
],
8+
});
9+
10+
test('printer: remove-useless-colon-from-type-checker: report', (t) => {
11+
t.report('remove-useless-colon-from-type-checker', `Remove useless colon: '+:' -> '+'`);
12+
t.end();
13+
});
14+
15+
test('printer: remove-useless-colon-from-type-checker: transform', (t) => {
16+
t.transform('remove-useless-colon-from-type-checker');
17+
t.end();
18+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const beforeIf = createTypeChecker([
2+
['+', isInsideArray],
3+
['-', isCoupleLines],
4+
]);
5+
6+
export const x = create([
7+
['+ -> !', isInsideArray],
8+
['- parentPath ->', isCoupleLines],
9+
]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const beforeIf = createTypeChecker([
2+
['+:', isInsideArray],
3+
['-:', isCoupleLines],
4+
]);
5+
6+
export const x = create([
7+
['+ -> !', isInsideArray],
8+
['- parentPath ->', isCoupleLines],
9+
]);

packages/plugin-printer/test/printer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,8 @@ test('plugin-printer: transform: add-missing-colon-to-type-checker', (t) => {
101101
t.transform('add-missing-colon-to-type-checker');
102102
t.end();
103103
});
104+
105+
test('plugin-printer: transform: remove-useless-colon-from-type-checker', (t) => {
106+
t.transform('remove-useless-colon-from-type-checker');
107+
t.end();
108+
});

0 commit comments

Comments
 (0)