prettier 1.6.1 → 2.0.0.pre.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +316 -293
  3. data/CONTRIBUTING.md +6 -9
  4. data/LICENSE +1 -1
  5. data/README.md +11 -12
  6. data/dist/haml/embed.js +53 -0
  7. data/dist/haml/parser.js +31 -0
  8. data/{src → dist}/haml/parser.rb +0 -0
  9. data/dist/haml/printer.js +336 -0
  10. data/dist/parser/getInfo.js +17 -0
  11. data/{src → dist}/parser/netcat.js +1 -0
  12. data/dist/parser/parseSync.js +128 -0
  13. data/dist/parser/server.rb +140 -0
  14. data/dist/plugin.js +143 -0
  15. data/dist/prettier.js +15 -0
  16. data/dist/rbs/parser.js +34 -0
  17. data/{src → dist}/rbs/parser.rb +0 -0
  18. data/dist/rbs/printer.js +517 -0
  19. data/dist/ruby/embed.js +110 -0
  20. data/dist/ruby/nodes/alias.js +59 -0
  21. data/{src → dist}/ruby/nodes/aref.js +26 -35
  22. data/dist/ruby/nodes/args.js +165 -0
  23. data/dist/ruby/nodes/arrays.js +126 -0
  24. data/dist/ruby/nodes/assign.js +41 -0
  25. data/dist/ruby/nodes/blocks.js +68 -0
  26. data/dist/ruby/nodes/calls.js +220 -0
  27. data/dist/ruby/nodes/case.js +50 -0
  28. data/dist/ruby/nodes/class.js +54 -0
  29. data/dist/ruby/nodes/commands.js +124 -0
  30. data/dist/ruby/nodes/conditionals.js +242 -0
  31. data/dist/ruby/nodes/constants.js +38 -0
  32. data/dist/ruby/nodes/flow.js +66 -0
  33. data/dist/ruby/nodes/hashes.js +130 -0
  34. data/dist/ruby/nodes/heredocs.js +30 -0
  35. data/dist/ruby/nodes/hooks.js +35 -0
  36. data/dist/ruby/nodes/ints.js +27 -0
  37. data/dist/ruby/nodes/lambdas.js +69 -0
  38. data/dist/ruby/nodes/loops.js +73 -0
  39. data/dist/ruby/nodes/massign.js +73 -0
  40. data/dist/ruby/nodes/methods.js +70 -0
  41. data/dist/ruby/nodes/operators.js +70 -0
  42. data/dist/ruby/nodes/params.js +89 -0
  43. data/dist/ruby/nodes/patterns.js +109 -0
  44. data/dist/ruby/nodes/regexp.js +45 -0
  45. data/dist/ruby/nodes/rescue.js +82 -0
  46. data/dist/ruby/nodes/return.js +75 -0
  47. data/dist/ruby/nodes/statements.js +111 -0
  48. data/dist/ruby/nodes/strings.js +218 -0
  49. data/dist/ruby/nodes/super.js +30 -0
  50. data/dist/ruby/nodes/undef.js +26 -0
  51. data/dist/ruby/nodes.js +151 -0
  52. data/dist/ruby/parser.js +34 -0
  53. data/{src → dist}/ruby/parser.rb +1215 -252
  54. data/dist/ruby/printer.js +125 -0
  55. data/dist/ruby/toProc.js +93 -0
  56. data/dist/types/haml.js +4 -0
  57. data/dist/types/plugin.js +3 -0
  58. data/dist/types/rbs.js +4 -0
  59. data/dist/types/ruby.js +4 -0
  60. data/dist/types/utils.js +2 -0
  61. data/dist/types.js +30 -0
  62. data/dist/utils/containsAssignment.js +15 -0
  63. data/dist/utils/getTrailingComma.js +6 -0
  64. data/dist/utils/hasAncestor.js +15 -0
  65. data/{src → dist}/utils/inlineEnsureParens.js +16 -17
  66. data/dist/utils/isEmptyBodyStmt.js +10 -0
  67. data/dist/utils/isEmptyStmts.js +10 -0
  68. data/dist/utils/literal.js +8 -0
  69. data/dist/utils/literallineWithoutBreakParent.js +8 -0
  70. data/dist/utils/makeCall.js +13 -0
  71. data/dist/utils/noIndent.js +11 -0
  72. data/dist/utils/printEmptyCollection.js +44 -0
  73. data/dist/utils/skipAssignIndent.js +15 -0
  74. data/dist/utils.js +30 -0
  75. data/node_modules/prettier/bin-prettier.js +313 -190
  76. data/node_modules/prettier/doc.js +191 -323
  77. data/node_modules/prettier/index.js +2753 -3677
  78. data/node_modules/prettier/package.json +1 -1
  79. data/node_modules/prettier/parser-angular.js +13 -14
  80. data/node_modules/prettier/parser-babel.js +7 -7
  81. data/node_modules/prettier/parser-espree.js +7 -7
  82. data/node_modules/prettier/parser-flow.js +7 -7
  83. data/node_modules/prettier/parser-glimmer.js +1 -1
  84. data/node_modules/prettier/parser-graphql.js +1 -1
  85. data/node_modules/prettier/parser-html.js +17 -17
  86. data/node_modules/prettier/parser-markdown.js +9 -9
  87. data/node_modules/prettier/parser-meriyah.js +7 -7
  88. data/node_modules/prettier/parser-postcss.js +2 -2
  89. data/node_modules/prettier/parser-typescript.js +7 -7
  90. data/node_modules/prettier/parser-yaml.js +2 -2
  91. data/node_modules/prettier/third-party.js +143 -78
  92. data/package.json +26 -18
  93. metadata +74 -67
  94. data/src/haml/embed.js +0 -87
  95. data/src/haml/parser.js +0 -23
  96. data/src/haml/printer.js +0 -438
  97. data/src/parser/parseSync.js +0 -172
  98. data/src/parser/server.rb +0 -66
  99. data/src/plugin.js +0 -148
  100. data/src/prettier.js +0 -16
  101. data/src/rbs/parser.js +0 -37
  102. data/src/rbs/printer.js +0 -643
  103. data/src/ruby/embed.js +0 -142
  104. data/src/ruby/nodes/alias.js +0 -73
  105. data/src/ruby/nodes/args.js +0 -222
  106. data/src/ruby/nodes/arrays.js +0 -162
  107. data/src/ruby/nodes/assign.js +0 -47
  108. data/src/ruby/nodes/blocks.js +0 -90
  109. data/src/ruby/nodes/calls.js +0 -246
  110. data/src/ruby/nodes/case.js +0 -65
  111. data/src/ruby/nodes/class.js +0 -64
  112. data/src/ruby/nodes/commands.js +0 -131
  113. data/src/ruby/nodes/conditionals.js +0 -282
  114. data/src/ruby/nodes/constants.js +0 -43
  115. data/src/ruby/nodes/flow.js +0 -74
  116. data/src/ruby/nodes/hashes.js +0 -155
  117. data/src/ruby/nodes/heredocs.js +0 -36
  118. data/src/ruby/nodes/hooks.js +0 -34
  119. data/src/ruby/nodes/ints.js +0 -31
  120. data/src/ruby/nodes/lambdas.js +0 -76
  121. data/src/ruby/nodes/loops.js +0 -98
  122. data/src/ruby/nodes/massign.js +0 -98
  123. data/src/ruby/nodes/methods.js +0 -74
  124. data/src/ruby/nodes/operators.js +0 -83
  125. data/src/ruby/nodes/params.js +0 -106
  126. data/src/ruby/nodes/patterns.js +0 -157
  127. data/src/ruby/nodes/regexp.js +0 -56
  128. data/src/ruby/nodes/rescue.js +0 -101
  129. data/src/ruby/nodes/return.js +0 -94
  130. data/src/ruby/nodes/statements.js +0 -142
  131. data/src/ruby/nodes/strings.js +0 -272
  132. data/src/ruby/nodes/super.js +0 -35
  133. data/src/ruby/nodes/undef.js +0 -42
  134. data/src/ruby/nodes.js +0 -34
  135. data/src/ruby/parser.js +0 -37
  136. data/src/ruby/printer.js +0 -147
  137. data/src/ruby/toProc.js +0 -105
  138. data/src/utils/containsAssignment.js +0 -11
  139. data/src/utils/getTrailingComma.js +0 -5
  140. data/src/utils/hasAncestor.js +0 -17
  141. data/src/utils/isEmptyBodyStmt.js +0 -7
  142. data/src/utils/isEmptyStmts.js +0 -11
  143. data/src/utils/literal.js +0 -7
  144. data/src/utils/literallineWithoutBreakParent.js +0 -7
  145. data/src/utils/makeCall.js +0 -14
  146. data/src/utils/noIndent.js +0 -10
  147. data/src/utils/printEmptyCollection.js +0 -49
  148. data/src/utils/skipAssignIndent.js +0 -17
  149. data/src/utils.js +0 -13
