Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve line breaks in container literals
Summary: If you write this in Javascript: ``` const foo = { bar: baz; qux: grault; }; ``` then Prettier will respect that you put the key-values on different lines, even though they could all fit on the same line. (This applies to even the case that there is only one key-value pair.) This is desired behavior. Oftentimes we want to put members of a container literal on lines of their own for readability or emphasis. This commit changes it so that we look to see if the members of the container literal appear to be on different lines, and if so, preserves that formatting and keeps them on different lines. Differential Revision: D5503181 fbshipit-source-id: f7d81e2ce909430161035524b0f442d6806b9fe2
- Loading branch information
Showing
4 changed files
with
130 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
hphp/hack/test/hackfmt/tests/collection_literal_newlines.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?hh // strict | ||
$foo = array('should_stay_on_same_line'); | ||
$foo = array( | ||
'should_remain_on_new_line', | ||
); | ||
$foo = varray['should_stay_on_same_line']; | ||
$foo = varray[ | ||
'should_remain_on_new_line', | ||
]; | ||
|
||
$foo = array('should_stay' => 'on_same_line'); | ||
$foo = array( | ||
'should_remain' => 'on_new_line', | ||
); | ||
$foo = darray['should_stay' => 'on_same_line']; | ||
$foo = darray[ | ||
'should_remain' => 'on_new_line', | ||
]; | ||
|
||
$foo = Set { 'should_stay_on_same_line' }; | ||
$foo = Set { | ||
'should_remain_on_new_line', | ||
}; | ||
$foo = Vector { 'should_stay_on_same_line' }; | ||
$foo = Vector { | ||
'should_remain_on_new_line', | ||
}; | ||
$foo = Map { 'should_stay' => 'on_same_line' }; | ||
$foo = Map { | ||
'should_remain' => 'on_new_line', | ||
}; | ||
|
||
$foo = keyset['should_stay_on_same_line']; | ||
$foo = keyset[ | ||
'should_remain_on_new_line', | ||
]; | ||
$foo = vec['should_stay_on_same_line']; | ||
$foo = vec[ | ||
'should_remain_on_new_line', | ||
]; | ||
$foo = dict['should_stay' => 'on_same_line']; | ||
$foo = dict[ | ||
'should_remain' => 'on_new_line', | ||
]; | ||
|
||
// If there are no elements, there should not be a newline. | ||
$foo = dict[ | ||
]; |
47 changes: 47 additions & 0 deletions
47
hphp/hack/test/hackfmt/tests/collection_literal_newlines.php.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?hh // strict | ||
$foo = array('should_stay_on_same_line'); | ||
$foo = array( | ||
'should_remain_on_new_line', | ||
); | ||
$foo = varray['should_stay_on_same_line']; | ||
$foo = varray[ | ||
'should_remain_on_new_line', | ||
]; | ||
|
||
$foo = array('should_stay' => 'on_same_line'); | ||
$foo = array( | ||
'should_remain' => 'on_new_line', | ||
); | ||
$foo = darray['should_stay' => 'on_same_line']; | ||
$foo = darray[ | ||
'should_remain' => 'on_new_line', | ||
]; | ||
|
||
$foo = Set { 'should_stay_on_same_line' }; | ||
$foo = Set { | ||
'should_remain_on_new_line', | ||
}; | ||
$foo = Vector { 'should_stay_on_same_line' }; | ||
$foo = Vector { | ||
'should_remain_on_new_line', | ||
}; | ||
$foo = Map { 'should_stay' => 'on_same_line' }; | ||
$foo = Map { | ||
'should_remain' => 'on_new_line', | ||
}; | ||
|
||
$foo = keyset['should_stay_on_same_line']; | ||
$foo = keyset[ | ||
'should_remain_on_new_line', | ||
]; | ||
$foo = vec['should_stay_on_same_line']; | ||
$foo = vec[ | ||
'should_remain_on_new_line', | ||
]; | ||
$foo = dict['should_stay' => 'on_same_line']; | ||
$foo = dict[ | ||
'should_remain' => 'on_new_line', | ||
]; | ||
|
||
// If there are no elements, there should not be a newline. | ||
$foo = dict[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters