prettier 2.1.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -6
- data/README.md +16 -16
- data/exe/rbprettier +2 -2
- data/lib/prettier/rake/task.rb +5 -5
- data/lib/prettier.rb +11 -11
- data/package.json +9 -23
- data/rubocop.yml +6 -6
- data/{dist/parser → src}/getInfo.js +0 -1
- data/{dist/parser → src}/netcat.js +0 -1
- data/src/parseSync.js +212 -0
- data/src/plugin.js +161 -0
- data/{dist/parser → src}/server.rb +45 -31
- metadata +94 -78
- data/bin/console +0 -7
- data/dist/haml/embed.js +0 -53
- data/dist/haml/parser.js +0 -31
- data/dist/haml/parser.rb +0 -149
- data/dist/haml/printer.js +0 -336
- data/dist/parser/parseSync.js +0 -179
- data/dist/plugin.js +0 -143
- data/dist/prettier.js +0 -15
- data/dist/rbs/parser.js +0 -34
- data/dist/rbs/parser.rb +0 -155
- data/dist/rbs/printer.js +0 -525
- data/dist/ruby/embed.js +0 -115
- data/dist/ruby/location.js +0 -19
- data/dist/ruby/nodes/alias.js +0 -60
- data/dist/ruby/nodes/aref.js +0 -51
- data/dist/ruby/nodes/args.js +0 -138
- data/dist/ruby/nodes/arrays.js +0 -122
- data/dist/ruby/nodes/assign.js +0 -37
- data/dist/ruby/nodes/blocks.js +0 -90
- data/dist/ruby/nodes/calls.js +0 -263
- data/dist/ruby/nodes/case.js +0 -50
- data/dist/ruby/nodes/class.js +0 -54
- data/dist/ruby/nodes/commands.js +0 -138
- data/dist/ruby/nodes/conditionals.js +0 -246
- data/dist/ruby/nodes/constants.js +0 -35
- data/dist/ruby/nodes/flow.js +0 -59
- data/dist/ruby/nodes/hashes.js +0 -126
- data/dist/ruby/nodes/heredocs.js +0 -30
- data/dist/ruby/nodes/hooks.js +0 -35
- data/dist/ruby/nodes/ints.js +0 -27
- data/dist/ruby/nodes/lambdas.js +0 -70
- data/dist/ruby/nodes/loops.js +0 -75
- data/dist/ruby/nodes/massign.js +0 -60
- data/dist/ruby/nodes/methods.js +0 -50
- data/dist/ruby/nodes/operators.js +0 -68
- data/dist/ruby/nodes/params.js +0 -95
- data/dist/ruby/nodes/patterns.js +0 -119
- data/dist/ruby/nodes/regexp.js +0 -45
- data/dist/ruby/nodes/rescue.js +0 -86
- data/dist/ruby/nodes/return.js +0 -100
- data/dist/ruby/nodes/statements.js +0 -110
- data/dist/ruby/nodes/strings.js +0 -220
- data/dist/ruby/nodes/super.js +0 -26
- data/dist/ruby/nodes/undef.js +0 -31
- data/dist/ruby/nodes.js +0 -177
- data/dist/ruby/parser.js +0 -35
- data/dist/ruby/parser.rb +0 -9134
- data/dist/ruby/printer.js +0 -67
- data/dist/ruby/toProc.js +0 -91
- data/dist/types/haml.js +0 -4
- data/dist/types/plugin.js +0 -3
- data/dist/types/rbs.js +0 -4
- data/dist/types/ruby.js +0 -4
- data/dist/types/utils.js +0 -2
- data/dist/types.js +0 -34
- data/dist/utils/containsAssignment.js +0 -18
- data/dist/utils/getChildNodes.js +0 -305
- data/dist/utils/getTrailingComma.js +0 -6
- data/dist/utils/hasAncestor.js +0 -15
- data/dist/utils/inlineEnsureParens.js +0 -49
- data/dist/utils/isEmptyBodyStmt.js +0 -10
- data/dist/utils/isEmptyParams.js +0 -12
- data/dist/utils/isEmptyStmts.js +0 -10
- data/dist/utils/literal.js +0 -8
- data/dist/utils/literallineWithoutBreakParent.js +0 -8
- data/dist/utils/makeCall.js +0 -14
- data/dist/utils/noIndent.js +0 -11
- data/dist/utils/printEmptyCollection.js +0 -46
- data/dist/utils/skipAssignIndent.js +0 -19
- data/dist/utils.js +0 -32
data/dist/ruby/nodes/calls.js
DELETED
@@ -1,263 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.printCallContainer = exports.printMethodAddBlock = exports.printMethodAddArg = exports.printCall = void 0;
|
7
|
-
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
-
const utils_1 = require("../../utils");
|
9
|
-
const toProc_1 = __importDefault(require("../toProc"));
|
10
|
-
const { group, hardline, ifBreak, indent, join, softline } = prettier_1.default;
|
11
|
-
const chained = ["call", "method_add_arg", "method_add_block"];
|
12
|
-
function hasLeadingComments(node) {
|
13
|
-
var _a;
|
14
|
-
return (_a = node.comments) === null || _a === void 0 ? void 0 : _a.some(({ leading }) => leading);
|
15
|
-
}
|
16
|
-
const printCall = (path, opts, print) => {
|
17
|
-
const node = path.getValue();
|
18
|
-
const receiverDoc = path.call(print, "receiver");
|
19
|
-
const operatorDoc = (0, utils_1.makeCall)(path, opts, print);
|
20
|
-
// You can call lambdas with a special syntax that looks like func.(*args).
|
21
|
-
// In this case, "call" is returned for the 3rd child node. We don't alter
|
22
|
-
// call syntax so if `call` is implicit, we don't print it out.
|
23
|
-
const messageDoc = node.message === "call" ? "" : path.call(print, "message");
|
24
|
-
// Create some arrays that are going to represent each side of our call.
|
25
|
-
let leftSideDoc = [receiverDoc];
|
26
|
-
let rightSideDoc = [operatorDoc, messageDoc];
|
27
|
-
// If there are leading comments on the right side of the call, then it means
|
28
|
-
// we have a structure where there's a receiver and an operator together, then
|
29
|
-
// a comment, then the message, as in:
|
30
|
-
//
|
31
|
-
// foo.
|
32
|
-
// # comment
|
33
|
-
// baz
|
34
|
-
//
|
35
|
-
// In the case we need to group the receiver and the operator together or
|
36
|
-
// we'll end up with a syntax error.
|
37
|
-
const operatorIsTrailing = node.message !== "call" && hasLeadingComments(node.message);
|
38
|
-
if (operatorIsTrailing) {
|
39
|
-
leftSideDoc = [receiverDoc, operatorDoc];
|
40
|
-
rightSideDoc = [messageDoc];
|
41
|
-
}
|
42
|
-
// For certain left sides of the call nodes, we want to attach directly to
|
43
|
-
// the } or end.
|
44
|
-
if (utils_1.noIndent.includes(node.receiver.type)) {
|
45
|
-
return [leftSideDoc, rightSideDoc];
|
46
|
-
}
|
47
|
-
if (node.receiver.type === "call" &&
|
48
|
-
node.receiver.message !== "call" &&
|
49
|
-
node.receiver.message.value === "where" &&
|
50
|
-
messageDoc === "not") {
|
51
|
-
// This is very specialized behavior wherein we group .where.not calls
|
52
|
-
// together because it looks better. For more information, see
|
53
|
-
// https://github.com/prettier/plugin-ruby/issues/862.
|
54
|
-
}
|
55
|
-
else {
|
56
|
-
// Otherwise, we're going to put a line node into the right side doc.
|
57
|
-
rightSideDoc.unshift(node.receiver.comments ? hardline : softline);
|
58
|
-
}
|
59
|
-
// Get a reference to the parent node so we can check if we're inside a chain
|
60
|
-
const parentNode = path.getParentNode();
|
61
|
-
// If our parent node is a chained node then we're not going to group the
|
62
|
-
// right side of the expression, as we want to have a nice multi-line layout.
|
63
|
-
if (chained.includes(parentNode.type) && !node.comments) {
|
64
|
-
parentNode.chain = (node.chain || 0) + 1;
|
65
|
-
parentNode.callChain = (node.callChain || 0) + 1;
|
66
|
-
parentNode.firstReceiverType = node.firstReceiverType || node.receiver.type;
|
67
|
-
// Here we're going to determine what doc nodes to send up to the parent
|
68
|
-
// node to represent when we're in the multi-line form.
|
69
|
-
let breakDocLHS;
|
70
|
-
if (node.breakDoc && operatorIsTrailing) {
|
71
|
-
// Here we already have a child node that has passed up its
|
72
|
-
// representation. In this case node.breakDoc represents the receiver
|
73
|
-
// without any lines inserted. With regard to this node, it means it's
|
74
|
-
// everything up until the operator. So we're just going to append the
|
75
|
-
// operator.
|
76
|
-
breakDocLHS = node.breakDoc.concat(operatorDoc);
|
77
|
-
}
|
78
|
-
else if (node.breakDoc) {
|
79
|
-
// Here we don't need a trailing operator, so we're just going to use the
|
80
|
-
// existing node.breakDoc. The operator will be a part of the rightSideDoc
|
81
|
-
// variable.
|
82
|
-
breakDocLHS = node.breakDoc;
|
83
|
-
}
|
84
|
-
else {
|
85
|
-
// Here we're at the bottom of the chain, so there's no representation yet
|
86
|
-
// for the receiver. So we're just going to pass up the left side.
|
87
|
-
breakDocLHS = leftSideDoc;
|
88
|
-
}
|
89
|
-
parentNode.breakDoc = breakDocLHS.concat(rightSideDoc);
|
90
|
-
}
|
91
|
-
// If we're at the top of a chain, then we're going to print out a nice
|
92
|
-
// multi-line layout if this doesn't break into multiple lines.
|
93
|
-
if (!chained.includes(parentNode.type) &&
|
94
|
-
(node.chain || 0) >= 3 &&
|
95
|
-
node.breakDoc) {
|
96
|
-
return ifBreak(group(indent(node.breakDoc.concat(rightSideDoc))), [
|
97
|
-
leftSideDoc,
|
98
|
-
group(rightSideDoc)
|
99
|
-
]);
|
100
|
-
}
|
101
|
-
return group([leftSideDoc, group(indent(rightSideDoc))]);
|
102
|
-
};
|
103
|
-
exports.printCall = printCall;
|
104
|
-
const printMethodAddArg = (path, opts, print) => {
|
105
|
-
const node = path.getValue();
|
106
|
-
const methodDoc = path.call(print, "call");
|
107
|
-
const argsDoc = path.call(print, "args");
|
108
|
-
// You can end up here if you have a method with a ? ending, presumably
|
109
|
-
// because the parser knows that it cannot be a local variable. You can also
|
110
|
-
// end up here if you are explicitly using an empty set of parentheses.
|
111
|
-
if (argsDoc.length === 0) {
|
112
|
-
// If you're using an explicit set of parentheses on something that looks
|
113
|
-
// like a constant, then we need to match that in order to maintain valid
|
114
|
-
// Ruby. For example, you could do something like Foo(), on which we would
|
115
|
-
// need to keep the parentheses to make it look like a method call.
|
116
|
-
if (node.call.type === "fcall" && node.call.value.type === "const") {
|
117
|
-
return [methodDoc, "()"];
|
118
|
-
}
|
119
|
-
// If you're using an explicit set parentheses with the special call syntax,
|
120
|
-
// then we need to explicitly print out an extra set of parentheses. For
|
121
|
-
// example, if you call something like Foo.new.() (implicitly calling the
|
122
|
-
// #call method on a new instance of the Foo class), then we have to print
|
123
|
-
// out those parentheses, otherwise we'll end up with Foo.new.
|
124
|
-
if (node.call.type === "call" && node.call.message === "call") {
|
125
|
-
return [methodDoc, "()"];
|
126
|
-
}
|
127
|
-
return methodDoc;
|
128
|
-
}
|
129
|
-
// This case will ONLY be hit if we can successfully turn the block into a
|
130
|
-
// to_proc call. In that case, we just explicitly add the parens around it.
|
131
|
-
if (node.args.type === "args" && argsDoc.length > 0) {
|
132
|
-
return [methodDoc, "(", ...argsDoc, ")"];
|
133
|
-
}
|
134
|
-
// Get a reference to the parent node so we can check if we're inside a chain
|
135
|
-
const parentNode = path.getParentNode();
|
136
|
-
// If our parent node is a chained node then we're not going to group the
|
137
|
-
// right side of the expression, as we want to have a nice multi-line layout.
|
138
|
-
if (chained.includes(parentNode.type) && !node.comments) {
|
139
|
-
parentNode.chain = (node.chain || 0) + 1;
|
140
|
-
parentNode.breakDoc = (node.breakDoc || [methodDoc]).concat(argsDoc);
|
141
|
-
parentNode.firstReceiverType = node.firstReceiverType;
|
142
|
-
}
|
143
|
-
// This is the threshold at which we will start to try to make a nicely
|
144
|
-
// indented call chain. For the most part, it's always 3.
|
145
|
-
let threshold = 3;
|
146
|
-
// Here, we have very specialized behavior where if we're within a sig block,
|
147
|
-
// then we're going to assume we're creating a Sorbet type signature. In that
|
148
|
-
// case, we really want the threshold to be lowered to 2 so that we create
|
149
|
-
// method chains off of any two method calls within the block. For more
|
150
|
-
// details, see
|
151
|
-
// https://github.com/prettier/plugin-ruby/issues/863.
|
152
|
-
let sigBlock = path.getParentNode(2);
|
153
|
-
if (sigBlock) {
|
154
|
-
// If we're at a do_block, then we want to go one more level up. This is
|
155
|
-
// because do_blocks have bodystmt nodes instead of just stmt nodes.
|
156
|
-
if (sigBlock.type === "do_block") {
|
157
|
-
sigBlock = path.getParentNode(3);
|
158
|
-
}
|
159
|
-
if (sigBlock.type === "method_add_block") {
|
160
|
-
// Pulling this out into a separate variable so we can get back some of
|
161
|
-
// our type safety.
|
162
|
-
const sigBlockNode = sigBlock;
|
163
|
-
if (sigBlockNode.block &&
|
164
|
-
sigBlockNode.call.type === "method_add_arg" &&
|
165
|
-
sigBlockNode.call.call.type === "fcall" &&
|
166
|
-
sigBlockNode.call.call.value.value === "sig") {
|
167
|
-
threshold = 2;
|
168
|
-
}
|
169
|
-
}
|
170
|
-
}
|
171
|
-
// If we're at the top of a chain, then we're going to print out a nice
|
172
|
-
// multi-line layout if this doesn't break into multiple lines.
|
173
|
-
if (!chained.includes(parentNode.type) &&
|
174
|
-
(node.chain || 0) >= threshold &&
|
175
|
-
node.breakDoc) {
|
176
|
-
// This is pretty specialized behavior. Basically if we're at the top of a
|
177
|
-
// chain but we've only had method calls without arguments and now we have
|
178
|
-
// arguments, then we're effectively trying to call a method with arguments
|
179
|
-
// that is nested under a bunch of stuff. So we group together to first part
|
180
|
-
// to make it so just the arguments break. This looks like, for example:
|
181
|
-
//
|
182
|
-
// config.action_dispatch.rescue_responses.merge!(
|
183
|
-
// 'ActiveRecord::ConnectionTimeoutError' => :service_unavailable,
|
184
|
-
// 'ActiveRecord::QueryCanceled' => :service_unavailable
|
185
|
-
// )
|
186
|
-
//
|
187
|
-
if (node.callChain === node.chain) {
|
188
|
-
return [group(indent(node.breakDoc)), group(argsDoc)];
|
189
|
-
}
|
190
|
-
return ifBreak(group(indent(node.breakDoc.concat(argsDoc))), [
|
191
|
-
methodDoc,
|
192
|
-
argsDoc
|
193
|
-
]);
|
194
|
-
}
|
195
|
-
// If there are already parentheses, then we can just use the doc that's
|
196
|
-
// already printed.
|
197
|
-
if (node.args.type == "arg_paren") {
|
198
|
-
return group([methodDoc, argsDoc]);
|
199
|
-
}
|
200
|
-
return [methodDoc, " ", join(", ", argsDoc), " "];
|
201
|
-
};
|
202
|
-
exports.printMethodAddArg = printMethodAddArg;
|
203
|
-
const printMethodAddBlock = (path, opts, print) => {
|
204
|
-
const node = path.getValue();
|
205
|
-
const callDoc = path.call(print, "call");
|
206
|
-
const blockDoc = path.call(print, "block");
|
207
|
-
// Don't bother trying to do any kind of fancy toProc transform if the option
|
208
|
-
// is disabled.
|
209
|
-
if (opts.rubyToProc) {
|
210
|
-
const proc = (0, toProc_1.default)(path, node.block);
|
211
|
-
if (proc && node.call.type === "call") {
|
212
|
-
return group([
|
213
|
-
path.call(print, "call"),
|
214
|
-
"(",
|
215
|
-
indent([softline, proc]),
|
216
|
-
[softline, ")"]
|
217
|
-
]);
|
218
|
-
}
|
219
|
-
if (proc) {
|
220
|
-
return path.call(print, "call");
|
221
|
-
}
|
222
|
-
}
|
223
|
-
// Get a reference to the parent node so we can check if we're inside a chain
|
224
|
-
const parentNode = path.getParentNode();
|
225
|
-
// If our parent node is a chained node then we're not going to group the
|
226
|
-
// right side of the expression, as we want to have a nice multi-line layout.
|
227
|
-
if (chained.includes(parentNode.type)) {
|
228
|
-
parentNode.chain = (node.chain || 0) + 1;
|
229
|
-
parentNode.breakDoc = (node.breakDoc || [callDoc]).concat(blockDoc);
|
230
|
-
parentNode.firstReceiverType = node.firstReceiverType;
|
231
|
-
}
|
232
|
-
// If we're at the top of a chain, then we're going to print out a nice
|
233
|
-
// multi-line layout if this doesn't break into multiple lines.
|
234
|
-
if (!chained.includes(parentNode.type) &&
|
235
|
-
(node.chain || 0) >= 3 &&
|
236
|
-
node.breakDoc) {
|
237
|
-
// This is pretty specialized behavior. Basically if we're at the top of a
|
238
|
-
// chain but we've only had method calls without arguments and now we have
|
239
|
-
// a method call with a block, then we're effectively trying to call a
|
240
|
-
// method with arguments that is nested under a bunch of stuff. So we group
|
241
|
-
// together to first part to make it so just the block breaks. This looks
|
242
|
-
// like, for example:
|
243
|
-
//
|
244
|
-
// Rails.application.routes.draw do
|
245
|
-
// root 'articles#index'
|
246
|
-
// resources :articles
|
247
|
-
// end
|
248
|
-
//
|
249
|
-
if (node.callChain === node.chain) {
|
250
|
-
return [group(indent(node.breakDoc)), group(blockDoc)];
|
251
|
-
}
|
252
|
-
return ifBreak(group(indent(node.breakDoc.concat(blockDoc))), [
|
253
|
-
callDoc,
|
254
|
-
blockDoc
|
255
|
-
]);
|
256
|
-
}
|
257
|
-
return [callDoc, blockDoc];
|
258
|
-
};
|
259
|
-
exports.printMethodAddBlock = printMethodAddBlock;
|
260
|
-
const printCallContainer = (path, opts, print) => {
|
261
|
-
return path.call(print, "value");
|
262
|
-
};
|
263
|
-
exports.printCallContainer = printCallContainer;
|
data/dist/ruby/nodes/case.js
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.printWhen = exports.printCase = void 0;
|
7
|
-
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
-
const { align, fill, group, hardline, indent, line } = prettier_1.default;
|
9
|
-
const printCase = (path, opts, print) => {
|
10
|
-
const parts = ["case"];
|
11
|
-
// You don't need to explicitly have something to test against in a case
|
12
|
-
// statement (without it it effectively becomes an if/elsif chain).
|
13
|
-
if (path.getValue().value) {
|
14
|
-
parts.push(" ", path.call(print, "value"));
|
15
|
-
}
|
16
|
-
return [...parts, hardline, path.call(print, "cons"), hardline, "end"];
|
17
|
-
};
|
18
|
-
exports.printCase = printCase;
|
19
|
-
const printWhen = (path, opts, print) => {
|
20
|
-
const node = path.getValue();
|
21
|
-
// The `fill` builder command expects an array of docs alternating with
|
22
|
-
// line breaks. This is so it can loop through and determine where to break.
|
23
|
-
const preds = fill(path.call(print, "args").reduce((accum, pred, index) => {
|
24
|
-
if (index === 0) {
|
25
|
-
return [pred];
|
26
|
-
}
|
27
|
-
// Pull off the last element and make it concat with a comma so that
|
28
|
-
// we can maintain alternating lines and docs.
|
29
|
-
return [
|
30
|
-
...accum.slice(0, -1),
|
31
|
-
[accum[accum.length - 1], ","],
|
32
|
-
line,
|
33
|
-
pred
|
34
|
-
];
|
35
|
-
}, []));
|
36
|
-
const stmts = path.call(print, "stmts");
|
37
|
-
const parts = [["when ", align("when ".length, preds)]];
|
38
|
-
// It's possible in a when to just have empty void statements, in which case
|
39
|
-
// we would skip adding the body.
|
40
|
-
if (!stmts.every((part) => !part)) {
|
41
|
-
parts.push(indent([hardline, stmts]));
|
42
|
-
}
|
43
|
-
// This is the next clause on the case statement, either another `when` or
|
44
|
-
// an `else` clause.
|
45
|
-
if (node.cons) {
|
46
|
-
parts.push(hardline, path.call(print, "cons"));
|
47
|
-
}
|
48
|
-
return group(parts);
|
49
|
-
};
|
50
|
-
exports.printWhen = printWhen;
|
data/dist/ruby/nodes/class.js
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.printSClass = exports.printModule = exports.printClass = void 0;
|
7
|
-
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
-
const utils_1 = require("../../utils");
|
9
|
-
const { group, hardline, indent } = prettier_1.default;
|
10
|
-
const printClass = (path, opts, print) => {
|
11
|
-
const node = path.getValue();
|
12
|
-
const parts = ["class ", path.call(print, "constant")];
|
13
|
-
if (node.superclass) {
|
14
|
-
parts.push(" < ", path.call(print, "superclass"));
|
15
|
-
}
|
16
|
-
const declaration = group(parts);
|
17
|
-
if ((0, utils_1.isEmptyBodyStmt)(node.bodystmt)) {
|
18
|
-
return group([declaration, hardline, "end"]);
|
19
|
-
}
|
20
|
-
return group([
|
21
|
-
declaration,
|
22
|
-
indent([hardline, path.call(print, "bodystmt")]),
|
23
|
-
[hardline, "end"]
|
24
|
-
]);
|
25
|
-
};
|
26
|
-
exports.printClass = printClass;
|
27
|
-
const printModule = (path, opts, print) => {
|
28
|
-
const node = path.getValue();
|
29
|
-
const declaration = group(["module ", path.call(print, "constant")]);
|
30
|
-
if ((0, utils_1.isEmptyBodyStmt)(node.bodystmt)) {
|
31
|
-
return group([declaration, hardline, "end"]);
|
32
|
-
}
|
33
|
-
return group([
|
34
|
-
declaration,
|
35
|
-
indent([hardline, path.call(print, "bodystmt")]),
|
36
|
-
hardline,
|
37
|
-
"end"
|
38
|
-
]);
|
39
|
-
};
|
40
|
-
exports.printModule = printModule;
|
41
|
-
const printSClass = (path, opts, print) => {
|
42
|
-
const node = path.getValue();
|
43
|
-
const declaration = ["class << ", path.call(print, "target")];
|
44
|
-
if ((0, utils_1.isEmptyBodyStmt)(node.bodystmt)) {
|
45
|
-
return group([declaration, hardline, "end"]);
|
46
|
-
}
|
47
|
-
return group([
|
48
|
-
declaration,
|
49
|
-
indent([hardline, path.call(print, "bodystmt")]),
|
50
|
-
hardline,
|
51
|
-
"end"
|
52
|
-
]);
|
53
|
-
};
|
54
|
-
exports.printSClass = printSClass;
|
data/dist/ruby/nodes/commands.js
DELETED
@@ -1,138 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.printCommandCall = exports.printCommand = void 0;
|
7
|
-
const prettier_1 = __importDefault(require("../../prettier"));
|
8
|
-
const utils_1 = require("../../utils");
|
9
|
-
const { align, group, ifBreak, indent, join, line, softline } = prettier_1.default;
|
10
|
-
function throwBadDoc(doc) {
|
11
|
-
throw new Error(`Unknown doc ${doc}`);
|
12
|
-
}
|
13
|
-
function reduceDocLength(sum, doc) {
|
14
|
-
// If we've hit a line, then we're going to start the counting back at 0 since
|
15
|
-
// it will be starting from the beginning of a line.
|
16
|
-
if (typeof doc === "object" && !Array.isArray(doc) && doc.type === "line") {
|
17
|
-
return 0;
|
18
|
-
}
|
19
|
-
return sum + docBreakLength(doc);
|
20
|
-
}
|
21
|
-
// Loop through the already created doc nodes and determine the overall length
|
22
|
-
// so that we can properly align the command arguments.
|
23
|
-
function docBreakLength(doc) {
|
24
|
-
if (Array.isArray(doc)) {
|
25
|
-
return doc.reduce(reduceDocLength, 0);
|
26
|
-
}
|
27
|
-
if (typeof doc === "string") {
|
28
|
-
return doc.length;
|
29
|
-
}
|
30
|
-
switch (doc.type) {
|
31
|
-
case "concat":
|
32
|
-
case "fill":
|
33
|
-
return doc.parts.reduce(reduceDocLength, 0);
|
34
|
-
case "align":
|
35
|
-
case "group":
|
36
|
-
case "indent":
|
37
|
-
case "line-suffix":
|
38
|
-
return docBreakLength(doc.contents);
|
39
|
-
case "if-break":
|
40
|
-
return docBreakLength(doc.breakContents);
|
41
|
-
case "line":
|
42
|
-
return 0;
|
43
|
-
case "break-parent":
|
44
|
-
case "cursor":
|
45
|
-
case "indent-if-break":
|
46
|
-
case "label":
|
47
|
-
case "line-suffix-boundary":
|
48
|
-
case "trim":
|
49
|
-
return 0;
|
50
|
-
default:
|
51
|
-
throwBadDoc(doc);
|
52
|
-
}
|
53
|
-
}
|
54
|
-
function hasDef(node) {
|
55
|
-
return (node.args.type === "args_add_block" &&
|
56
|
-
node.args.args.type === "args" &&
|
57
|
-
node.args.args.parts[0] &&
|
58
|
-
["def", "defs"].includes(node.args.args.parts[0].type));
|
59
|
-
}
|
60
|
-
// Very special handling case for rspec matchers. In general with rspec matchers
|
61
|
-
// you expect to see something like:
|
62
|
-
//
|
63
|
-
// expect(foo).to receive(:bar).with(
|
64
|
-
// 'one',
|
65
|
-
// 'two',
|
66
|
-
// 'three',
|
67
|
-
// 'four',
|
68
|
-
// 'five'
|
69
|
-
// )
|
70
|
-
//
|
71
|
-
// In this case the arguments are aligned to the left side as opposed to being
|
72
|
-
// aligned with the `receive` call.
|
73
|
-
function skipArgsAlign(node) {
|
74
|
-
return ["to", "not_to", "to_not"].includes(node.message.value);
|
75
|
-
}
|
76
|
-
// If there is a ternary argument to a command and it's going to get broken
|
77
|
-
// into multiple lines, then we're going to have to use parentheses around the
|
78
|
-
// command in order to make sure operator precedence doesn't get messed up.
|
79
|
-
function hasTernaryArg(node) {
|
80
|
-
var _a;
|
81
|
-
switch (node.type) {
|
82
|
-
case "args":
|
83
|
-
return node.parts.some((child) => child.type === "ifop");
|
84
|
-
case "args_add_block":
|
85
|
-
return hasTernaryArg(node.args) || ((_a = node.block) === null || _a === void 0 ? void 0 : _a.type) === "ifop";
|
86
|
-
}
|
87
|
-
}
|
88
|
-
const printCommand = (path, opts, print) => {
|
89
|
-
const node = path.getValue();
|
90
|
-
const command = path.call(print, "message");
|
91
|
-
const joinedArgs = join([",", line], path.call(print, "args"));
|
92
|
-
const hasTernary = hasTernaryArg(node.args);
|
93
|
-
let breakArgs;
|
94
|
-
if (hasTernary) {
|
95
|
-
breakArgs = indent([softline, joinedArgs]);
|
96
|
-
}
|
97
|
-
else if (hasDef(node)) {
|
98
|
-
breakArgs = joinedArgs;
|
99
|
-
}
|
100
|
-
else {
|
101
|
-
breakArgs = align(docBreakLength(command) + 1, joinedArgs);
|
102
|
-
}
|
103
|
-
return group(ifBreak([
|
104
|
-
command,
|
105
|
-
hasTernary ? "(" : " ",
|
106
|
-
breakArgs,
|
107
|
-
hasTernary ? [softline, ")"] : ""
|
108
|
-
], [command, " ", joinedArgs]));
|
109
|
-
};
|
110
|
-
exports.printCommand = printCommand;
|
111
|
-
const printCommandCall = (path, opts, print) => {
|
112
|
-
const node = path.getValue();
|
113
|
-
const parts = [
|
114
|
-
path.call(print, "receiver"),
|
115
|
-
(0, utils_1.makeCall)(path, opts, print),
|
116
|
-
path.call(print, "message")
|
117
|
-
];
|
118
|
-
if (!node.args) {
|
119
|
-
return parts;
|
120
|
-
}
|
121
|
-
const argDocs = join([",", line], path.call(print, "args"));
|
122
|
-
let breakDoc;
|
123
|
-
if (hasTernaryArg(node.args)) {
|
124
|
-
breakDoc = parts.concat("(", indent([softline, argDocs]), softline, ")");
|
125
|
-
parts.push(" ");
|
126
|
-
}
|
127
|
-
else if (skipArgsAlign(node)) {
|
128
|
-
parts.push(" ");
|
129
|
-
breakDoc = parts.concat(argDocs);
|
130
|
-
}
|
131
|
-
else {
|
132
|
-
parts.push(" ");
|
133
|
-
breakDoc = parts.concat(align(docBreakLength(parts), argDocs));
|
134
|
-
}
|
135
|
-
const joinedDoc = parts.concat(argDocs);
|
136
|
-
return group(ifBreak(breakDoc, joinedDoc));
|
137
|
-
};
|
138
|
-
exports.printCommandCall = printCommandCall;
|