Commit Graph

459 Commits (6632b95497423ed07ddaee8035abed99aaf5a777)

Author SHA1 Message Date
Christopher Chedeau 6632b95497 Preserve blank lines inside of objects (#606)
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
2017-02-09 11:08:58 -05:00
Christopher Chedeau 0287cadc62 Run prettier 0.16.0 on the codebase (#631) 2017-02-09 10:44:56 -05:00
Christopher Chedeau 4a00bc33a5 Put short elements at right of single binary expression on same line (#605)
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 #583
Fixes #503
2017-02-09 10:44:03 -05:00
Christopher Chedeau cda7650e27 Do not indent binary expressions inside of if (#604)
* 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
2017-02-09 10:36:05 -05:00
Umidbek Karimov 97fd8a8d81 Log filename with [update] or [ignore] flags during `--write` process. (#584)
* 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.
2017-02-07 14:03:48 -05:00
Alex Rattray 91d4f400a4 [JSX] jsx-whitespace breaks with parent (fixes #622) (#626) 2017-02-07 13:47:34 -05:00
Nathan Friedly 79d537a3b0 test on current release of node.js (#595) 2017-02-07 12:46:39 -05:00
Christopher Chedeau a051545fcb Preserve empty line before last comment (#594)
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
2017-02-07 12:41:49 -05:00
James Long 05637a73d3 Update snapshots 2017-02-07 12:15:36 -05:00
James Long 2ff937acdd Fix formatting in readme 2017-02-07 10:42:54 -05:00
James Long a3a6834a05 Expand vim instructions 2017-02-07 10:42:18 -05:00
Christopher Chedeau 6a05a55852 Remove last trailing line for directives-only files (#609)
* 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
2017-02-07 10:39:04 -05:00
Christopher Chedeau 4df5250f74 Add --debug-check cli option (#627)
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
2017-02-07 10:04:44 -05:00
Alex Rattray ee593bb645 [JSX] Fix spurious newline (fixes #614) (#628) 2017-02-07 09:58:25 -05:00
Christopher Chedeau 98d04d8c70 0.16.0 2017-02-05 18:28:20 -08:00
Christopher Chedeau b42e865c61 Print dangling on ast on all the paths 2017-02-04 18:53:47 -08:00
Christopher Chedeau 7abd6f5f9b Fix single indented JSX comment (#596)
This one was pretty annoying to figure out but now it's working. All the crazy comments I could throw at jsx seems to be working fine now :)

Fixes #543
2017-02-04 21:37:18 -05:00
Christopher Chedeau 153bf4cad9 Remove first line special case (#597)
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
2017-02-04 21:36:48 -05:00
Christopher Chedeau df831d7b59 Fix files with only comments on the flow parser (#598)
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`.
2017-02-04 21:36:29 -05:00
Christopher Chedeau c07329b4ef Do not attach comments to EmptyStatement (#599)
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.
2017-02-04 21:32:51 -05:00
Christopher Chedeau 77bf64c6b1 Print CallExpression comments inside of memberChain (#600)
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!)
2017-02-04 21:31:54 -05:00
Christopher Chedeau fb9629ca7f Better support try/catch comments (#607)
Uses the same technique as for if/then/else. Added a helper function to make it easier.

Fixes #601
2017-02-04 21:25:29 -05:00
Christopher Chedeau 91087e1e84 Use arrow function when inputted that way for flow objects (#608)
Omg, after 10 unsuccessful tries, I finally managed to get it working! The deciding factor was a trick by @mroch <3

Fixes #310
2017-02-04 21:23:37 -05:00
Davy Duperron 8c66e44db1 Fix use strict as expression statement (#602)
* 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.
2017-02-04 13:16:24 -08:00
Christopher Chedeau 6b74de3716 Remove test.js and put it back in the gitignore 2017-02-03 14:37:13 -08:00
Christopher Chedeau 3b66af3d60 Remove the concept of globalPrecedingNode (#561)
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
2017-02-03 13:56:06 -08:00
Christopher Chedeau 0924e7f3dc Break long imports (#590)
Fixes #576
2017-02-03 16:19:14 -05:00
Christopher Chedeau e623668183 Make all the member expressions but the last one part of the first group (#589)
Fixes #587
2017-02-03 16:18:54 -05:00
Christopher Chedeau 57ab92ed33 Support dangling comments in ClassBody (#570)
Comments were dropped there
2017-02-03 16:16:32 -05:00
Christopher Chedeau 4bd194e2ce Use breakParent inside of last arrow expansion (#559)
Fixes #549
Fixes #553
2017-02-03 16:15:21 -05:00
Christopher Chedeau b2a8f2de56 Add parenthesis around single argument arrow if comments (#573)
Fixes #568
2017-02-03 16:14:40 -05:00
Christopher Chedeau b7103ddbf4 Run prettier on 0.15.0 (#558) 2017-02-03 08:50:51 -08:00
Davy Duperron 6efad59300 565 jsx parentheses fix (#580)
* Add JSXElement in ShouldPrintSameLine helper function.

* Add tests 🚀.
2017-02-02 16:25:34 -08:00
Alex Rattray 86d1264097 Fix ternary indent bug (#577)
* Add test.js files back to source control

* Fix ternary indent bug (#564)
2017-02-02 10:37:59 -08:00
Simon Lydell 69b7bd3fa8 Revert "Print \x and \u escapes in strings and regexes lowercase (#522)" (#574)
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.
2017-02-02 09:54:10 -08:00
Christopher Chedeau b040be2206 0.15.0 2017-02-01 16:31:11 -08:00
Christopher Chedeau 5042168df6 Make comments inside of MemberExpression look good (#556)
This is using the same technique as #544 and will conflict with it when we try to merge both :)

Fixes #200
2017-02-01 16:26:55 -08:00
Christopher Chedeau b6c9e42705 Whitelist UnaryExpression for parentless objects (#545)
It seems like unary are unlikely to need parenthesis

Fixes #542
2017-02-01 16:24:22 -08:00
Christopher Chedeau e56fd38604 Make comments between if & else to look good (#544)
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
2017-02-01 16:23:49 -08:00
Alex Rattray be8f13dd24 [JSX] Handle non-breaking space (#557) 2017-02-01 15:29:42 -08:00
Lucas Bento 6c7837bab6 Fix small typo on Jetbrains section (#552) 2017-02-01 09:17:33 -08:00
Christopher Chedeau 7587b5a599 Remove spurious test.js 2017-01-31 19:38:57 -08:00
Christopher Chedeau d0bc2991f6 Reimplement MemberExpression printing (#469)
There are currently three issues related to suboptimal rendering of MemberExpression chains. The previous implementation was trying to flatten only a single group at the same time, but it didn't work well because we didn't have the full context to be able to make decisions.

In this implementation, I'm going through the entire chain at the same time and group it into logical units and make decisions based on this. It solves all the problems I can think of and if we need to tweak it in the future, it should be easy.

Fixes #268
Fixes #212
Fixes #21
2017-01-31 12:32:42 -08:00
Christopher Chedeau 410e75d858 Update snapshot tests from conflicting PRs 2017-01-31 09:39:00 -08:00
Christopher Chedeau d3fa519492 Break nested calls (#517)
If there's a break inside of a call, we want to force it in the group, otherwise it may get the indentation wrong. See the real-world use case in #513

Fixes #513
2017-01-31 11:45:47 -05:00
Christopher Chedeau 8bc3c617a0 Preserve next line with trailing comment (#535)
It turns that our hasNextLine logic needs to be tuned to skip all the trailing comments. The code is not pretty but it does the job. It looks like it fixes a bunch of things in the test cases :)

I made sure that nested inline comments are NOT valid JavaScript

```js
/* /* a */ */
Uncaught SyntaxError: Unexpected token *
```

so it is okay to do a dumb search for */ when you are in a comment
2017-01-31 11:27:44 -05:00
Umidbek Karimov f5291e2f01 Fix Jetbrains example screenshot url. (#534) 2017-01-31 07:26:33 -08:00
Simon Lydell 7148184d65 Print \x and \u escapes in strings and regexes lowercase (#522)
* Print \x and \u escapes in strings and regexes lowercase

Theoretically, we would want to do this for escapes int identifiers as
well. However, neither flow nor babylon preserves escapes in
identifiers. For example, `\u0061.\u{0061}` cannot be distinguished from
`a.a`. Nobody uses such escapes in real code anyway. It could also be
considered a feature that such escapes are converted to real unicode
characters.

* Update snapshots

* Normalize escapes in template literals

* Update snapshots
2017-01-31 06:56:26 -08:00
Hawken Rives ea597182a9 Add 0.14.1 to CHANGELOG (#525) 2017-01-31 06:54:53 -08:00
Amjad Masad 3155acba6e Update `bracketSpacing` comment to say it's about {} (#529)
Before e13bb7dbba `bracketSpacing` used to be about both `[]` and `{}` but now it's only about `{}`.
2017-01-31 06:54:31 -08:00