Skip to content

Commit bcaf4cc

Browse files
committed
feature: @putout/plugin-printer: add-missing-colon-to-type-checker: add
1 parent 077f8e2 commit bcaf4cc

13 files changed

+156
-8
lines changed

packages/plugin-printer/README.md

Lines changed: 25 additions & 0 deletions
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-spaces-to-type-checker](#add-missing-spaces-to-type-checker);
18+
-[add-missing-colon-to-type-checker](#add-missing-spaces-to-type-checker);
1819
-[add-missing-tuples-to-type-checker](#add-missing-tuples-to-type-checker);
1920
-[apply-breakline](#apply-breakline);
2021
-[apply-computed-print](#apply-computed-print);
@@ -39,6 +40,8 @@ npm i @putout/plugin-printer -D
3940
"rules": {
4041
"printer/add-args": "on",
4142
"printer/add-missing-tuples-to-type-checker": "on",
43+
"printer/add-missing-colon-to-type-checker": "on",
44+
"printer/add-missing-spaces-to-type-checker": "on",
4245
"printer/apply-breakline": "on",
4346
"printer/apply-linebreak": "on",
4447
"printer/apply-computed-print": "on",
@@ -131,6 +134,28 @@ export const beforeIf = createTypeChecker([
131134
]);
132135
```
133136

137+
## add-missing-colon-to-type-checker
138+
139+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/97d38d9bee0586deb8c627c19e399a27/889646df2dcfcfe86f7eb0efb6e3ed5791701f58).
140+
141+
### ❌ Example of incorrect code
142+
143+
```js
144+
export const beforeIf = createTypeChecker([
145+
['+ -> !', isInsideArray],
146+
['- parentPath ->', isCoupleLines],
147+
]);
148+
```
149+
150+
### ✅ Example of correct code
151+
152+
```js
153+
export const beforeIf = createTypeChecker([
154+
['+: -> !', isInsideArray],
155+
['-: parentPath ->', isCoupleLines],
156+
]);
157+
```
158+
134159
## add-missing-tuple-to-type-checker
135160

136161
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9fdfda423df3af9b0f08541710706299/0259bba117997861fe7573bad5477282f4df8554).
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+
['-: parentPath ->', 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+
['- parentPath ->', isCoupleLines],
4+
]);
5+
6+
export const x = create([
7+
['+ -> !', isInsideArray],
8+
['- parentPath ->', isCoupleLines],
9+
]);
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+
isIdentifier,
5+
isCallExpression,
6+
} = types;
7+
8+
const {setLiteralValue} = operator;
9+
10+
export const report = ({path, where}) => {
11+
const {value} = path.node;
12+
const withColon = addColon(value, where);
13+
14+
return `Add missing colon: '${value}' -> '${withColon}'`;
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.startsWith('+') && !value.includes(':'))
46+
where.push('after-plus');
47+
48+
if (value.startsWith('-') && !value.includes('-:'))
49+
where.push('after-minus');
50+
51+
return where;
52+
}
53+
54+
function addColon(value, where) {
55+
if (where.includes('after-plus'))
56+
value = value.replace('+', '+:');
57+
58+
if (where.includes('after-minus'))
59+
value = value.replace('-', '-:');
60+
61+
return value;
62+
}
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-colon-to-type-checker', plugin],
7+
],
8+
});
9+
10+
test('printer: add-missing-colon-to-type-checker: report', (t) => {
11+
t.report('add-missing-colon-to-type-checker', `Add missing colon: '+ -> !' -> '+: -> !'`);
12+
t.end();
13+
});
14+
15+
test('printer: add-missing-colon-to-type-checker: transform', (t) => {
16+
t.transform('add-missing-colon-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 addMissingColonToTypeChecker from './add-missing-colon-to-type-checker/index.js';
12
import * as mergeTuplesOfTypeCheckers from './merge-tuples-of-type-checkers/index.js';
23
import * as checkIfSuccessPossibleInTypeChecker from './check-if-success-possible-in-type-checker/index.js';
34
import * as removeUselessTuplesFromTypeChecker from './remove-useless-tuples-from-type-checker/index.js';
@@ -36,4 +37,5 @@ export const rules = {
3637
'remove-useless-tuples-from-type-checker': removeUselessTuplesFromTypeChecker,
3738
'check-if-success-possible-in-type-checker': checkIfSuccessPossibleInTypeChecker,
3839
'merge-tuples-of-type-checkers': mergeTuplesOfTypeCheckers,
40+
'add-missing-colon-to-type-checker': addMissingColonToTypeChecker,
3941
};
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+
['-: parentPath ->', 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+
['- parentPath ->', isCoupleLines],
4+
]);
5+
6+
export const x = create([
7+
['+ -> !', isInsideArray],
8+
['- parentPath ->', isCoupleLines],
9+
]);
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
]);

packages/plugin-printer/test/fixture/check-if-success-possible-in-type-checker-fix.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const isSimple = createTypeChecker([
88
]);
99

1010
const isSimpleAfterObject = createTypeChecker([
11-
['-', isSimple],
12-
['-', callWithNext(isObjectExpression)],
13-
['-', callWithPrev(isObjectExpression)],
11+
['-:', isSimple],
12+
['-:', callWithNext(isObjectExpression)],
13+
['-:', callWithPrev(isObjectExpression)],
1414
]);

0 commit comments

Comments
 (0)