Skip to content

Commit 742efbf

Browse files
committed
feature: @putout/plugin-printer: remove-useless-spaces-from-type-checker: add
1 parent 1895f72 commit 742efbf

File tree

9 files changed

+130
-1
lines changed

9 files changed

+130
-1
lines changed

packages/plugin-printer/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ npm i @putout/plugin-printer -D
2525
-[remove-useless-maybe](#remove-useless-maybe);
2626
-[remove-legacy-test-declaration](#remove-legacy-test-declaration);
2727
-[remove-trailing-spaces-from-type-checker](#remove-trailing-spaces-from-type-checker)
28+
-[remove-useless-spaces-from-type-checker](#remove-useless-spaces-from-type-checker)
2829

2930
## Config
3031

@@ -41,7 +42,8 @@ npm i @putout/plugin-printer -D
4142
"printer/declare": "on",
4243
"printer/remove-args": "on",
4344
"printer/remove-useless-maybe": "on",
44-
"printer/remove-trailing-spaces-from-type-checker": "on"
45+
"printer/remove-trailing-spaces-from-type-checker": "on",
46+
"printer/remove-useless-spaces-from-type-checker": "on"
4547
}
4648
}
4749
```
@@ -186,6 +188,26 @@ maybe.indent(wasNewline);
186188
print.newline();
187189
```
188190
191+
## remove-useless-spaces-from-type-checker
192+
193+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/2e90aa74898a496b488f9c369801b47a/fbd700d965952e80588ec61054b3ed7aee4d17a2).
194+
195+
### ❌ Example of incorrect code
196+
197+
```js
198+
export const beforeIf = createTypeChecker([
199+
['- : -> !', isInsideArray],
200+
]);
201+
```
202+
203+
### ✅ Example of correct code
204+
205+
```js
206+
export const beforeIf = createTypeChecker([
207+
['-: -> !', isInsideArray],
208+
]);
209+
```
210+
189211
## remove-trailing-spaces-from-type-checker
190212
191213
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 removeUselessSpacesFromTypeChecker from './remove-useless-spaces-from-type-checker/index.js';
12
import * as addMissingSpacesToTypeChecker from './add-missing-spaces-to-type-checker/index.js';
23
import * as removeTrailingSpacesFromTypeChecker from './remove-trailing-spaces-from-type-checker/index.js';
34
import * as removeUselessMaybe from './remove-useless-maybe/index.js';
@@ -24,4 +25,5 @@ export const rules = {
2425
'remove-useless-maybe': removeUselessMaybe,
2526
'remove-trailing-spaces-from-type-checker': removeTrailingSpacesFromTypeChecker,
2627
'add-missing-spaces-to-type-checker': addMissingSpacesToTypeChecker,
28+
'remove-useless-spaces-from-type-checker': removeUselessSpacesFromTypeChecker,
2729
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const beforeIf = createTypeChecker([
2+
['-: -> !', isInsideArray],
3+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const beforeIf = createTypeChecker([
2+
['- : -> !', isInsideArray],
3+
]);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 = removeSpaces(value, where);
13+
14+
return `Remove useless spaces: '${value}' -> '${withSpaces}'`;
15+
};
16+
17+
export const fix = ({path, where}) => {
18+
const {value} = path.node;
19+
setLiteralValue(path, removeSpaces(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+
traceColon(tools);
56+
57+
return where;
58+
}
59+
60+
function traceColon({push, includes}) {
61+
if (includes(' :'))
62+
push('before-colon');
63+
}
64+
65+
function removeSpaces(value, where) {
66+
if (where.includes('before-colon'))
67+
value = value.replace(' :', ':');
68+
69+
return value;
70+
}
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-spaces-from-type-checker', plugin],
7+
],
8+
});
9+
10+
test('printer: remove-useless-spaces-from-type-checker: report', (t) => {
11+
t.report('remove-useless-spaces-from-type-checker', `Remove useless spaces: '- : -> !' -> '-: -> !'`);
12+
t.end();
13+
});
14+
15+
test('printer: remove-useless-spaces-from-type-checker: transform', (t) => {
16+
t.transform('remove-useless-spaces-from-type-checker');
17+
t.end();
18+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const beforeIf = createTypeChecker([
2+
['-: -> !', isInsideArray],
3+
]);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const beforeIf = createTypeChecker([
2+
['- : -> !', isInsideArray],
3+
]);

packages/plugin-printer/test/printer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ test('plugin-printer: transform: add-missing-spaces-to-type-checker', (t) => {
6666
t.transform('add-missing-spaces-to-type-checker');
6767
t.end();
6868
});
69+
70+
test('plugin-printer: transform: remove-useless-spaces-from-type-checker', (t) => {
71+
t.transform('remove-useless-spaces-from-type-checker');
72+
t.end();
73+
});

0 commit comments

Comments
 (0)