@haneullabs/prettier-plugin-move 0.3.3

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.
Files changed (149) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/CONTRIBUTING.md +31 -0
  3. package/README.md +96 -0
  4. package/bin/prettier-move.js +29 -0
  5. package/out/cst/annotation.js +64 -0
  6. package/out/cst/annotation.js.map +1 -0
  7. package/out/cst/common.js +376 -0
  8. package/out/cst/common.js.map +1 -0
  9. package/out/cst/constant.js +92 -0
  10. package/out/cst/constant.js.map +1 -0
  11. package/out/cst/enum_definition.js +69 -0
  12. package/out/cst/enum_definition.js.map +1 -0
  13. package/out/cst/expression/abort_expression.js +32 -0
  14. package/out/cst/expression/abort_expression.js.map +1 -0
  15. package/out/cst/expression/annotation_expression.js +35 -0
  16. package/out/cst/expression/annotation_expression.js.map +1 -0
  17. package/out/cst/expression/assign_expression.js +51 -0
  18. package/out/cst/expression/assign_expression.js.map +1 -0
  19. package/out/cst/expression/binary_expression.js +70 -0
  20. package/out/cst/expression/binary_expression.js.map +1 -0
  21. package/out/cst/expression/block.js +58 -0
  22. package/out/cst/expression/block.js.map +1 -0
  23. package/out/cst/expression/block_item.js +25 -0
  24. package/out/cst/expression/block_item.js.map +1 -0
  25. package/out/cst/expression/borrow_expression.js +26 -0
  26. package/out/cst/expression/borrow_expression.js.map +1 -0
  27. package/out/cst/expression/break_expression.js +27 -0
  28. package/out/cst/expression/break_expression.js.map +1 -0
  29. package/out/cst/expression/call_expression.js +25 -0
  30. package/out/cst/expression/call_expression.js.map +1 -0
  31. package/out/cst/expression/cast_expression.js +31 -0
  32. package/out/cst/expression/cast_expression.js.map +1 -0
  33. package/out/cst/expression/continue_expression.js +26 -0
  34. package/out/cst/expression/continue_expression.js.map +1 -0
  35. package/out/cst/expression/dereference_expression.js +27 -0
  36. package/out/cst/expression/dereference_expression.js.map +1 -0
  37. package/out/cst/expression/dot_expression.js +66 -0
  38. package/out/cst/expression/dot_expression.js.map +1 -0
  39. package/out/cst/expression/expression_list.js +26 -0
  40. package/out/cst/expression/expression_list.js.map +1 -0
  41. package/out/cst/expression/identified_expression.js +28 -0
  42. package/out/cst/expression/identified_expression.js.map +1 -0
  43. package/out/cst/expression/if_expression.js +133 -0
  44. package/out/cst/expression/if_expression.js.map +1 -0
  45. package/out/cst/expression/index.js +74 -0
  46. package/out/cst/expression/index.js.map +1 -0
  47. package/out/cst/expression/index_expression.js +28 -0
  48. package/out/cst/expression/index_expression.js.map +1 -0
  49. package/out/cst/expression/lambda_expression.js +72 -0
  50. package/out/cst/expression/lambda_expression.js.map +1 -0
  51. package/out/cst/expression/let_statement.js +59 -0
  52. package/out/cst/expression/let_statement.js.map +1 -0
  53. package/out/cst/expression/loop_expression.js +27 -0
  54. package/out/cst/expression/loop_expression.js.map +1 -0
  55. package/out/cst/expression/macro_call_expression.js +66 -0
  56. package/out/cst/expression/macro_call_expression.js.map +1 -0
  57. package/out/cst/expression/match_expression.js +86 -0
  58. package/out/cst/expression/match_expression.js.map +1 -0
  59. package/out/cst/expression/move_or_copy_expression.js +27 -0
  60. package/out/cst/expression/move_or_copy_expression.js.map +1 -0
  61. package/out/cst/expression/name_expression.js +26 -0
  62. package/out/cst/expression/name_expression.js.map +1 -0
  63. package/out/cst/expression/pack_expression.js +27 -0
  64. package/out/cst/expression/pack_expression.js.map +1 -0
  65. package/out/cst/expression/return_expression.js +44 -0
  66. package/out/cst/expression/return_expression.js.map +1 -0
  67. package/out/cst/expression/unary_expression.js +26 -0
  68. package/out/cst/expression/unary_expression.js.map +1 -0
  69. package/out/cst/expression/unit_expression.js +17 -0
  70. package/out/cst/expression/unit_expression.js.map +1 -0
  71. package/out/cst/expression/vector_expression.js +80 -0
  72. package/out/cst/expression/vector_expression.js.map +1 -0
  73. package/out/cst/expression/while_expression.js +42 -0
  74. package/out/cst/expression/while_expression.js.map +1 -0
  75. package/out/cst/formatting.js +100 -0
  76. package/out/cst/formatting.js.map +1 -0
  77. package/out/cst/function_definition.js +248 -0
  78. package/out/cst/function_definition.js.map +1 -0
  79. package/out/cst/literal.js +68 -0
  80. package/out/cst/literal.js.map +1 -0
  81. package/out/cst/module.js +158 -0
  82. package/out/cst/module.js.map +1 -0
  83. package/out/cst/source_file.js +38 -0
  84. package/out/cst/source_file.js.map +1 -0
  85. package/out/cst/struct_definition.js +209 -0
  86. package/out/cst/struct_definition.js.map +1 -0
  87. package/out/cst/use_declaration.js +212 -0
  88. package/out/cst/use_declaration.js.map +1 -0
  89. package/out/imports-grouping.js +259 -0
  90. package/out/imports-grouping.js.map +1 -0
  91. package/out/index.js +97 -0
  92. package/out/index.js.map +1 -0
  93. package/out/printer.js +69 -0
  94. package/out/printer.js.map +1 -0
  95. package/out/tree.js +371 -0
  96. package/out/tree.js.map +1 -0
  97. package/out/utilities.js +251 -0
  98. package/out/utilities.js.map +1 -0
  99. package/package.json +34 -0
  100. package/prettier.config.js +12 -0
  101. package/src/cst/annotation.ts +71 -0
  102. package/src/cst/common.ts +430 -0
  103. package/src/cst/constant.ts +110 -0
  104. package/src/cst/enum_definition.ts +73 -0
  105. package/src/cst/expression/abort_expression.ts +35 -0
  106. package/src/cst/expression/annotation_expression.ts +38 -0
  107. package/src/cst/expression/assign_expression.ts +66 -0
  108. package/src/cst/expression/binary_expression.ts +75 -0
  109. package/src/cst/expression/block.ts +72 -0
  110. package/src/cst/expression/block_item.ts +29 -0
  111. package/src/cst/expression/borrow_expression.ts +28 -0
  112. package/src/cst/expression/break_expression.ts +33 -0
  113. package/src/cst/expression/call_expression.ts +28 -0
  114. package/src/cst/expression/cast_expression.ts +35 -0
  115. package/src/cst/expression/continue_expression.ts +29 -0
  116. package/src/cst/expression/dereference_expression.ts +33 -0
  117. package/src/cst/expression/dot_expression.ts +89 -0
  118. package/src/cst/expression/expression_list.ts +28 -0
  119. package/src/cst/expression/identified_expression.ts +30 -0
  120. package/src/cst/expression/if_expression.ts +177 -0
  121. package/src/cst/expression/index.ts +85 -0
  122. package/src/cst/expression/index_expression.ts +37 -0
  123. package/src/cst/expression/lambda_expression.ts +84 -0
  124. package/src/cst/expression/let_statement.ts +73 -0
  125. package/src/cst/expression/loop_expression.ts +29 -0
  126. package/src/cst/expression/macro_call_expression.ts +79 -0
  127. package/src/cst/expression/match_expression.ts +102 -0
  128. package/src/cst/expression/move_or_copy_expression.ts +29 -0
  129. package/src/cst/expression/name_expression.ts +28 -0
  130. package/src/cst/expression/pack_expression.ts +29 -0
  131. package/src/cst/expression/return_expression.ts +50 -0
  132. package/src/cst/expression/unary_expression.ts +28 -0
  133. package/src/cst/expression/unit_expression.ts +18 -0
  134. package/src/cst/expression/vector_expression.ts +97 -0
  135. package/src/cst/expression/while_expression.ts +45 -0
  136. package/src/cst/formatting.ts +100 -0
  137. package/src/cst/function_definition.ts +300 -0
  138. package/src/cst/literal.ts +69 -0
  139. package/src/cst/module.ts +191 -0
  140. package/src/cst/source_file.ts +38 -0
  141. package/src/cst/struct_definition.ts +267 -0
  142. package/src/cst/use_declaration.ts +238 -0
  143. package/src/imports-grouping.ts +300 -0
  144. package/src/index.ts +119 -0
  145. package/src/printer.ts +93 -0
  146. package/src/tree.ts +438 -0
  147. package/src/utilities.ts +387 -0
  148. package/tree-sitter-move.wasm +0 -0
  149. package/tsconfig.json +26 -0
