Skip to content

Commit 9d2b356

Browse files
committed
Long method chains with args at the end
IF you have something like ```ruby config.action_dispatch.rescue_responses.merge!( # We created more threads than RAILS_MAX_THREADS 'ActiveRecord::ConnectionTimeoutError' => :service_unavailable, # Some query hits a timeout 'ActiveRecord::QueryCanceled' => :service_unavailable, # Rack::Timeout 'Rack::Timeout::RequestExpiryError' => :service_unavailable, 'Rack::Timeout::RequestTimeoutError' => :service_unavailable, 'Rack::Timeout::RequestTimeoutException' => :service_unavailable, ) ``` then we want to group the args and the method chain.
1 parent e2614ed commit 9d2b356

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1616
- [@rindek], [@kddeisz] - Do not remove parentheses when using the special `call` syntax with no arguments.
1717
- [@ykpythemind] - Do not change regexp bounds if the body has certain content.
1818
- [@karanmandal], [@kddeisz] - Correctly print for loops.
19+
- [@rafbm], [@kddeisz]
1920

2021
## [1.0.1] - 2020-12-12
2122

@@ -1071,6 +1072,7 @@ would previously result in `array[]`, but now prints properly.
10711072
[@overload119]: https://github.com/Overload119
10721073
[@petevk]: https://github.com/petevk
10731074
[@pje]: https://github.com/pje
1075+
[@rafbm]: https://github.com/rafbm
10741076
[@rindek]: https://github.com/rindek
10751077
[@rosskinsella]: https://github.com/RossKinsella
10761078
[@rsullivan00]: https://github.com/Rsullivan00

src/nodes/calls.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function printCall(path, opts, print) {
3939
// right side of the expression, as we want to have a nice multi-line layout.
4040
if (chained.includes(parentNode.type)) {
4141
parentNode.chain = (node.chain || 0) + 1;
42+
parentNode.callChain = (node.callChain || 0) + 1;
4243
parentNode.breakDoc = (node.breakDoc || [receiverDoc]).concat(rightSideDoc);
4344
}
4445

@@ -109,6 +110,21 @@ function printMethodAddArg(path, opts, print) {
109110
// If we're at the top of a chain, then we're going to print out a nice
110111
// multi-line layout if this doesn't break into multiple lines.
111112
if (!chained.includes(parentNode.type) && (node.chain || 0) >= 3) {
113+
// This is pretty specialized behavior. Basically if we're at the top of a
114+
// chain but we've only had method calls without arguments and now we have
115+
// arguments, then we're effectively trying to call a method with arguments
116+
// that is nested under a bunch of stuff. So we group together to first part
117+
// to make it so just the arguments break. This looks like, for example:
118+
//
119+
// config.action_dispatch.rescue_responses.merge!(
120+
// 'ActiveRecord::ConnectionTimeoutError' => :service_unavailable,
121+
// 'ActiveRecord::QueryCanceled' => :service_unavailable
122+
// )
123+
//
124+
if (node.callChain === node.chain) {
125+
return concat([group(indent(concat(node.breakDoc))), group(argsDoc)]);
126+
}
127+
112128
return ifBreak(
113129
group(indent(concat(node.breakDoc.concat(argsDoc)))),
114130
concat([methodDoc, argsDoc])

test/js/nodes/calls.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { ruby } = require("../utils");
1+
const { long, ruby } = require("../utils");
22

33
describe("calls", () => {
44
test("simple calls", () => {
@@ -26,6 +26,17 @@ describe("calls", () => {
2626
return expect(before).toChangeFormat(after);
2727
});
2828

29+
test("chains of methods with one with arguments right at the top", () => {
30+
const content = ruby(`
31+
aaa.bbb.ccc.ddd.eee.merge(
32+
${long.slice(0, 30)}: 'aaa',
33+
${long.slice(0, 31)}: 'bbb'
34+
)
35+
`);
36+
37+
return expect(content).toMatchFormat();
38+
});
39+
2940
test("tons of calls that fit on one line", () => {
3041
const content = "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z";
3142

0 commit comments

Comments
 (0)