@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,73 @@
1
+ // Copyright (c) The Move Contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { AstPath, Doc, doc } from 'prettier';
5
+ import { Node } from '..';
6
+ import { MoveOptions, printFn, treeFn } from '../printer';
7
+ import { list, printIdentifier, printTrailingComment } from '../utilities';
8
+ const { join, lineSuffix, group } = doc.builders;
9
+
10
+ export default function (path: AstPath<Node>): treeFn | null {
11
+ switch (path.node.type) {
12
+ case EnumDefinition.EnumDefinition:
13
+ return printEnumDefinition;
14
+ case EnumDefinition.EnumVariants:
15
+ return printEnumVariants;
16
+ case EnumDefinition.Variant:
17
+ return printVariant;
18
+
19
+ // identifiers
20
+ case EnumDefinition.EnumIdentifier:
21
+ case EnumDefinition.VariantIdentifier:
22
+ return printIdentifier;
23
+ }
24
+
25
+ return null;
26
+ }
27
+
28
+ export enum EnumDefinition {
29
+ EnumDefinition = 'enum_definition',
30
+ EnumVariants = 'enum_variants',
31
+ Variant = 'variant',
32
+
33
+ EnumIdentifier = 'enum_identifier',
34
+ VariantIdentifier = 'variant_identifier',
35
+ }
36
+
37
+ /**
38
+ * Print `enum_definition` node.
39
+ */
40
+ export function printEnumDefinition(path: AstPath<Node>, _opt: MoveOptions, print: printFn): Doc {
41
+ const isPublic = path.node.child(0)?.type == 'public';
42
+
43
+ return [isPublic ? 'public ' : '', 'enum ', path.map(print, 'nonFormattingChildren')];
44
+ }
45
+
46
+ /**
47
+ * Print `enum_variants` node.
48
+ * List of `variant` nodes.
49
+ */
50
+ export function printEnumVariants(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
51
+ if (path.node.nonFormattingChildren.length === 0) {
52
+ return ' {}';
53
+ }
54
+
55
+ return [
56
+ ' ',
57
+ group(list({ path, print, options, open: '{', close: '}', addWhitespace: true }), {
58
+ shouldBreak: true,
59
+ }),
60
+ ];
61
+ }
62
+
63
+ /**
64
+ * Print `variant` node.
65
+ * Inside:
66
+ * - `variant_identifier`
67
+ * - `datatype_fields` (optional)
68
+ */
69
+ export function printVariant(path: AstPath<Node>, _opt: MoveOptions, print: printFn): Doc {
70
+ const trailing = lineSuffix(printTrailingComment(path, false));
71
+ path.node.disableTrailingComment();
72
+ return [path.map(print, 'nonFormattingChildren'), trailing];
73
+ }
@@ -0,0 +1,35 @@
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 { group, indent, line } = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'abort_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printAbortExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `abort_expression` node.
22
+ */
23
+ function printAbortExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
24
+ const expression = path.node.nonFormattingChildren[0];
25
+ const printed = path.call(print, 'nonFormattingChildren', 0);
26
+
27
+ if (!expression) return 'abort';
28
+
29
+ return group([
30
+ 'abort',
31
+ expression?.isList || expression?.isControlFlow
32
+ ? [' ', printed]
33
+ : [indent(line), indent(printed)],
34
+ ]);
35
+ }
@@ -0,0 +1,38 @@
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 { group, indent, softline } = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'annotation_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printAnnotationExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `annotation_expression` node.
22
+ */
23
+ function printAnnotationExpression(path: AstPath<Node>, _opt: MoveOptions, print: printFn): Doc {
24
+ const children = path.map(print, 'nonFormattingChildren');
25
+ if (children.length !== 2) {
26
+ throw new Error('`annotation_expression` node should have 2 children');
27
+ }
28
+
29
+ return group([
30
+ '(',
31
+ indent(softline),
32
+ indent(children[0]!), // expression
33
+ ': ',
34
+ children[1]!, // type
35
+ softline,
36
+ ')',
37
+ ]);
38
+ }
@@ -0,0 +1,66 @@
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 { printTrailingComment } from '../../utilities';
8
+ const { group, indent, line } = doc.builders;
9
+
10
+ /** The type of the node implemented in this file */
11
+ export const NODE_TYPE = 'assign_expression';
12
+
13
+ export default function (path: AstPath<Node>): treeFn | null {
14
+ if (path.node.type === NODE_TYPE) {
15
+ return printAssignExpression;
16
+ }
17
+
18
+ return null;
19
+ }
20
+
21
+ /**
22
+ * Print `assign_expression` node.
23
+ */
24
+ function printAssignExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
25
+ if (path.node.nonFormattingChildren.length !== 2) {
26
+ throw new Error('`assign_expression` must have 2 children');
27
+ }
28
+
29
+ const result: Doc[] = [];
30
+ let shouldBreak = false;
31
+
32
+ // together with the LHS we print trailing comment if there is one
33
+ result.push(
34
+ path.call(
35
+ (lhs) => {
36
+ const hasComment = !!lhs.node.trailingComment;
37
+
38
+ if (lhs.node.trailingComment?.type == 'line_comment') {
39
+ shouldBreak = true;
40
+ const trailingLineComment = printTrailingComment(lhs, true);
41
+ lhs.node.disableTrailingComment();
42
+ return [print(lhs), ' =', indent(trailingLineComment)];
43
+ }
44
+
45
+ return [print(lhs), hasComment ? '=' : ' ='];
46
+ },
47
+ 'nonFormattingChildren',
48
+ 0,
49
+ ),
50
+ );
51
+
52
+ const rhs = path.node.nonFormattingChildren[1]!;
53
+ if ((rhs.isControlFlow || rhs.isList) && !shouldBreak) {
54
+ result.push(group([' ', path.call(print, 'nonFormattingChildren', 1)]));
55
+ } else {
56
+ // then print the rhs
57
+ result.push(
58
+ group([
59
+ shouldBreak ? '' : indent(line),
60
+ indent(path.call(print, 'nonFormattingChildren', 1)),
61
+ ]),
62
+ );
63
+ }
64
+
65
+ return result;
66
+ }
@@ -0,0 +1,75 @@
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 { group, line } = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'binary_expression';
11
+
12
+ // TODO: re-enable binary expression once we figure out how to achieve it.
13
+ export default function (path: AstPath<Node>): treeFn | null {
14
+ if (path.node.type === NODE_TYPE) {
15
+ return () => path.node.text;
16
+ // TODO: re-enable binary expression once we figure out how to achieve it.
17
+ // return printBinaryExpression;
18
+ } else if (path.node.type === 'binary_operator') {
19
+ // return printBinaryOperator;
20
+ // TODO: re-enable binary expression once we figure out how to achieve it.
21
+ return () => path.node.text;
22
+ }
23
+
24
+ return null;
25
+ }
26
+
27
+ /**
28
+ * Print `binary_expression` node.
29
+ * (Currently disabled)
30
+ */
31
+ function printBinaryExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
32
+ if (path.node.nonFormattingChildren.length != 3) {
33
+ throw new Error('`binary_expression` node should have 3 children');
34
+ }
35
+
36
+ const [one, two, three] = path.map(print, 'nonFormattingChildren');
37
+ const rhs = path.node.nonFormattingChildren[2];
38
+
39
+ if (rhs?.type === 'block' || rhs?.type === 'expression_list') {
40
+ return [one!, ' ', two!, ' ', three!];
41
+ }
42
+
43
+ return [one!, ' ', two!, group([line, three!], { shouldBreak: false })];
44
+ }
45
+
46
+ /**
47
+ * Print `binary_operator` node.
48
+ */
49
+ export function printBinaryOperator(path: AstPath<Node>, _opt: MoveOptions, _p: printFn): Doc {
50
+ return path.node.text;
51
+ }
52
+
53
+ /**
54
+
55
+ or: '||',
56
+ and: '&&',
57
+ eq: '==',
58
+ neq: '!=',
59
+ lt: '<',
60
+ gt: '>',
61
+ le: '<=',
62
+ ge: '>=',
63
+ bit_or: '|',
64
+ xor: '^',
65
+ bit_and: '&',
66
+ shl: '<<',
67
+ shr: '>>',
68
+ add: '+',
69
+ sub: '-',
70
+ mul: '*',
71
+ div: '/',
72
+ mod: '%'
73
+
74
+
75
+ */
@@ -0,0 +1,72 @@
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 { block, shouldBreakFirstChild } from '../../utilities';
8
+ const { group, indent, join, conditionalGroup, hardlineWithoutBreakParent } = doc.builders;
9
+
10
+ /** The type of the node implemented in this file */
11
+ const NODE_TYPE = 'block';
12
+
13
+ export default function (path: AstPath<Node>): treeFn | null {
14
+ if (path.node.type === NODE_TYPE) {
15
+ return printBlock;
16
+ }
17
+
18
+ return null;
19
+ }
20
+
21
+ /**
22
+ * Special case of `block` node, that does not break the parent. A must-have for
23
+ * lambda expressions.
24
+ */
25
+ export function printNonBreakingBlock(
26
+ path: AstPath<Node>,
27
+ options: MoveOptions,
28
+ print: printFn,
29
+ ): Doc {
30
+ const length = path.node.nonFormattingChildren.length;
31
+
32
+ if (length == 0) {
33
+ return '{}';
34
+ }
35
+
36
+ return group([
37
+ '{',
38
+ indent(hardlineWithoutBreakParent),
39
+ indent(join(hardlineWithoutBreakParent, path.map(print, 'namedAndEmptyLineChildren'))),
40
+ hardlineWithoutBreakParent,
41
+ '}',
42
+ ]);
43
+ }
44
+
45
+ export function printBreakableBlock(
46
+ path: AstPath<Node>,
47
+ options: MoveOptions,
48
+ print: printFn,
49
+ ): Doc {
50
+ const length = path.node.nonFormattingChildren.length;
51
+
52
+ if (length == 0) {
53
+ return '{}';
54
+ }
55
+
56
+ return block({
57
+ options,
58
+ print,
59
+ path,
60
+ shouldBreak: shouldBreakFirstChild(path),
61
+ });
62
+ }
63
+
64
+ /**
65
+ * Print `block` node.
66
+ */
67
+ export function printBlock(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
68
+ return conditionalGroup([
69
+ printBreakableBlock(path, options, print),
70
+ printNonBreakingBlock(path, options, print),
71
+ ]);
72
+ }
@@ -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
+ import { printTrailingComment } from '../../utilities';
8
+ const { group, lineSuffix } = doc.builders;
9
+
10
+ /** The type of the node implemented in this file */
11
+ const NODE_TYPE = 'block_item';
12
+
13
+ export default function (path: AstPath<Node>): treeFn | null {
14
+ if (path.node.type === NODE_TYPE) {
15
+ return printBlockItem;
16
+ }
17
+
18
+ return null;
19
+ }
20
+
21
+ /**
22
+ * Print `block_item` node.
23
+ */
24
+ function printBlockItem(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
25
+ const trailing = lineSuffix(printTrailingComment(path));
26
+ path.node.disableTrailingComment();
27
+
28
+ return [group([path.call(print, 'nonFormattingChildren', 0), ';', trailing])];
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 = 'borrow_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printBorrowExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `borrow_expression` node.
22
+ */
23
+ function printBorrowExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
24
+ return [
25
+ path.call(print, 'nonFormattingChildren', 0), // borrow type
26
+ path.call(print, 'nonFormattingChildren', 1), // expression
27
+ ];
28
+ }
@@ -0,0 +1,33 @@
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 } = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'break_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printBreakExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `break_expression` node.
22
+ */
23
+ export function printBreakExpression(
24
+ path: AstPath<Node>,
25
+ options: MoveOptions,
26
+ print: printFn,
27
+ ): Doc {
28
+ if (path.node.nonFormattingChildren.length > 0) {
29
+ return join(' ', ['break', ...path.map(print, 'nonFormattingChildren')]);
30
+ }
31
+
32
+ return 'break';
33
+ }
@@ -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
+
8
+ /** The type of the node implemented in this file */
9
+ export const NODE_TYPE = 'call_expression';
10
+
11
+ export default function (path: AstPath<Node>): treeFn | null {
12
+ if (path.node.type === NODE_TYPE) {
13
+ return printCallExpression;
14
+ }
15
+
16
+ return null;
17
+ }
18
+
19
+ /**
20
+ * Print `call_expression` node.
21
+ * Inside:
22
+ * - `module_access`
23
+ * - `type_arguments`
24
+ * - `arg_list`
25
+ */
26
+ function printCallExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
27
+ return path.map(print, 'nonFormattingChildren');
28
+ }
@@ -0,0 +1,35 @@
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 } = doc.builders;
8
+
9
+ /** The type of the node implemented in this file */
10
+ export const NODE_TYPE = 'cast_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printCastExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `cast_expression` node.
22
+ * Inside:
23
+ * - `expression`
24
+ * - `type`
25
+ */
26
+ function printCastExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
27
+ const parens = path.node.child(0)?.text == '(';
28
+ const children = path.map(print, 'nonFormattingChildren');
29
+
30
+ if (parens) {
31
+ return ['(', join(' as ', children), ')'];
32
+ }
33
+
34
+ return join(' as ', children);
35
+ }
@@ -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 = 'continue_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printContinueExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `continue_expression` node.
22
+ */
23
+ function printContinueExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
24
+ if (path.node.nonFormattingChildren.length === 1) {
25
+ return ['continue ', path.call(print, 'nonFormattingChildren', 0)];
26
+ }
27
+
28
+ return 'continue';
29
+ }
@@ -0,0 +1,33 @@
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 = 'dereference_expression';
11
+
12
+ export default function (path: AstPath<Node>): treeFn | null {
13
+ if (path.node.type === NODE_TYPE) {
14
+ return printDereferenceExpression;
15
+ }
16
+
17
+ return null;
18
+ }
19
+
20
+ /**
21
+ * Print `dereference_expression` node.
22
+ *
23
+ * Inside:
24
+ * - `*`
25
+ * - `_expression`
26
+ */
27
+ function printDereferenceExpression(
28
+ path: AstPath<Node>,
29
+ options: MoveOptions,
30
+ print: printFn,
31
+ ): Doc {
32
+ return ['*', path.call(print, 'nonFormattingChildren', 0)];
33
+ }
@@ -0,0 +1,89 @@
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 { printLeadingComment, printTrailingComment } from '../../utilities';
8
+ const { group, indent, ifBreak, breakParent, lineSuffix, softline } = doc.builders;
9
+
10
+ /** The type of the node implemented in this file */
11
+ export const NODE_TYPE = 'dot_expression';
12
+
13
+ export default function (path: AstPath<Node>): treeFn | null {
14
+ if (path.node.type === NODE_TYPE) {
15
+ return printDotExpression;
16
+ }
17
+
18
+ return null;
19
+ }
20
+
21
+ /**
22
+ * Print `dot_expression` node.
23
+ *
24
+ * Note, that it's intentional, that the return value is not a `group`. For more info,
25
+ * @see [This Issue](https://github.com/prettier/prettier/issues/15710#issuecomment-1836701758)
26
+ */
27
+ function printDotExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
28
+ if (path.node.nonFormattingChildren.length < 2) {
29
+ throw new Error('`dot_expression` node should have at least 2 children');
30
+ }
31
+
32
+ // chain is a `dot_expression` that is a child of another `dot_expression`
33
+ // or which has a `dot_expression` as a child
34
+ const isChain =
35
+ path.node.nonFormattingChildren[0]!.type === NODE_TYPE ||
36
+ path.node.parent?.type === NODE_TYPE;
37
+
38
+ const isParentList = path.node.parent?.isList;
39
+
40
+ // if dot expression has a trailing comment and it breaks, we need to
41
+ // print it manually after the rhs
42
+ const trailing = lineSuffix(printTrailingComment(path));
43
+
44
+ const lhs = path.call(
45
+ (path) => printNode(path, options, print, false),
46
+ 'nonFormattingChildren',
47
+ 0,
48
+ );
49
+ const rhs = path.call(
50
+ (path) => printNode(path, options, print, true),
51
+ 'nonFormattingChildren',
52
+ 1,
53
+ );
54
+
55
+ // if it's a single expression, we don't need to group it
56
+ // and optionally no need to break it; no need to special
57
+ // print it in this case
58
+ if (!isChain) {
59
+ const right = path.node.nonFormattingChildren[1]!;
60
+ if (right.leadingComment.length > 0) {
61
+ path.node.disableTrailingComment();
62
+ return [lhs, indent(softline), indent(rhs), trailing];
63
+ }
64
+
65
+ return [lhs, rhs];
66
+ }
67
+
68
+ path.node.disableTrailingComment();
69
+ const parts = [lhs, ifBreak(indent(softline), ''), ifBreak(indent(rhs), rhs), trailing];
70
+
71
+ // group if parent is not `dot_expression`
72
+ if (isChain && path.node.parent?.type !== NODE_TYPE) {
73
+ return group(parts);
74
+ }
75
+
76
+ return parts;
77
+ }
78
+
79
+ // In `dot_expression` we need to keep the `.` in the same line as the `rhs`,
80
+ // so we need to prevent automatic printing of comments in the `print` call, and
81
+ // perform it manually.
82
+ function printNode(path: AstPath<Node>, options: MoveOptions, print: printFn, insertDot = false) {
83
+ const leading = printLeadingComment(path, options);
84
+ const shouldBreak =
85
+ path.node.leadingComment.length > 0 || path.node.trailingComment?.type === 'line_comment';
86
+
87
+ path.node.disableLeadingComment();
88
+ return [leading, shouldBreak ? breakParent : '', insertDot ? '.' : '', print(path)];
89
+ }