@@ -0,0 +1,102 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Node } from '../..';
5
+ import { MoveOptions, printFn, treeFn } from '../../printer';
6
+ import { AstPath, Doc } from 'prettier';
7
+ import { list } from '../../utilities';
8
+ import { builders } from 'prettier/doc';
9
+ const { join, indent, group, softline, line } = builders;
10
+
11
+ /** The type of the node implemented in this file */
12
+ export const NODE_TYPE = 'match_expression';
13
+
14
+ export default function (path: AstPath<Node>): treeFn | null {
15
+ if (path.node.type === NODE_TYPE) {
16
+ return printMatchExpression;
17
+ } else if (path.node.type === 'match_arm') {
18
+ return printMatchArm;
19
+ } else if (path.node.type === 'match_condition') {
20
+ return printMatchCondition;
21
+ }
22
+
23
+ return null;
24
+ }
25
+
26
+ /**
27
+ * Print `match_expression` node.
28
+ * Inside:
29
+ * - `match`
30
+ * - `(`
31
+ * - `_expression`
32
+ * - `)`
33
+ * - `_match_body`
34
+ */
35
+ function printMatchExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
36
+ const condNode = path.node.nonFormattingChildren[0]!;
37
+ const parts: Doc[] = ['match '];
38
+
39
+ if (condNode.isBreakableExpression) {
40
+ parts.push('(', path.call(print, 'nonFormattingChildren', 0), ')');
41
+ } else {
42
+ parts.push(
43
+ group([
44
+ '(',
45
+ indent(softline),
46
+ indent(path.call(print, 'nonFormattingChildren', 0)),
47
+ softline,
48
+ ')',
49
+ ]),
50
+ );
51
+ }
52
+
53
+ parts.push(
54
+ ' ',
55
+ list({
56
+ path,
57
+ print,
58
+ options,
59
+ open: '{',
60
+ close: '}',
61
+ skipChildren: 1,
62
+ shouldBreak: true,
63
+ }),
64
+ );
65
+
66
+ return parts;
67
+ }
68
+
69
+ /**
70
+ * Print `match_arm` node.
71
+ */
72
+ function printMatchArm(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
73
+ const children = path.map(print, 'nonFormattingChildren');
74
+
75
+ if (children.length < 2) {
76
+ throw new Error('`match_arm` node should have at least 2 children');
77
+ }
78
+
79
+ if (children.length == 2) {
80
+ return group(join(' => ', children));
81
+ }
82
+
83
+ if (children.length == 3) {
84
+ return [children[0]!, ' ', children[1]!, group([' =>', indent(line), children[2]!])];
85
+ }
86
+
87
+ throw new Error('`match_arm` node should have at most 3 children');
88
+ }
89
+
90
+ /**
91
+ * Prints `match_condition` node in `match_arm`.
92
+ * Example: `Enum if (x == 1) => 1,`, `if (...)` here is a `match_condition` node.
93
+ */
94
+ function printMatchCondition(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
95
+ const children = path.node.nonFormattingChildren;
96
+
97
+ if (children.length !== 1) {
98
+ throw new Error('`match_condition` expects 1 child');
99
+ }
100
+
101
+ return ['if (', path.call(print, 'nonFormattingChildren', 0), ')'];
102
+ }
@@ -0,0 +1,29 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Node } from '../..';
5
+ import { MoveOptions, printFn, treeFn } from '../../printer';
6
+ import { AstPath, Doc, doc } from 'prettier';
7
+ const {} = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'move_or_copy_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printMoveOrCopyExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `move_or_copy_expression` node.
22
+ */
23
+ function printMoveOrCopyExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
24
+ const ref = path.node.child(0)!.text == 'move' ? ['move', ' '] : ['copy', ' '];
25
+ return [
26
+ ...ref,
27
+ path.call(print, 'nonFormattingChildren', 0), // expression
28
+ ];
29
+ }
@@ -0,0 +1,28 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Node } from '../..';
5
+ import { MoveOptions, printFn, treeFn } from '../../printer';
6
+ import { AstPath, Doc, doc } from 'prettier';
7
+ const {} = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'name_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printNameExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `name_expression` node.
22
+ * Inside:
23
+ * - `module_access`
24
+ * - `type_arguments`
25
+ */
26
+ function printNameExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
27
+ return path.map(print, 'nonFormattingChildren');
28
+ }
@@ -0,0 +1,29 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Node } from '../..';
5
+ import { MoveOptions, printFn, treeFn } from '../../printer';
6
+ import { AstPath, Doc, doc } from 'prettier';
7
+ const {} = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'pack_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printPackExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `pack_expression` node.
22
+ * Inside:
23
+ * - `module_access`
24
+ * - `type_arguments` (optional)
25
+ * - `field_initialize_list`
26
+ */
27
+ function printPackExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
28
+ return path.map(print, 'nonFormattingChildren');
29
+ }
@@ -0,0 +1,50 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Node } from '../..';
5
+ import { MoveOptions, printFn, treeFn } from '../../printer';
6
+ import { AstPath, Doc, doc } from 'prettier';
7
+ const { join, indent } = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'return_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printReturnExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `return_expression` node.
22
+ */
23
+ function printReturnExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
24
+ const nodes = path.node.nonFormattingChildren;
25
+
26
+ if (nodes.length === 0) {
27
+ return 'return';
28
+ }
29
+
30
+ // either label or expression
31
+ if (nodes.length === 1) {
32
+ const expression = nodes[0]!;
33
+ const printed = path.call(print, 'nonFormattingChildren', 0);
34
+ return ['return ', expression.isBreakableExpression ? printed : indent(printed)];
35
+ }
36
+
37
+ // labeled expression
38
+ if (nodes.length === 2) {
39
+ const expression = nodes[1]!;
40
+ const printedLabel = path.call(print, 'nonFormattingChildren', 0);
41
+ const printedExpression = path.call(print, 'nonFormattingChildren', 1);
42
+ return join(' ', [
43
+ 'return',
44
+ printedLabel,
45
+ expression.isBreakableExpression ? printedExpression : indent(printedExpression),
46
+ ]);
47
+ }
48
+
49
+ throw new Error('Invalid return expression');
50
+ }
@@ -0,0 +1,28 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Node } from '../..';
5
+ import { MoveOptions, printFn, treeFn } from '../../printer';
6
+ import { AstPath, Doc, doc } from 'prettier';
7
+ const {} = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'unary_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printUnaryExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `unary_expression` node.
22
+ */
23
+ function printUnaryExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
24
+ return [
25
+ path.call(print, 'nonFormattingChildren', 0),
26
+ path.call(print, 'nonFormattingChildren', 1),
27
+ ];
28
+ }
@@ -0,0 +1,18 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Node } from '../..';
5
+ import { treeFn } from '../../printer';
6
+ import { AstPath, Doc, doc } from 'prettier';
7
+ const {} = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'unit_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return () => '()';
15
+ }
16
+
17
+ return null;
18
+ }
@@ -0,0 +1,97 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Node } from '../..';
5
+ import { MoveOptions, printFn, treeFn } from '../../printer';
6
+ import { AstPath, Doc, doc } from 'prettier';
7
+ import { list, printTrailingComment } from '../../utilities';
8
+ import { printBreakableBlock } from './block';
9
+ const { group, lineSuffix } = doc.builders;
10
+
11
+ /** The type of the node implemented in this file */
12
+ export const NODE_TYPE = 'vector_expression';
13
+
14
+ export default function (path: AstPath<Node>): treeFn | null {
15
+ if (path.node.type === NODE_TYPE) {
16
+ return printVectorExpression;
17
+ }
18
+
19
+ return null;
20
+ }
21
+
22
+ /**
23
+ * Print `vector_expression` node.
24
+ */
25
+ function printVectorExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
26
+ if (path.node.namedChildCount === 0) {
27
+ return 'vector[]';
28
+ }
29
+
30
+ // Injected print callback for elements in the vector
31
+ const printCb = (path: AstPath<Node>) => printElement(path, options, print);
32
+ const trailing = path.node.trailingComment;
33
+ let trailingComment: Doc = '';
34
+
35
+ if (trailing?.type === 'line_comment') {
36
+ trailingComment = printTrailingComment(path, false);
37
+ path.node.disableTrailingComment();
38
+ }
39
+
40
+ // Vector without type specified
41
+ // Eg: `vector[....]`
42
+ if (path.node.child(0)?.text == 'vector[') {
43
+ return group(
44
+ [
45
+ 'vector',
46
+ list({ path, print: printCb, options, open: '[', close: ']' }) as Doc[],
47
+ lineSuffix(trailingComment),
48
+ ],
49
+ { shouldBreak: false },
50
+ );
51
+ }
52
+
53
+ if (!path.node.nonFormattingChildren[0]?.isTypeParam) {
54
+ throw new Error(
55
+ `Expected a type parameter in the \`vector_expression\`, got \`${path.node.nonFormattingChildren[0]?.type}\``,
56
+ );
57
+ }
58
+
59
+ if (path.node.nonFormattingChildren.slice(1).some((child) => child.isTypeParam)) {
60
+ throw new Error('Expected only one type parameter in the `vector_expression`');
61
+ }
62
+
63
+ // Vector with type
64
+ // Eg: `vector<TYPE>[...]`
65
+ return [
66
+ 'vector<',
67
+ // do not break the type in vector literal
68
+ // indent(softline),
69
+ group(path.call(print, 'nonFormattingChildren', 0), { shouldBreak: false }),
70
+ '>',
71
+ group(
72
+ list({
73
+ path,
74
+ print: printCb,
75
+ options,
76
+ open: '[',
77
+ close: ']',
78
+ skipChildren: 1,
79
+ shouldBreak: false,
80
+ }) as Doc[],
81
+ ),
82
+ lineSuffix(trailingComment),
83
+ ];
84
+ }
85
+
86
+ /**
87
+ * Special print elements in the `vector_expression`.
88
+ *
89
+ * - we want to use breakable blocks for `block` nodes;
90
+ */
91
+ function printElement(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
92
+ if (path.node.type === 'block') {
93
+ return printBreakableBlock(path, options, print);
94
+ }
95
+
96
+ return print(path);
97
+ }
@@ -0,0 +1,45 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Node } from '../..';
5
+ import { MoveOptions, printFn, treeFn } from '../../printer';
6
+ import { AstPath, Doc, doc } from 'prettier';
7
+ const { indent, softline, group } = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'while_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printWhileExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `while_expression` node.
22
+ *
23
+ * ```
24
+ * // single line
25
+ * while (bool_expr) expr
26
+ *
27
+ * // break condition
28
+ * while (
29
+ * very_long_expr &&
30
+ * very_long_expr
31
+ * ) {
32
+ * expr;
33
+ * }
34
+ * ```
35
+ */
36
+ function printWhileExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
37
+ const condition = path.node.nonFormattingChildren[0]!.isList
38
+ ? [indent(softline), path.call(print, 'nonFormattingChildren', 0), softline]
39
+ : [indent(softline), indent(path.call(print, 'nonFormattingChildren', 0)), softline];
40
+
41
+ return [
42
+ ['while (', group(condition), ') '],
43
+ path.call(print, 'nonFormattingChildren', 1), // body
44
+ ];
45
+ }
@@ -0,0 +1,100 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Node } from '..';
5
+ import { treeFn } from '../printer';
6
+ import { AstPath, Doc } from 'prettier';
7
+
8
+ /**
9
+ * Creates a callback function to print commments and comment-related nodes.
10
+ *
11
+ * @param path
12
+ * @returns
13
+ */
14
+ export default function (path: AstPath<Node>): treeFn | null {
15
+ switch (path.node.type) {
16
+ case Formatting.LineComment:
17
+ return printLineComment;
18
+ case Formatting.BlockComment:
19
+ return printBlockComment;
20
+ case Formatting.EmptyLine:
21
+ return printEmptyLine;
22
+ case Formatting.Newline:
23
+ return printNewline;
24
+ default:
25
+ return null;
26
+ }
27
+ }
28
+
29
+ export enum Formatting {
30
+ LineComment = 'line_comment',
31
+ BlockComment = 'block_comment',
32
+ /**
33
+ * Token that doesn't exist in the grammar but we insert it in
34
+ * the `Tree` representation of CST to represent an empty line.
35
+ */
36
+ EmptyLine = 'empty_line',
37
+ /**
38
+ * Special node to insert a newline before the next node.
39
+ * We use it to make a call to hardline or not.
40
+ */
41
+ Newline = 'newline',
42
+ }
43
+
44
+ export function startsOnNewLine(path: AstPath<Node>): boolean {
45
+ return path.previous?.type == Formatting.EmptyLine;
46
+ }
47
+
48
+ export function shouldNewLine(path: AstPath<Node>): boolean {
49
+ return path.node.nextNamedSibling?.type == Formatting.Newline;
50
+ }
51
+
52
+ /**
53
+ * Test if a node is a formatting node.
54
+ *
55
+ * @param node
56
+ * @returns
57
+ */
58
+ export function isFormatting(node: Node): boolean {
59
+ return [
60
+ Formatting.LineComment,
61
+ Formatting.BlockComment,
62
+ Formatting.EmptyLine,
63
+ Formatting.Newline,
64
+ ].includes(node.type as Formatting);
65
+ }
66
+
67
+ export function isComment(node: Node | null): boolean {
68
+ return [Formatting.LineComment, Formatting.BlockComment].includes(node?.type as Formatting);
69
+ }
70
+
71
+ export function isEmptyLine(node: Node | null): boolean {
72
+ return Formatting.EmptyLine == node?.type;
73
+ }
74
+
75
+ export function isNewline(node: Node | null): boolean {
76
+ return Formatting.Newline == node?.type;
77
+ }
78
+
79
+ /**
80
+ * Print `line_comment` node.
81
+ * Comments are handled via the `addLeadingComments` function.
82
+ */
83
+ export function printLineComment(path: AstPath<Node>): Doc {
84
+ return path.node.text;
85
+ }
86
+
87
+ /**
88
+ * Print `block_comment` node.
89
+ */
90
+ export function printBlockComment(path: AstPath<Node>): Doc {
91
+ return path.node.text;
92
+ }
93
+
94
+ export function printEmptyLine(path: AstPath<Node>): Doc {
95
+ return ''; // should not be printed directly, used in `join(hardline)` to act as an extra newline
96
+ }
97
+
98
+ export function printNewline(path: AstPath<Node>): Doc {
99
+ return ''; // should not be printed, ever
100
+ }