@@ -1,90 +0,0 @@
1
- const {
2
- breakParent,
3
- concat,
4
- group,
5
- ifBreak,
6
- indent,
7
- join,
8
- removeLines,
9
- softline
10
- } = require("../../prettier");
11
- const { hasAncestor } = require("../../utils");
12
-
13
- function printBlockVar(path, opts, print) {
14
- const parts = ["|", removeLines(path.call(print, "body", 0))];
15
-
16
- // The second part of this node is a list of optional block-local variables
17
- if (path.getValue().body[1]) {
18
- parts.push("; ", join(", ", path.map(print, "body", 1)));
19
- }
20
-
21
- parts.push("| ");
22
- return concat(parts);
23
- }
24
-
25
- function printBlock(braces) {
26
- return function printBlockWithBraces(path, opts, print) {
27
- const [variables, statements] = path.getValue().body;
28
- const stmts =
29
- statements.type === "stmts" ? statements.body : statements.body[0].body;
30
-
31
- let doBlockBody = "";
32
- if (
33
- stmts.length !== 1 ||
34
- stmts[0].type !== "void_stmt" ||
35
- stmts[0].comments
36
- ) {
37
- doBlockBody = indent(concat([softline, path.call(print, "body", 1)]));
38
- }
39
-
40
- // If this block is nested underneath a command or command_call node, then
41
- // we can't use `do...end` because that will get associated with the parent
42
- // node as opposed to the current node (because of the difference in
43
- // operator precedence). Instead, we still use a multi-line format but
44
- // switch to using braces instead.
45
- const useBraces = braces && hasAncestor(path, ["command", "command_call"]);
46
-
47
- const doBlock = concat([
48
- useBraces ? " {" : " do",
49
- variables ? concat([" ", path.call(print, "body", 0)]) : "",
50
- doBlockBody,
51
- concat([softline, useBraces ? "}" : "end"])
52
- ]);
53
-
54
- // We can hit this next pattern if within the block the only statement is a
55
- // comment.
56
- if (
57
- stmts.length === 1 &&
58
- stmts[0].type === "void_stmt" &&
59
- stmts[0].comments
60
- ) {
61
- return concat([breakParent, doBlock]);
62
- }
63
-
64
- const blockReceiver = path.getParentNode().body[0];
65
-
66
- // If the parent node is a command node, then there are no parentheses
67
- // around the arguments to that command, so we need to break the block
68
- if (["command", "command_call"].includes(blockReceiver.type)) {
69
- return concat([breakParent, doBlock]);
70
- }
71
-
72
- const hasBody = stmts.some(({ type }) => type !== "void_stmt");
73
- const braceBlock = concat([
74
- " {",
75
- hasBody || variables ? " " : "",
76
- variables ? path.call(print, "body", 0) : "",
77
- path.call(print, "body", 1),
78
- hasBody ? " " : "",
79
- "}"
80
- ]);
81
-
82
- return group(ifBreak(doBlock, braceBlock));
83
- };
84
- }
85
-
86
- module.exports = {
87
- block_var: printBlockVar,
88
- brace_block: printBlock(true),
89
- do_block: printBlock(false)
90
- };
@@ -1,246 +0,0 @@
1
- const {
2
- concat,
3
- group,
4
- hardline,
5
- ifBreak,
6
- indent,
7
- join,
8
- softline
9
- } = require("../../prettier");
10
- const { makeCall, noIndent } = require("../../utils");
11
-
12
- const toProc = require("../toProc");
13
-
14
- const chained = ["call", "method_add_arg", "method_add_block"];
15
-
16
- function printCall(path, opts, print) {
17
- const node = path.getValue();
18
- const [receiverNode, _operatorNode, messageNode] = node.body;
19
-
20
- const receiverDoc = path.call(print, "body", 0);
21
- const operatorDoc = makeCall(path, opts, print);
22
-
23
- // You can call lambdas with a special syntax that looks like func.(*args).
24
- // In this case, "call" is returned for the 3rd child node. We don't alter
25
- // call syntax so if `call` is implicit, we don't print it out.
26
- const messageDoc = messageNode === "call" ? "" : path.call(print, "body", 2);
27
-
28
- // For certain left sides of the call nodes, we want to attach directly to
29
- // the } or end.
30
- if (noIndent.includes(receiverNode.type)) {
31
- return concat([receiverDoc, operatorDoc, messageDoc]);
32
- }
33
-
34
- // The right side of the call node, as in everything including the operator
35
- // and beyond.
36
- let rightSideDoc = concat([
37
- receiverNode.comments ? hardline : softline,
38
- operatorDoc,
39
- messageDoc
40
- ]);
41
-
42
- // This is very specialized behavior wherein we group .where.not calls
43
- // together because it looks better. For more information, see
44
- // https://github.com/prettier/plugin-ruby/issues/862.
45
- if (
46
- receiverNode.type === "call" &&
47
- receiverNode.body[2].body === "where" &&
48
- messageDoc === "not"
49
- ) {
50
- rightSideDoc = concat([operatorDoc, messageDoc]);
51
- }
52
-
53
- // Get a reference to the parent node so we can check if we're inside a chain
54
- const parentNode = path.getParentNode();
55
-
56
- // If our parent node is a chained node then we're not going to group the
57
- // right side of the expression, as we want to have a nice multi-line layout.
58
- if (chained.includes(parentNode.type) && !node.comments) {
59
- parentNode.chain = (node.chain || 0) + 1;
60
- parentNode.callChain = (node.callChain || 0) + 1;
61
- parentNode.breakDoc = (node.breakDoc || [receiverDoc]).concat(rightSideDoc);
62
- parentNode.firstReceiverType = node.firstReceiverType || receiverNode.type;
63
- }
64
-
65
- // If we're at the top of a chain, then we're going to print out a nice
66
- // multi-line layout if this doesn't break into multiple lines.
67
- if (!chained.includes(parentNode.type) && (node.chain || 0) >= 3) {
68
- return ifBreak(
69
- group(indent(concat(node.breakDoc.concat(rightSideDoc)))),
70
- concat([receiverDoc, group(rightSideDoc)])
71
- );
72
- }
73
-
74
- return group(concat([receiverDoc, group(indent(rightSideDoc))]));
75
- }
76
-
77
- function printMethodAddArg(path, opts, print) {
78
- const node = path.getValue();
79
-
80
- const [methodNode, argNode] = node.body;
81
- const [methodDoc, argsDoc] = path.map(print, "body");
82
-
83
- // You can end up here if you have a method with a ? ending, presumably
84
- // because the parser knows that it cannot be a local variable. You can also
85
- // end up here if you are explicitly using an empty set of parentheses.
86
- if (argsDoc.length === 0) {
87
- // If you're using an explicit set of parentheses on something that looks
88
- // like a constant, then we need to match that in order to maintain valid
89
- // Ruby. For example, you could do something like Foo(), on which we would
90
- // need to keep the parentheses to make it look like a method call.
91
- if (methodNode.type === "fcall" && methodNode.body[0].type === "@const") {
92
- return concat([methodDoc, "()"]);
93
- }
94
-
95
- // If you're using an explicit set parentheses with the special call syntax,
96
- // then we need to explicitly print out an extra set of parentheses. For
97
- // example, if you call something like Foo.new.() (implicitly calling the
98
- // #call method on a new instance of the Foo class), then we have to print
99
- // out those parentheses, otherwise we'll end up with Foo.new.
100
- if (methodNode.type === "call" && methodNode.body[2] === "call") {
101
- return concat([methodDoc, "()"]);
102
- }
103
-
104
- return methodDoc;
105
- }
106
-
107
- // This case will ONLY be hit if we can successfully turn the block into a
108
- // to_proc call. In that case, we just explicitly add the parens around it.
109
- if (argNode.type === "args" && argsDoc.length > 0) {
110
- return concat([methodDoc, "("].concat(argsDoc).concat(")"));
111
- }
112
-
113
- // Get a reference to the parent node so we can check if we're inside a chain
114
- const parentNode = path.getParentNode();
115
-
116
- // If our parent node is a chained node then we're not going to group the
117
- // right side of the expression, as we want to have a nice multi-line layout.
118
- if (chained.includes(parentNode.type) && !node.comments) {
119
- parentNode.chain = (node.chain || 0) + 1;
120
- parentNode.breakDoc = (node.breakDoc || [methodDoc]).concat(argsDoc);
121
- parentNode.firstReceiverType = node.firstReceiverType;
122
- }
123
-
124
- // This is the threshold at which we will start to try to make a nicely
125
- // indented call chain. For the most part, it's always 3.
126
- let threshold = 3;
127
-
128
- // Here, we have very specialized behavior where if we're within a sig block,
129
- // then we're going to assume we're creating a Sorbet type signature. In that
130
- // case, we really want the threshold to be lowered to 2 so that we create
131
- // method chains off of any two method calls within the block. For more
132
- // details, see
133
- // https://github.com/prettier/plugin-ruby/issues/863.
134
- let sigBlock = path.getParentNode(2);
135
- if (sigBlock) {
136
- // If we're at a do_block, then we want to go one more level up. This is
137
- // because do_blocks have bodystmt nodes instead of just stmt nodes.
138
- if (sigBlock.type === "do_block") {
139
- sigBlock = path.getParentNode(3);
140
- }
141
-
142
- if (
143
- sigBlock.type === "method_add_block" &&
144
- sigBlock.body[1] &&
145
- sigBlock.body[0].type === "method_add_arg" &&
146
- sigBlock.body[0].body[0].type === "fcall" &&
147
- sigBlock.body[0].body[0].body[0].body === "sig"
148
- ) {
149
- threshold = 2;
150
- }
151
- }
152
-
153
- // If we're at the top of a chain, then we're going to print out a nice
154
- // multi-line layout if this doesn't break into multiple lines.
155
- if (!chained.includes(parentNode.type) && (node.chain || 0) >= threshold) {
156
- // This is pretty specialized behavior. Basically if we're at the top of a
157
- // chain but we've only had method calls without arguments and now we have
158
- // arguments, then we're effectively trying to call a method with arguments
159
- // that is nested under a bunch of stuff. So we group together to first part
160
- // to make it so just the arguments break. This looks like, for example:
161
- //
162
- // config.action_dispatch.rescue_responses.merge!(
163
- // 'ActiveRecord::ConnectionTimeoutError' => :service_unavailable,
164
- // 'ActiveRecord::QueryCanceled' => :service_unavailable
165
- // )
166
- //
167
- if (node.callChain === node.chain) {
168
- return concat([group(indent(concat(node.breakDoc))), group(argsDoc)]);
169
- }
170
-
171
- return ifBreak(
172
- group(indent(concat(node.breakDoc.concat(argsDoc)))),
173
- concat([methodDoc, argsDoc])
174
- );
175
- }
176
-
177
- // If there are already parentheses, then we can just use the doc that's
178
- // already printed.
179
- if (argNode.type == "arg_paren") {
180
- return concat([methodDoc, argsDoc]);
181
- }
182
-
183
- return concat([methodDoc, " ", join(", ", argsDoc), " "]);
184
- }
185
-
186
- function printMethodAddBlock(path, opts, print) {
187
- const node = path.getValue();
188
-
189
- const [callNode, blockNode] = node.body;
190
- const [callDoc, blockDoc] = path.map(print, "body");
191
-
192
- // Don't bother trying to do any kind of fancy toProc transform if the option
193
- // is disabled.
194
- if (opts.rubyToProc) {
195
- const proc = toProc(path, blockNode);
196
-
197
- if (proc && callNode.type === "call") {
198
- return group(
199
- concat([
200
- path.call(print, "body", 0),
201
- "(",
202
- indent(concat([softline, proc])),
203
- concat([softline, ")"])
204
- ])
205
- );
206
- }
207
-
208
- if (proc) {
209
- return path.call(print, "body", 0);
210
- }
211
- }
212
-
213
- // Get a reference to the parent node so we can check if we're inside a chain
214
- const parentNode = path.getParentNode();
215
-
216
- // If our parent node is a chained node then we're not going to group the
217
- // right side of the expression, as we want to have a nice multi-line layout.
218
- if (chained.includes(parentNode.type)) {
219
- parentNode.chain = (node.chain || 0) + 1;
220
- parentNode.breakDoc = (node.breakDoc || [callDoc]).concat(blockDoc);
221
- parentNode.firstReceiverType = node.firstReceiverType;
222
- }
223
-
224
- // If we're at the top of a chain, then we're going to print out a nice
225
- // multi-line layout if this doesn't break into multiple lines.
226
- if (!chained.includes(parentNode.type) && (node.chain || 0) >= 3) {
227
- return ifBreak(
228
- group(indent(concat(node.breakDoc.concat(blockDoc)))),
229
- concat([callDoc, blockDoc])
230
- );
231
- }
232
-
233
- return concat([callDoc, blockDoc]);
234
- }
235
-
236
- function printCallContainer(path, opts, print) {
237
- return path.call(print, "body", 0);
238
- }
239
-
240
- module.exports = {
241
- call: printCall,
242
- fcall: printCallContainer,
243
- method_add_arg: printMethodAddArg,
244
- method_add_block: printMethodAddBlock,
245
- vcall: printCallContainer
246
- };
@@ -1,65 +0,0 @@
1
- const {
2
- align,
3
- concat,
4
- fill,
5
- group,
6
- hardline,
7
- indent,
8
- line
9
- } = require("../../prettier");
10
-
11
- function printCase(path, opts, print) {
12
- const statement = ["case"];
13
-
14
- // You don't need to explicitly have something to test against in a case
15
- // statement (without it it effectively becomes an if/elsif chain).
16
- if (path.getValue().body[0]) {
17
- statement.push(" ", path.call(print, "body", 0));
18
- }
19
-
20
- return concat(
21
- statement.concat([hardline, path.call(print, "body", 1), hardline, "end"])
22
- );
23
- }
24
-
25
- function printWhen(path, opts, print) {
26
- const [_preds, _stmts, addition] = path.getValue().body;
27
-
28
- // The `fill` builder command expects an array of docs alternating with
29
- // line breaks. This is so it can loop through and determine where to break.
30
- const preds = fill(
31
- path.call(print, "body", 0).reduce((accum, pred, index) => {
32
- if (index === 0) {
33
- return [pred];
34
- }
35
-
36
- // Pull off the last element and make it concat with a comma so that
37
- // we can maintain alternating lines and docs.
38
- return accum
39
- .slice(0, -1)
40
- .concat([concat([accum[accum.length - 1], ","]), line, pred]);
41
- }, null)
42
- );
43
-
44
- const stmts = path.call(print, "body", 1);
45
- const parts = [concat(["when ", align("when ".length, preds)])];
46
-
47
- // It's possible in a when to just have empty void statements, in which case
48
- // we would skip adding the body.
49
- if (!stmts.parts.every((part) => !part)) {
50
- parts.push(indent(concat([hardline, stmts])));
51
- }
52
-
53
- // This is the next clause on the case statement, either another `when` or
54
- // an `else` clause.
55
- if (addition) {
56
- parts.push(hardline, path.call(print, "body", 2));
57
- }
58
-
59
- return group(concat(parts));
60
- }
61
-
62
- module.exports = {
63
- case: printCase,
64
- when: printWhen
65
- };
@@ -1,64 +0,0 @@
1
- const { concat, group, hardline, indent } = require("../../prettier");
2
- const { isEmptyBodyStmt } = require("../../utils");
3
-
4
- function printClass(path, opts, print) {
5
- const [_constant, superclass, bodystmt] = path.getValue().body;
6
-
7
- const parts = ["class ", path.call(print, "body", 0)];
8
- if (superclass) {
9
- parts.push(" < ", path.call(print, "body", 1));
10
- }
11
-
12
- const declaration = group(concat(parts));
13
- if (isEmptyBodyStmt(bodystmt)) {
14
- return group(concat([declaration, hardline, "end"]));
15
- }
16
-
17
- return group(
18
- concat([
19
- declaration,
20
- indent(concat([hardline, path.call(print, "body", 2)])),
21
- concat([hardline, "end"])
22
- ])
23
- );
24
- }
25
-
26
- function printModule(path, opts, print) {
27
- const node = path.getValue();
28
- const declaration = group(concat(["module ", path.call(print, "body", 0)]));
29
-
30
- if (isEmptyBodyStmt(node.body[1])) {
31
- return group(concat([declaration, hardline, "end"]));
32
- }
33
-
34
- return group(
35
- concat([
36
- declaration,
37
- indent(concat([hardline, path.call(print, "body", 1)])),
38
- concat([hardline, "end"])
39
- ])
40
- );
41
- }
42
-
43
- function printSClass(path, opts, print) {
44
- const bodystmt = path.getValue().body[1];
45
- const declaration = concat(["class << ", path.call(print, "body", 0)]);
46
-
47
- if (isEmptyBodyStmt(bodystmt)) {
48
- return group(concat([declaration, hardline, "end"]));
49
- }
50
-
51
- return group(
52
- concat([
53
- declaration,
54
- indent(concat([hardline, path.call(print, "body", 1)])),
55
- concat([hardline, "end"])
56
- ])
57
- );
58
- }
59
-
60
- module.exports = {
61
- class: printClass,
62
- module: printModule,
63
- sclass: printSClass
64
- };
@@ -1,131 +0,0 @@
1
- const {
2
- align,
3
- concat,
4
- group,
5
- ifBreak,
6
- indent,
7
- join,
8
- line,
9
- softline
10
- } = require("../../prettier");
11
- const { makeCall } = require("../../utils");
12
-
13
- function docLength(doc) {
14
- if (doc.length) {
15
- return doc.length;
16
- }
17
-
18
- if (doc.parts) {
19
- return doc.parts.reduce((sum, child) => sum + docLength(child), 0);
20
- }
21
-
22
- if (doc.contents) {
23
- return docLength(doc.contents);
24
- }
25
-
26
- return 0;
27
- }
28
-
29
- function hasDef(node) {
30
- return (
31
- node.body[1].type === "args_add_block" &&
32
- node.body[1].body[0].type === "args" &&
33
- node.body[1].body[0].body[0] &&
34
- ["def", "defs"].includes(node.body[1].body[0].body[0].type)
35
- );
36
- }
37
-
38
- // Very special handling case for rspec matchers. In general with rspec matchers
39
- // you expect to see something like:
40
- //
41
- // expect(foo).to receive(:bar).with(
42
- // 'one',
43
- // 'two',
44
- // 'three',
45
- // 'four',
46
- // 'five'
47
- // )
48
- //
49
- // In this case the arguments are aligned to the left side as opposed to being
50
- // aligned with the `receive` call.
51
- function skipArgsAlign(path) {
52
- return ["to", "not_to"].includes(path.getValue().body[2].body);
53
- }
54
-
55
- // If there is a ternary argument to a command and it's going to get broken
56
- // into multiple lines, then we're going to have to use parentheses around the
57
- // command in order to make sure operator precedence doesn't get messed up.
58
- function hasTernaryArg(node) {
59
- return node.body[0].body.some((child) => child.type === "ifop");
60
- }
61
-
62
- function printCommand(path, opts, print) {
63
- const node = path.getValue();
64
-
65
- const command = path.call(print, "body", 0);
66
- const joinedArgs = join(concat([",", line]), path.call(print, "body", 1));
67
-
68
- const hasTernary = hasTernaryArg(node.body[1]);
69
- let breakArgs;
70
-
71
- if (hasTernary) {
72
- breakArgs = indent(concat([softline, joinedArgs]));
73
- } else if (hasDef(path.getValue())) {
74
- breakArgs = joinedArgs;
75
- } else {
76
- breakArgs = align(docLength(command) + 1, joinedArgs);
77
- }
78
-
79
- return group(
80
- ifBreak(
81
- concat([
82
- command,
83
- hasTernary ? "(" : " ",
84
- breakArgs,
85
- hasTernary ? concat([softline, ")"]) : ""
86
- ]),
87
- concat([command, " ", joinedArgs])
88
- )
89
- );
90
- }
91
-
92
- function printCommandCall(path, opts, print) {
93
- const node = path.getValue();
94
- const parts = [
95
- path.call(print, "body", 0),
96
- makeCall(path, opts, print),
97
- path.call(print, "body", 2)
98
- ];
99
-
100
- if (!node.body[3]) {
101
- return concat(parts);
102
- }
103
-
104
- const argDocs = join(concat([",", line]), path.call(print, "body", 3));
105
- let breakDoc;
106
-
107
- if (hasTernaryArg(node.body[3])) {
108
- breakDoc = parts.concat(
109
- "(",
110
- indent(concat([softline, argDocs])),
111
- softline,
112
- ")"
113
- );
114
- parts.push(" ");
115
- } else if (skipArgsAlign(path)) {
116
- parts.push(" ");
117
- breakDoc = parts.concat(argDocs);
118
- } else {
119
- parts.push(" ");
120
- breakDoc = parts.concat(align(docLength(concat(parts)), argDocs));
121
- }
122
-
123
- const joinedDoc = parts.concat(argDocs);
124
-
125
- return group(ifBreak(concat(breakDoc), concat(joinedDoc)));
126
- }
127
-
128
- module.exports = {
129
- command: printCommand,
130
- command_call: printCommandCall
131
- };