Skip to content

Commit 5fea189

Browse files
committed
feature: @putout/plugin-printer: remove-useless-not-from-type-checker
1 parent bc142bd commit 5fea189

9 files changed

Lines changed: 163 additions & 4 deletions

File tree

packages/plugin-printer/README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ npm i @putout/plugin-printer -D
2929
-[remove-args](#remove-args);
3030
-[remove-useless-maybe](#remove-useless-maybe);
3131
-[remove-legacy-test-declaration](#remove-legacy-test-declaration);
32-
-[remove-trailing-spaces-from-type-checker](#remove-trailing-spaces-from-type-checker)
33-
-[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)
35-
-[remove-useless-tuples-from-type-checker](#remove-useless-tuples-from-type-checker)
32+
-[remove-trailing-spaces-from-type-checker](#remove-trailing-spaces-from-type-checker);
33+
-[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);
35+
-[remove-useless-tuples-from-type-checker](#remove-useless-tuples-from-type-checker);
36+
-[remove-useless-not-from-type-checker](#remove-useless-not-from-type-checker);
3637

3738
## Config
3839

@@ -57,6 +58,7 @@ npm i @putout/plugin-printer -D
5758
"printer/remove-trailing-spaces-from-type-checker": "on",
5859
"printer/remove-useless-spaces-from-type-checker": "on",
5960
"printer/remove-useless-colon-from-type-checker": "on",
61+
"printer/remove-useless-not-from-type-checker": "on",
6062
"printer/remove-useless-tuples-from-type-checker": "on"
6163
}
6264
}
@@ -294,6 +296,28 @@ export const beforeIf = createTypeChecker([
294296
]);
295297
```
296298
299+
## remove-useless-not-from-type-checker
300+
301+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/051b981021fd1e048b675106f0850211/2022b9797125016d4e5c5e6b5d4e066ab2e103ad).
302+
303+
### ❌ Example of incorrect code
304+
305+
```js
306+
export const beforeIf = createTypeChecker([
307+
['-: -> !+'],
308+
['+: -> !-'],
309+
]);
310+
```
311+
312+
### ✅ Example of correct code
313+
314+
```js
315+
export const beforeIf = createTypeChecker([
316+
['-: -> -'],
317+
['+: -> +'],
318+
]);
319+
```
320+
297321
## remove-useless-tuples-from-type-checker
298322
299323
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 removeUselessNotFromTypeChecker from './remove-useless-not-from-type-checker/index.js';
12
import * as removeUselessColonFromTypeChecker from './remove-useless-colon-from-type-checker/index.js';
23
import * as addMissingColonToTypeChecker from './add-missing-colon-to-type-checker/index.js';
34
import * as mergeTuplesOfTypeCheckers from './merge-tuples-of-type-checkers/index.js';
@@ -40,4 +41,5 @@ export const rules = {
4041
'merge-tuples-of-type-checkers': mergeTuplesOfTypeCheckers,
4142
'add-missing-colon-to-type-checker': addMissingColonToTypeChecker,
4243
'remove-useless-colon-from-type-checker': removeUselessColonFromTypeChecker,
44+
'remove-useless-not-from-type-checker': removeUselessNotFromTypeChecker,
4345
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const beforeIf = createTypeChecker([
2+
['-: -> -'],
3+
['+: -> +'],
4+
]);
5+
6+
export const afterIf = is([
7+
['-: -> !', isInsideArray],
8+
]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const beforeIf = createTypeChecker([
2+
['-: -> !+'],
3+
['+: -> !-'],
4+
]);
5+
6+
7+
export const afterIf = is([
8+
['-: -> !', isInsideArray],
9+
]);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 withoutNot = removeNot(value, where);
13+
14+
return `Avoid useless '!': '${value}' -> '${withoutNot}'`;
15+
};
16+
17+
export const fix = ({path, where}) => {
18+
const {value} = path.node;
19+
setLiteralValue(path, removeNot(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+
const includes = value.includes.bind(value);
45+
const endsWith = value.endsWith.bind(value);
46+
const push = where.push.bind(where);
47+
48+
const tools = {
49+
push,
50+
includes,
51+
endsWith,
52+
};
53+
54+
if (includes('!+'))
55+
traceNot(tools);
56+
57+
if (includes('!-'))
58+
traceNot(tools);
59+
60+
return where;
61+
}
62+
63+
function traceNot({push, includes}) {
64+
if (includes('!+'))
65+
push('before-plus');
66+
67+
if (includes('!-'))
68+
push('before-minus');
69+
}
70+
71+
function removeNot(value, where) {
72+
if (where.includes('before-plus'))
73+
value = value.replace('!+', '-');
74+
75+
if (where.includes('before-minus'))
76+
value = value.replace('!-', '+');
77+
78+
return value;
79+
}
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-not-from-type-checker', plugin],
7+
],
8+
});
9+
10+
test('printer: remove-useless-not-from-type-checker: report', (t) => {
11+
t.report('remove-useless-not-from-type-checker', `Avoid useless '!': '-: -> !+' -> '-: -> -'`);
12+
t.end();
13+
});
14+
15+
test('printer: remove-useless-not-from-type-checker: transform', (t) => {
16+
t.transform('remove-useless-not-from-type-checker');
17+
t.end();
18+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const beforeIf = createTypeChecker(['-: -> -', '+: -> +']);
2+
3+
export const afterIf = is([
4+
['-: -> !', isInsideArray],
5+
]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const beforeIf = createTypeChecker([
2+
['-: -> !+'],
3+
['+: -> !-'],
4+
]);
5+
6+
7+
export const afterIf = is([
8+
['-: -> !', isInsideArray],
9+
]);

packages/plugin-printer/test/printer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,8 @@ test('plugin-printer: transform: remove-useless-colon-from-type-checker', (t) =>
106106
t.transform('remove-useless-colon-from-type-checker');
107107
t.end();
108108
});
109+
110+
test('plugin-printer: transform: remove-useless-not-from-type-checker', (t) => {
111+
t.transform('remove-useless-not-from-type-checker');
112+
t.end();
113+
});

0 commit comments

Comments
 (0)