Fix doc literal function args erroneously overriding split behavior
commit6ec875cab3334acdfebfc3f527a64e7377bd99e9
authorJake Bailey (Hacklang) <jakebailey@fb.com>
Fri, 7 Jan 2022 23:26:25 +0000 (7 15:26 -0800)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Fri, 7 Jan 2022 23:27:50 +0000 (7 15:27 -0800)
tree15133394a14f7df55f89000eb5af71f3927ce11d
parent7c3c0f3ae7daf24967915054b83e9fc1f0b959d7
Fix doc literal function args erroneously overriding split behavior

Summary:
hackfmt currently formats this:

```
f(
  123,
  <<<DOC
  abc
DOC
,
);
```

to this:

```
f(
  123, <<<DOC
  abc
DOC
);
```

The main issue here is the `this#set_next_split_rule (RuleKind (Rule.Simple Cost.Base))` in Chunk_builder. It causes the split before the doc literal to break independently of the other splits around the arguments.

This line was added in D4829869 (https://github.com/facebook/hhvm/commit/723f0ff06a90f3b037998ca79762eaa882d64889). The diff summary does not explain this change, but I think it may have been added to support producing output like this:

```
  return JSON::decode(<<<JSON
  { "foo": "{$bar}" }
JSON
  );
```

Rather than:

```
  return JSON::decode(
    <<<JSON
  { "foo": "{$bar}" }
JSON
  );
```

The former output is nice, but `set_next_split_rule` is the wrong way to do it. Since D4829869 (https://github.com/facebook/hhvm/commit/723f0ff06a90f3b037998ca79762eaa882d64889), we have added a more sensible mechanism ([delimited_nest ~split_when_children_split](https://www.internalfb.com/code/fbsource/[f260ce4b5b7624ef8663830b434553bfc19a283a]/fbcode/hphp/hack/src/hackfmt/hack_format.ml?lines=3032)) for allowing function arguments to be formatted in this style.

This diff removes the `set_next_split_rule`, fixing the behavior for arguments preceding doc literals. It also uses `delimited_nest ~split_when_children_split` to continue producing the style with no line breaks inside the function invocation parentheses, when appropriate.

The new behavior is version-gated for hackfmt.version >= 3.

Reviewed By: Wilfred

Differential Revision: D31095509

fbshipit-source-id: 6bbaec0963e38f3cc9ed61689a27f69020a30dee
hphp/hack/src/hackfmt.ml
hphp/hack/src/hackfmt/chunk_builder.ml
hphp/hack/src/hackfmt/hack_format.ml
hphp/hack/src/hackfmt/libhackfmt.ml
hphp/hack/test/hackfmt/tests/doc_string_literals_v2.flags [new file with mode: 0644]
hphp/hack/test/hackfmt/tests/doc_string_literals_v2.php [copied from hphp/hack/test/hackfmt/tests/doc_string_literals.php with 68% similarity]
hphp/hack/test/hackfmt/tests/doc_string_literals_v2.php.exp [copied from hphp/hack/test/hackfmt/tests/doc_string_literals.php.exp with 69% similarity]
hphp/hack/test/hackfmt/tests/doc_string_literals_v3.flags [new file with mode: 0644]
hphp/hack/test/hackfmt/tests/doc_string_literals_v3.php [moved from hphp/hack/test/hackfmt/tests/doc_string_literals.php with 68% similarity]
hphp/hack/test/hackfmt/tests/doc_string_literals_v3.php.exp [moved from hphp/hack/test/hackfmt/tests/doc_string_literals.php.exp with 63% similarity]