* Add prettier indentation for UnionTypeAnnotation in ObjectTypeAnnotation.
* Update tests accordingly.
* Fix indentation.
* Update a bunch of tests 🚀.
* Switch to a more consistent indentation.
* Fix tests to match new indentation 🚀.
The reason why trailing comments on a line work with commas is because of a special `lineSuffix` document that delays printing the content until a \n is printed. But `lineSuffix` only worked for pure string. By making it work with arbitrary documents we can fix trailing commas with trailing comments.
Fixes#538
Before, we would always concatenate the first `.call()` if the identifier started with a capital letter, but we do want to break if the content of the call doesn't fit in one line.
Fixes#639
Because the group was too high up, the comment would be taken into consideration to determine the size and it would break and add parenthesis. Adding a group where we actually want the ifBreak seems to be passing all the existing tests and fixes this edge case.
Fixes#655
I was trying to debug #620 and got confused that it didn't show `//comment1`. Turns out we just fail silently if we can't print a node type. I added support for `line-suffix` and throw if we encounter a node type that we can't print.
We already preserve blank lines inside of classes but not objects. It's still very common to have objects that represent classes (React.createClass) and having the same behavior there seems like a good idea. It does make the snapshot tests look better!
Fixes#554
I've been trying to come up with a heuristic to stop putting small elements on a new line and I just realized that all the cases I found out only involved a single binary expression. If there's only a single operation, we can just put them on the same line (if it fits) and it's not going to look weird.
We do the same thing for MemberExpression where we only break if there are more than one ".".
Fixes#583Fixes#503
* Do not indent binary expressions inside of if
The reason why the indent has been added is to support the
```js
var x = a ||
b;
```
use case where we need a level of indentation. But inside of a `if` it just looks strange
```js
if (
a ||
b
) {
```
because the parent already indented the first line.
Fixes#567
* Tweak logic to make more explicit
* Make `--write` logs more informative.
* Add format duration to logs.
* Use colors instead of [ignore] and [update] labels.
* Remove `colors` dependency.
* Use `chalk` to colorize logs.
* Print filename before `format` and add duration info later.
* Print filename before read error.
We want to preserve an empty line before the last comment. It is added as a trailing comment of the node and adding the same logic to detect if the next line is empty is working.
Fixes#566
* Remove last trailing line for directives-only files
There are two hardlines that are added that do not need to.
- The first one is when there's an empty line afterwards, we want to remove it. The solution I opted for to fix this one is to trimRight the originalText so that globally it's never going to return yes for the isNextLineEmpty.
- The second one is at the end of Program, but we already printed it inside of the directive itself, so we can just add a condition to make sure it's only printed when there's a body or a comment, but not a directive.
Fixes#527
* Add comment
This let's you run `pp(x) === pp(pp(x))` across an entire codebase quickly. I think it's small enough that we can merge it in to master while we are nailing down all the issues.
You can see the output in #610
If you removed it before #561, it would trigger an exception in the globalPreceding branch. But now that we don't have this hack anymore, it works fine.
Fixes#543
Babylon has a two top level nodes: File and Program whereas flow just has Program. This causes two things two happen that prevents comments from being displayed:
1) Because there's a single node, none of following/preceding/enclosing exist. We ran into a TOOD case that we now need to fill. We just need to attach comments to the only node we have: the ast.
2) Both the raw comments and the computed comments are set on the `.comments` field of the object, however, it is being reset after calling `attach`, so we lose the computed comments :( The fix is to use a local variable and delete the comments before calling `attach`.
Since we don't print EmptyStatement, we don't want to attach comments to them. I've tried actually printing the EmptyStatement but it messes up a ton of other things.
I made sure that MemberExpressions where printed correctly but didn't do the same for CallExpression. Now it is! It is a bit less straightforward because the comments for the root CallExpression have already been printed, so we need to extract the first call out.
(This is the last place where we drop comments on the test suite, we can start enabling throwing when comments are dropped!)
* Add parentheses if node is StringLiteral surrounded by ExpressionStatement :tools:.
* Add new test.
* Update tests to run against Babylon, plus no regression case.
* Split actual test and no regression test in two files.
* Disable Flow till facebook/flow#3234 is merged.
The original motivation for this change is trying to fix#560 where the comment was attached to the JSXText node instead of the JSXExpressionContainer because it used the globalPrecedingNode. In general, I don't think that it is very safe to attach a comment to a random node just because it happened to be before.
I tried to delete the globalPrecedingNode codepath and I think that it actually improves the results. I'll add inline comments in the pull request to explain the various changes.
Fixes#560
This reverts commit 7148184d65.
There are four types of literals where escapes were normalized:
1. Strings ('\xAb' and "\xAb")
2. Regexes (/\xAb/)
3. Untagged template literals (`\xAb`)
4. Tagged template literals (tag`\xAb`)
However, changing the case of the escapes alters the runtime behavior of
in two of the above cases.
```js
/\xAb/.source === '\\xAb' // true
String.raw`\xAb` === '\\xAb' // true
```
So for regexes and tagged template literals the escapes must not be
changed. Instead of enforcing lowercase escapes in only 50% of the
different cases, it was decided not to bother with escapes at all.
Closes#562.
While trying to figure out how to handle both MemberExpression comments and IfStatement comments, I ended up doing this one as well... Sorry @yamafaktory :(
The logic is a bit annoying but works.
Fixes#487