@putout/plugin-printer 8.3.2 → 8.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -20,11 +20,13 @@ npm i @putout/plugin-printer -D
20
20
  - ✅ [apply-create-test-url](#apply-create-test-url);
21
21
  - ✅ [apply-linebreak](#apply-linebreak);
22
22
  - ✅ [apply-types](#apply-types);
23
+ - ✅ [check-type-passed-to-type-checker](#check-type-passed-to-type-checker);
23
24
  - ✅ [declare](#declare);
24
25
  - ✅ [remove-args](#remove-args);
25
26
  - ✅ [remove-useless-maybe](#remove-useless-maybe);
26
27
  - ✅ [remove-legacy-test-declaration](#remove-legacy-test-declaration);
27
28
  - ✅ [remove-trailing-spaces-from-type-checker](#remove-trailing-spaces-from-type-checker)
29
+ - ✅ [remove-useless-spaces-from-type-checker](#remove-useless-spaces-from-type-checker)
28
30
 
29
31
  ## Config
30
32
 
@@ -38,10 +40,12 @@ npm i @putout/plugin-printer -D
38
40
  "printer/apply-computed-print": "on",
39
41
  "printer/apply-create-test-url": "on",
40
42
  "printer/apply-types": "on",
43
+ "printer/check-type-passed-to-type-checker": "on",
41
44
  "printer/declare": "on",
42
45
  "printer/remove-args": "on",
43
46
  "printer/remove-useless-maybe": "on",
44
- "printer/remove-trailing-spaces-from-type-checker": "on"
47
+ "printer/remove-trailing-spaces-from-type-checker": "on",
48
+ "printer/remove-useless-spaces-from-type-checker": "on"
45
49
  }
46
50
  }
47
51
  ```
@@ -186,6 +190,26 @@ maybe.indent(wasNewline);
186
190
  print.newline();
187
191
  ```
188
192
 
193
+ ## remove-useless-spaces-from-type-checker
194
+
195
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/2e90aa74898a496b488f9c369801b47a/fbd700d965952e80588ec61054b3ed7aee4d17a2).
196
+
197
+ ### ❌ Example of incorrect code
198
+
199
+ ```js
200
+ export const beforeIf = createTypeChecker([
201
+ ['- : -> !', isInsideArray],
202
+ ]);
203
+ ```
204
+
205
+ ### ✅ Example of correct code
206
+
207
+ ```js
208
+ export const beforeIf = createTypeChecker([
209
+ ['-: -> !', isInsideArray],
210
+ ]);
211
+ ```
212
+
189
213
  ## remove-trailing-spaces-from-type-checker
190
214
 
191
215
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/91d33e39485c31e76b534ad8447ba4db/abca8d21ad6edc1f6fed23edb05fa166677f11d6).
@@ -206,6 +230,26 @@ export const beforeIf = createTypeChecker([
206
230
  ]);
207
231
  ```
208
232
 
233
+ ## check-type-passed-to-type-checker
234
+
235
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9b35f5f2f23bece832f0f87c929d30e2/b08df8ef8ce6c2a03bceebe6119846c45a78a119).
236
+
237
+ ### ❌ Example of incorrect code
238
+
239
+ ```js
240
+ export const beforeIf = createTypeChecker([
241
+ ['- : -> WrongType'],
242
+ ]);
243
+ ```
244
+
245
+ ### ✅ Example of correct code
246
+
247
+ ```js
248
+ export const beforeIf = createTypeChecker([
249
+ ['- : -> 🧨 WrongType'],
250
+ ]);
251
+ ```
252
+
209
253
  ## declare
210
254
 
211
255
  ### ❌ Example of incorrect code
@@ -57,7 +57,7 @@ function createWhere(value) {
57
57
  if (includes(':'))
58
58
  traceColon(tools);
59
59
 
60
- return where.filter(Boolean);
60
+ return where;
61
61
  }
62
62
 
63
63
  function traceColon({push, includes, endsWith}) {
@@ -0,0 +1,71 @@
1
+ import {types, operator} from 'putout';
2
+
3
+ const {
4
+ isIdentifier,
5
+ isCallExpression,
6
+ } = types;
7
+
8
+ const {setLiteralValue} = operator;
9
+ const ARROW = ' -> ';
10
+ const ARROW_NOT = ' -> !';
11
+ const ARROW_LENGTH = ARROW.length;
12
+ const ARROW_NOT_LENGTH = ARROW_NOT.length;
13
+ const MARK = '🧨';
14
+
15
+ export const report = ({type}) => {
16
+ return `Unknown type detected: '${type}'`;
17
+ };
18
+
19
+ export const fix = ({path, type}) => {
20
+ const {value} = path.node;
21
+ setLiteralValue(path, value.replace(type, `${MARK} ${type}`));
22
+ };
23
+
24
+ export const traverse = ({push}) => ({
25
+ StringLiteral(path) {
26
+ const call = path.find(isCallExpression);
27
+
28
+ if (!call)
29
+ return;
30
+
31
+ if (!isIdentifier(call.node.callee, {name: 'createTypeChecker'}))
32
+ return;
33
+
34
+ const {value} = path.node;
35
+
36
+ if (value.includes(MARK))
37
+ return;
38
+
39
+ if (value.endsWith('->'))
40
+ return;
41
+
42
+ if (value.includes('!')) {
43
+ const arrowNotIndex = value.indexOf(ARROW_NOT);
44
+ const typeNotIndex = arrowNotIndex + ARROW_NOT_LENGTH;
45
+
46
+ const type = value.slice(typeNotIndex, value.length);
47
+
48
+ if (!types[`is${type}`])
49
+ push({
50
+ path,
51
+ type,
52
+ });
53
+
54
+ return;
55
+ }
56
+
57
+ const arrowIndex = value.indexOf(ARROW);
58
+ const typeIndex = arrowIndex + ARROW_LENGTH;
59
+
60
+ const type = value.slice(typeIndex, value.length);
61
+
62
+ if (!type)
63
+ return;
64
+
65
+ if (!types[`is${type}`])
66
+ push({
67
+ path,
68
+ type,
69
+ });
70
+ },
71
+ });
package/lib/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import * as checkTypePassedToTypeChecker from './check-type-passed-to-type-checker/index.js';
2
+ import * as removeUselessSpacesFromTypeChecker from './remove-useless-spaces-from-type-checker/index.js';
1
3
  import * as addMissingSpacesToTypeChecker from './add-missing-spaces-to-type-checker/index.js';
2
4
  import * as removeTrailingSpacesFromTypeChecker from './remove-trailing-spaces-from-type-checker/index.js';
3
5
  import * as removeUselessMaybe from './remove-useless-maybe/index.js';
@@ -24,4 +26,6 @@ export const rules = {
24
26
  'remove-useless-maybe': removeUselessMaybe,
25
27
  'remove-trailing-spaces-from-type-checker': removeTrailingSpacesFromTypeChecker,
26
28
  'add-missing-spaces-to-type-checker': addMissingSpacesToTypeChecker,
29
+ 'remove-useless-spaces-from-type-checker': removeUselessSpacesFromTypeChecker,
30
+ 'check-type-passed-to-type-checker': checkTypePassedToTypeChecker,
27
31
  };
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-printer",
3
- "version": "8.3.2",
3
+ "version": "8.5.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds support of transformations for @putout/printer",