@putout/plugin-printer 8.21.0 → 8.23.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
@@ -27,6 +27,7 @@ npm i @putout/plugin-printer -D
27
27
  - ✅ [check-if-success-possible-in-type-checker](#check-if-success-possible-in-type-checker);
28
28
  - ✅ [merge-tuple-of-type-checker](#merge-tuple-of-type-checker);
29
29
  - ✅ [declare](#declare);
30
+ - ✅ [declare-nearby](#declare-nearby);
30
31
  - ✅ [remove-args](#remove-args);
31
32
  - ✅ [remove-useless-maybe](#remove-useless-maybe);
32
33
  - ✅ [remove-useless-print](#remove-useless-print);
@@ -58,6 +59,7 @@ npm i @putout/plugin-printer -D
58
59
  "printer/check-type-passed-to-type-checker": "on",
59
60
  "printer/check-if-success-possible-in-type-checker": "on",
60
61
  "printer/declare": "on",
62
+ "printer/declare-nearby": "on",
61
63
  "printer/merge-tuple-of-type-checker": "on",
62
64
  "printer/remove-args": "on",
63
65
  "printer/remove-useless-maybe": "on",
@@ -543,6 +545,34 @@ test('', (t) => {
543
545
  });
544
546
  ```
545
547
 
548
+ ## declare-nearby
549
+
550
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/03d729e40ffd352678cdcc92a1cb7e2d/33a3d522247db4a1dd6d34b035176f26fc020ca8).
551
+
552
+ ### ❌ Example of incorrect code
553
+
554
+ ```js
555
+ export const FunctionDeclaration = {
556
+ print(path, printer, semantics) {
557
+ const {print} = printer;
558
+
559
+ indent();
560
+ },
561
+ };
562
+ ```
563
+
564
+ ### ✅ Example of correct code
565
+
566
+ ```js
567
+ export const FunctionDeclaration = {
568
+ print(path, printer, semantics) {
569
+ const {indent, print} = printer;
570
+
571
+ indent();
572
+ },
573
+ };
574
+ ```
575
+
546
576
  ## merge-tuple-of-type-checker
547
577
 
548
578
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/7351b8359ac3da967e9b4b80d49b51da/74de1543f6e129f6e22f38e2eb597abce6c122a6).
@@ -9,6 +9,7 @@ const names = [
9
9
  'indent',
10
10
  'write',
11
11
  'print',
12
+ 'traverse',
12
13
  'maybe',
13
14
  ];
14
15
 
@@ -0,0 +1,11 @@
1
+ const options = {
2
+ nearby: true,
3
+ };
4
+
5
+ export const declare = () => ({
6
+ indent: ['const {indent} = printer', options],
7
+ print: ['const {print} = printer', options],
8
+ write: ['const {write} = printer', options],
9
+ traverse: ['const {traverse} = printer', options],
10
+ maybe: ['const {maybe} = printer', options],
11
+ });
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as declareNearby from './declare-nearby/index.js';
1
2
  import * as applyPrinterDeclarations from './apply-printer-declarations/index.js';
2
3
  import * as removeUselessPrint from './remove-useless-print/index.js';
3
4
  import * as addMissingArrowToTypeChecker from './add-missing-arrow-to-type-checker/index.js';
@@ -54,4 +55,5 @@ export const rules = {
54
55
  'remove-useless-arrow-from-type-checker': removeUselessArrowFromTypeChecker,
55
56
  'add-missing-arrow-to-type-checker': addMissingArrowToTypeChecker,
56
57
  'remove-useless-print': removeUselessPrint,
58
+ 'declare-nearby': declareNearby,
57
59
  };
@@ -3,20 +3,21 @@ import {operator, types} from 'putout';
3
3
  const {
4
4
  isIdentifier,
5
5
  isCallExpression,
6
+ isArrayExpression,
6
7
  } = types;
7
8
 
8
9
  const {setLiteralValue} = operator;
9
10
 
10
11
  export const report = ({path, where}) => {
11
12
  const {value} = path.node;
12
- const withSpaces = addColon(value, where);
13
+ const withSpaces = removeArrow(value, where);
13
14
 
14
15
  return `Remove useless arrow: '${value}' -> '${withSpaces}'`;
15
16
  };
16
17
 
17
18
  export const fix = ({path, where}) => {
18
19
  const {value} = path.node;
19
- setLiteralValue(path, addColon(value, where));
20
+ setLiteralValue(path, removeArrow(value, where));
20
21
  };
21
22
  export const traverse = ({push}) => ({
22
23
  StringLiteral(path) {
@@ -28,8 +29,7 @@ export const traverse = ({push}) => ({
28
29
  if (!isIdentifier(call.node.callee, {name: 'createTypeChecker'}))
29
30
  return;
30
31
 
31
- const {value} = path.node;
32
- const where = createWhere(value);
32
+ const where = createWhere(path);
33
33
 
34
34
  if (where.length)
35
35
  push({
@@ -39,18 +39,32 @@ export const traverse = ({push}) => ({
39
39
  },
40
40
  });
41
41
 
42
- function createWhere(value) {
42
+ function createWhere(path) {
43
+ const {value} = path.node;
43
44
  const where = [];
44
45
 
45
46
  if (value.includes('-> ->'))
46
47
  where.push('duplicate');
47
48
 
49
+ if (isInsideCompareTuple(path) && value.includes(' -> '))
50
+ where.push('useless');
51
+
48
52
  return where;
49
53
  }
50
54
 
51
- function addColon(value, where) {
55
+ function removeArrow(value, where) {
52
56
  if (where.includes('duplicate'))
53
57
  value = value.replace('-> ->', '->');
54
58
 
59
+ if (where.includes('useless'))
60
+ value = value.replace(' -> ', '');
61
+
55
62
  return value;
56
63
  }
64
+
65
+ function isInsideCompareTuple({parentPath}) {
66
+ if (!isArrayExpression(parentPath))
67
+ return false;
68
+
69
+ return parentPath.node.elements.length === 3;
70
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-printer",
3
- "version": "8.21.0",
3
+ "version": "8.23.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",