Skip to content

Commit 05e99b3

Browse files
karptoniteczosel
andauthored
feat: update chain breaking heuristic (#1437)
* WIP update chain breaking heuristic * fix: only consider expression statements, fix tests Co-authored-by: Christian Zosel <christian@zosel.ch>
1 parent b0fd56d commit 05e99b3

5 files changed

Lines changed: 63 additions & 18 deletions

File tree

src/printer.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ function printMemberChain(path, options, print) {
395395
// $foo->Data['key']("foo")
396396
// ->method();
397397
//
398+
// 3. expression statements with variable names shorter than the tab width
399+
//
400+
// Example:
401+
// $foo->bar()
402+
// ->baz()
403+
// ->buzz()
398404

399405
function shouldNotWrap(groups) {
400406
const hasComputed =
@@ -404,11 +410,17 @@ function printMemberChain(path, options, print) {
404410
const firstNode = groups[0][0].node;
405411

406412
return (
407-
(firstNode.kind === "variable" && firstNode.name === "this") ||
413+
(firstNode.kind === "variable" &&
414+
(firstNode.name === "this" ||
415+
(isExpressionStatement && isShort(firstNode.name)))) ||
408416
isReferenceLikeNode(firstNode)
409417
);
410418
}
411419

420+
function isShort(name) {
421+
return name.length < options.tabWidth;
422+
}
423+
412424
const lastNode = getLast(groups[0]).node;
413425

414426
return (
@@ -419,6 +431,8 @@ function printMemberChain(path, options, print) {
419431
);
420432
}
421433

434+
const isExpressionStatement =
435+
path.getParentNode().kind === "expressionstatement";
422436
const shouldMerge =
423437
groups.length >= 2 && !groups[1][0].node.comments && shouldNotWrap(groups);
424438

tests/call/__snapshots__/jsfmt.spec.js.snap

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -602,23 +602,21 @@ string
602602
string
603603
string');
604604
605-
$a
606-
->call(
607-
$a,
608-
'
605+
$a->call(
606+
$a,
607+
'
609608
string
610609
string
611610
string',
612-
$c
613-
)
614-
->call(
615-
$a,
616-
'
611+
$c
612+
)->call(
613+
$a,
614+
'
617615
string
618616
string
619617
string',
620-
$c
621-
);
618+
$c
619+
);
622620
623621
call("string $var string");
624622
call("string $var string");

tests/comments/__snapshots__/jsfmt.spec.js.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,8 @@ $var = $a /* Comment */
710710
711711
$a /* Comment*/->/*Comment*/ bar /* Comment */;
712712
$a /* Comment */['test'];
713-
$a /* Comment */
714-
->/* Comment */ bar();
715-
$a /* Comment */
716-
::/* Comment */ bar();
713+
$a /* Comment */->/* Comment */ bar();
714+
$a /* Comment */::/* Comment */ bar();
717715
718716
================================================================================
719717
`;

tests/member_chain/__snapshots__/jsfmt.spec.js.snap

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ $a->b->a();
372372
$a->b()->c()->d();
373373
$a->b->c->d;
374374
375+
// should inline
376+
$t->shouldReceive( 'signUp' )->with( anInstanceOf( 'Foo\\\\Bar\\\\Baz' ), $this->app['foo.bar.baz']->getEmail())->once();
377+
$te->shouldReceive( 'signUp' )->with( anInstanceOf( 'Foo\\\\Bar\\\\Baz' ), $this->app['foo.bar.baz']->getEmail())->once();
378+
$tes->shouldReceive( 'signUp' )->with( anInstanceOf( 'Foo\\\\Bar\\\\Baz' ), $this->app['foo.bar.baz']->getEmail())->once();
379+
// should break
380+
$test->shouldReceive( 'signUp' )->with( anInstanceOf( 'Foo\\\\Bar\\\\Baz' ), $this->app['foo.bar.baz']->getEmail())->once();
381+
$a = $t->shouldReceive( 'signUp' )->with( anInstanceOf( 'Foo\\\\Bar\\\\Baz' ), $this->app['foo.bar.baz']->getEmail())->once();
382+
375383
$this->loooooooooooong->lookup = (int) $this->getRequest()->getParam(
376384
'some-param'
377385
);
@@ -468,12 +476,31 @@ $a->a()->b();
468476
$a->a()->b();
469477
$a->a()->b;
470478
$a->b->a();
471-
$a
472-
->b()
479+
$a->b()
473480
->c()
474481
->d();
475482
$a->b->c->d;
476483
484+
// should inline
485+
$t->shouldReceive('signUp')
486+
->with(anInstanceOf('Foo\\\\Bar\\\\Baz'), $this->app['foo.bar.baz']->getEmail())
487+
->once();
488+
$te->shouldReceive('signUp')
489+
->with(anInstanceOf('Foo\\\\Bar\\\\Baz'), $this->app['foo.bar.baz']->getEmail())
490+
->once();
491+
$tes->shouldReceive('signUp')
492+
->with(anInstanceOf('Foo\\\\Bar\\\\Baz'), $this->app['foo.bar.baz']->getEmail())
493+
->once();
494+
// should break
495+
$test
496+
->shouldReceive('signUp')
497+
->with(anInstanceOf('Foo\\\\Bar\\\\Baz'), $this->app['foo.bar.baz']->getEmail())
498+
->once();
499+
$a = $t
500+
->shouldReceive('signUp')
501+
->with(anInstanceOf('Foo\\\\Bar\\\\Baz'), $this->app['foo.bar.baz']->getEmail())
502+
->once();
503+
477504
$this->loooooooooooong->lookup = (int) $this->getRequest()->getParam(
478505
'some-param'
479506
);

tests/member_chain/member_chain.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333
$a->b()->c()->d();
3434
$a->b->c->d;
3535

36+
// should inline
37+
$t->shouldReceive( 'signUp' )->with( anInstanceOf( 'Foo\\Bar\\Baz' ), $this->app['foo.bar.baz']->getEmail())->once();
38+
$te->shouldReceive( 'signUp' )->with( anInstanceOf( 'Foo\\Bar\\Baz' ), $this->app['foo.bar.baz']->getEmail())->once();
39+
$tes->shouldReceive( 'signUp' )->with( anInstanceOf( 'Foo\\Bar\\Baz' ), $this->app['foo.bar.baz']->getEmail())->once();
40+
// should break
41+
$test->shouldReceive( 'signUp' )->with( anInstanceOf( 'Foo\\Bar\\Baz' ), $this->app['foo.bar.baz']->getEmail())->once();
42+
$a = $t->shouldReceive( 'signUp' )->with( anInstanceOf( 'Foo\\Bar\\Baz' ), $this->app['foo.bar.baz']->getEmail())->once();
43+
3644
$this->loooooooooooong->lookup = (int) $this->getRequest()->getParam(
3745
'some-param'
3846
);

0 commit comments

Comments
 (0)