Commit Graph

121 Commits (master)

Author SHA1 Message Date
Christopher Chedeau 93cad97b14 Preserve inline comment as last argument (#1390)
We already had the logic but only applied it for comments on their own line, we should also do it if they are inline.

Fixes #1380
2017-05-01 14:29:03 -07:00
Christopher Chedeau 314e96322c Add parenthesis for unusual nested ternaries (#1386)
All of the discussions around ternaries are for the form

```js
cond1 ? elem1_if : cond2 ? elem2_if : elem_else
```

but some of them are for the form

```js
cond1 ? cond2 ? elem2_if : elem2_else : elem1_else
```

which is more rare and would be good to call out by adding parenthesis.

```js
cond1 ? (cond2 ? elem2_if : elem2_else) : elem1_else
```

Note that we only want parenthesis if it's written inline, otherwise the indentation is good enough to understand the flow:

```js
cond1
  ? cond2 ? elem2_if : elem2_else
  : elem1_else
```
2017-05-01 14:28:36 -07:00
Christopher Chedeau 042e603a6e Fix arrow function parenthesis with comments in flow (#1339)
`leadingComment` and `trailingComment` are babylon-specific, we should use `comments` which is what prettier adds.

Fixes #1335
2017-04-19 10:43:31 -07:00
Brian Ng 3a7559be58 Preserve comment for last arg of ObjectMethod (#1216)
* Preserve comment after last arg of ObjectMethod

* Preserve comment after last arg with default value
2017-04-12 16:32:04 -07:00
Jan Kassens b82220b20f Prittier printing of snapshots (#1190)
This uses a custom snapshot serializer to reduce escaping in snapshot files and
make them easier to read.

Snapshot serializers are documented here:
https://facebook.github.io/jest/docs/configuration.html#snapshotserializers-array-string
2017-04-12 13:41:51 -07:00
Christopher Chedeau f51c5daacb Fix comment detection before comma (#1127)
We need to call the `skipToLineEnd` which skips `,` after an inline comment. So we have to put it inside of the while loop as you can have many comments, a comma and then many more comments.

Fixes #964
2017-04-10 11:03:35 -07:00
Christopher Chedeau 0669160adc Fix last argument comment for function declaration (#1176)
We already handle this but forgot to do it for FunctionDeclaration

Fixes #1166
2017-04-10 10:55:29 -07:00
Christopher Chedeau 6552257ab0 Stabilize else comments (#1177)
In #544, I fixed it for all the cases but didn't check for else :(

Fixes #1161
2017-04-10 10:46:44 -07:00
Christopher Chedeau 6b806d4411 Re-run snapshot tests 2017-04-07 09:37:53 -07:00
Christopher Chedeau f69b26081f Stabilize VariableDeclarator comments (#1130)
Fixes #932
2017-04-07 07:57:44 -07:00
Lucas Duailibe 8e9fb43841 Fix trailing comment on switch case (#1136)
* Fix trailing comment on switch case

* Use unshift and add another test case
2017-04-05 16:27:14 -07:00
Alex Rattray d0666c3da4 Fix leading comment inside returned SequenceExpression (#1133) 2017-04-05 13:41:35 -07:00
ChristianHersevoort 04c959d687 Feature/verify against same snapshot (#1087)
* Verify parsers against same snapshot

- Reworked run_spec, now accepts 3th optional array argument for
additional parsers to verify against
- Merged duplicate run_spec configs
- Removed duplicate snapshot data

* Formatted run_spec.js with prettier

* Fixed node4 incompatibility
2017-03-25 08:10:17 -07:00
Christopher Chedeau c3199359b0 Revert "Improve snapshot naming" (#1070)
* Revert "Remove mutation in `printBinaryishExpressions` (#1067)"

This reverts commit e7312ad7b2.

* Revert "Make it clear what parser was used in each snapshot (#1068)"

This reverts commit 4f7ae4815b.
2017-03-21 17:38:28 -07:00
ChristianHersevoort 4f7ae4815b Make it clear what parser was used in each snapshot (#1068) 2017-03-21 14:47:23 -07:00
Christopher Chedeau ce6e897950 [RFC] Fix comment location for binary expressions (#1043)
The root cause is that we're calling `printComments` with an empty string, meaning that the leading/trailing comments are not correctly inserted at the right location.

So, the way to fix it is to call `p => concat(parts)` but because we're mutating the array in place, it doesn't work. We need to mutate it and create a copy. But, the root call is actually checking the shape of the parts array which our code is now breaking...

```js
          // Don't include the initial expression in the indentation
          // level. The first item is guaranteed to be the first
          // left-most expression.
          parts.length > 0 ? parts[0] : "",
```

The consequence is that binary expressions are no longer indented if the first expression has a comment (but now it places the comment properly!), which seems like a good trade-off.

I'm not sure if we should merge this one or instead refactor this code such that it doesn't rely on mutation and looking at the shape of the printed tree. `printMemberChain` is a good thing to reference for inspiration.

Fixes #946
2017-03-21 12:13:13 -07:00
Christopher Chedeau 553966345a Fix comment sorting location (#1038)
Right now it's only doing one test for begin/end, but in the issue #1037, we have two nodes that have the same start but different end. The current implementation incorrectly sorts them and the identifier ends up being before the container and therefore the comment search doesn't recurse into it.

Fixes #1037
2017-03-20 10:21:54 -07:00
Christopher Chedeau 82d6e0b4ab Unrestrict flow union comments check (#1040)
I was being very careful by checking all the surrounding types but it actually turns out we want this behavior all the time.

Fixes #931
2017-03-20 12:33:42 -04:00
Christopher Chedeau c749ddd508 Add parens around return for binaryish expressions (#870)
It looks better when the first element is aligned with the rest and this is consistent with the way we render `if` test.

Fixes #866
2017-03-18 19:35:09 -07:00
Christopher Chedeau 16ed595086 Preserve comment position for last arg or method (#1025)
In #856, it handled a bunch of cases but missed class methods

Fixes #905
2017-03-17 14:00:01 -04:00
Davy Duperron dde8463ad9 Fix additional empty line switch case comment (#936)
* Refactor condition in printStatementSequence, add new helper function.

* Add new test cases.

* Move logic in SwitchCase case.

* Revert unrelated changes, remove unecessary variables.

* Use util.getLast helper function.

* Move variables out of the loop.

* Simplify code.

* Fix mapping with mutated path.
2017-03-17 08:08:36 -07:00
David Hong 411f0fb6e7 Print line/mixed comments on new lines inside JSXEmptyExpression (#985) 2017-03-10 18:23:11 -08:00
Christopher Chedeau d39facc022 Only allow same-line arrow-less body for explicit nodes (#966)
In practice, trying to allow calls to be inlined is causing a lot of code to look very weird and unstable as seen by the four issues this is fixing. It also requires us to add a conditional group and do hackery around it.

Fixes #959
Fixes #760
Fixes #615
Fixes #625
2017-03-09 09:08:12 -08:00
Alex Rattray 24c314da01 Recursively find leading comments inside ReturnStatements (#955) 2017-03-08 17:06:18 -08:00
Davy Duperron 008ac1c479 Fix files with comments only (#813)
* Add new handleOnlyComments function.

* Update tests.

* Update test as the printer forces a trailing newline if there were any contents.

* Implement a different heuristic.

* Update tests.

* Add directives checking in handleOnlyComments function.

* Add directives checking in handleOnlyComments function (amend to retrigger CI).

* Remove duplicate.
2017-03-07 11:02:45 -08:00
Davy Duperron 3f6259554e Fix broken export comment (#899)
* Add new helper function to convert comments as blocks in ExportNamedDeclaration.

* Add new test.

* Switch to a leading position.

* Update test accordingly.
2017-03-05 21:49:07 -08:00
Davy Duperron c852d1631c Fix unprinted comments in destructuring (#898)
* Add handlePropertyComments printing function.

* Add new test.
2017-03-05 14:40:03 -08:00
Christopher Chedeau 7d24b0a1ff Fix flow union comments (#853)
The comments infra has been architected with trailing operators and fails for leading `|`. Instead of having leading comments, we can put trailing comments on the previous one and use the same technique as assignment to deal with the indentation.

Fixes #849
2017-03-03 13:54:45 -08:00
Christopher Chedeau fe38dab45a Fix comment for `call( // comment` (#858)
This is a pretty exotic way to put a comment and it's been a long time since I have seen prettier output be as broken :)

Fixes #857
2017-03-03 16:47:13 -05:00
Christopher Chedeau 4b237d331e Fix jsx expression comment that break (#852)
In #596, I fixed a bunch of jsx expression comment edge cases and happened to add a softline there. But it turns out that it's not needed and is actually harmful :)

Fixes #712
2017-03-02 22:27:08 -05:00
Christopher Chedeau d90af427fb Fix comment after the last argument of a function (#856)
The issue is that the comment algorithm attaches it to the return type / value but we want to attach it to the previous one.

Fixes #612
2017-03-02 22:17:22 -05:00
Christopher Chedeau 1b9234a8c4 Update flow to 0.40 (#808)
This fixes the bug with comments swapped between `/*` and `//`

Fixes #755
2017-02-24 15:46:20 -08:00
Davy Duperron b32ace8934 Fix function declaration params comments (#802)
* Add new handleFunctionDeclarationComments function.

* Add dangling comments printing for function params.

* Add new test case.

* Update tests.

* Refactor handleFunctionDeclarationComments to only addDanglingComment when no params.

* Remove unecessary helper function, only attach dangling comments when no params.

* Reset flow tests, no more regression.
2017-02-24 07:26:30 -08:00
Davy Duperron 260a141dbe [WIP] Fix comments in template literals (#643)
* Add handleTemplateLiteralComment helper function.

* Fix handleTemplateLiteralComment function.

* Extend handleTemplateLiteralComment to deal with trailing comments 🚀.

* Add test.

* Make handle comments function naming more consistent, fix merge conflicts.

* Update tests.

* Add better comment injection in Template Literal.

* Pass options to attach function.

* Update tests to match new implementation.

* Fix let -> var in findExpressionIndexForComment for NodeJS v4.

* Reorder after merge conflicts.

* Drop old tests for dangling arrays.

* Replace redundant conditional by a boolean 🚀.

* Refactor implementation.
2017-02-23 09:23:56 -08:00
Rogelio Guzman d45e157563 Upgrade to Jest 19 (#762)
* Upgrade to Jest 19

* Use exact versions for Jest
2017-02-21 13:59:19 -08:00
Davy Duperron d613f92f60 Fix missing comments in assignment pattern (#704)
* Add handleAssignmentPatternComments helper function.

* Add new tests 🎉.

* Remove unecessary arg in function.

* Reorder it.

* Experimental fix for key comments in the printComments function.

* Pull master and switch to @vjeux implementation.

* Fix bad rebase.

* Add additional checking for enclosingNode in handleObjectProperty function.
2017-02-20 07:39:08 -08:00
Davy Duperron 01b0692307 Fix leading & operators in flow types (#738)
* Add new logic for & op as IntersectionTypeAnnotation.

* Update flow tests accordingly.

* Update flow types perf tests.

* Remove op variable.
2017-02-20 07:18:39 -08:00
Davy Duperron c32e0339d0 Fix missing dangling comments in for loop (#742)
* Add dangling comments printing in for loops.

* Add new tests.

* Simplify implementation by moving dangling comments upfront.

* Update tests accordingly.
2017-02-20 09:17:10 -05:00
Davy Duperron 0f34fb91eb Fix missing dangling comments in arrays (#744)
* Add missing dangling comments printing in arrays.

* Update tests accordingly.
2017-02-19 17:25:18 -08:00
Davy Duperron 1affecc1a0 Fix missing dangling comment in exports (#741)
* Add missing dangling comment injection.

* Update tests.

* Add code comment.
2017-02-19 14:39:09 -08:00
James Long 5d131027ce Make trailing-comma option support 2 different modes (#641)
* Make trailing-comma option support 2 different modes

* Add deprecation warning

* Add deprecation warning to API
2017-02-17 22:44:55 -05:00
Christopher Chedeau e30482d440 Fix trailing new lines preservation (#724)
There is an off by one error which made the algorithm not work all the time. In many cases, it actually is the opposite, so whenever you save, it adds/removes that line. I noticed it during the --debug-check run on our codebase but didn't investigate.

Fixes #723
2017-02-16 09:56:12 -05:00
Christopher Chedeau 330b136cec re-run snapshot tests 2017-02-15 21:21:09 -08:00
Davy Duperron b661fecd9c Fix comments in return statement argument (#657)
* Extend ReturnStatement case to better handle comments in argument 🎉.

* Update test.

* Add missing space between return and left parenthese.

* Fix current tests and add new tests.

* Do not break one-liner returns.

* Revert specific test.

* Patch from #683.
2017-02-13 18:35:42 -08:00
Christopher Chedeau d09e455a71 Always put a hardline before dangling comment (#675)
In #563 it looked odd that there was no space before `//`, it turns out that we don't automatically go to the new line for dangling comments. I think that we just should no matter what, so this is what this diff does.

Fixes #563
2017-02-13 20:08:58 -05:00
Christopher Chedeau ec6ffbe063 Properly support member chains comments (#668)
When there is a comment right before the first `.`, we want to force break such that the comment is printed on its own line.

Note: this needs to be based on-top of #667 as it always indent the first `.`

Fixes #613
2017-02-13 09:05:18 -08:00
Davy Duperron 76982ee2f7 Fix Flow union type annotations indentation (#650)
* 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 🚀.
2017-02-13 09:03:56 -08:00
Christopher Chedeau c5bfdae5d8 Fix dangling comments for arrays (#665)
It was missing a softline. I copy and pasted the dangling handling for objects.

Fixes #624
2017-02-12 23:09: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
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 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 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 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 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
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 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
Amjad Masad 045a351fce Fix closing call expression commented out (#530)
* [Failing test] Comments in call expression

```js
foo(
 // Hi
)
```

prints

```js
foo
// Hi();
```

* add one more failing case

* Don't group last args that has comments attached

* Update snapshot
2017-01-31 06:52:53 -08:00
Amjad Masad 57c8c99e7b Fix syntax error in empty object with dangling comment (#533) 2017-01-31 06:52:05 -08:00
Christopher Chedeau 959235afd7 [RFC] Keep expanded objects expanded (#495)
Another attempt at solving the issue where objects are not expanded the way people expect. If there's any new line in the original source, it's going to expand it. This gives more control to the user in how the objects should be formatted.

Fixes #74
2017-01-30 09:08:55 -08:00
Christopher Chedeau 32059f6914 Proper support for dangling comments (#492)
* Proper support for dangling comments

In one code path, the dangling comment case is not properly handled. So I added the dangling comment, but it turns out that we only print manually in two node types: Program and BlockStatement. I made the generic printComment function print it everywhere but those two nodes. I tried to get rid of those special cases but unfortunately we need them there otherwise they are not printed at the right place.

Fixes #20

* Output dangling comments in specific places
2017-01-27 16:09:15 -05:00
Christopher Chedeau 8f26568bc8 Properly print comments for BinaryExpression (#494)
We don't call the generic print on the BinaryExpression itself, so we need to manually print those comments. It's going to be useful for my work on the MemberExpression :)
2017-01-27 14:03:44 -05:00
Christopher Chedeau 612574d02d Stop using conditionalGroup inside of UnionTypeAnnotation (#491)
It's actually not needed to use conditionalGroup as we can use ifBreak for it. I was able to do it just for cleanup and found out that it also fixed two of the bugs we have with comments. That's great :p

Fixes #485
Fixes #486
2017-01-26 21:41:50 -05:00
James Long eaddd4b3e6 Refactor comment algorithm and improve newline/spaces detection (fixes #483, #275, #135, #78, #36) (#482) 2017-01-26 11:57:43 -08:00
James Long 3aa267d5e8 Remove `multilineGroup` (#450) 2017-01-24 13:54:01 -05:00
Christopher Chedeau fb52e7a5c8 Do not put a newline on empty `{}` for functions (#447)
The original intent of it was for `if then else` and `try catch` as they aren't likely to be empty, but it accidentally caught function bodys, which have many valid reasons to be empty. Let's special case those out.
2017-01-24 12:37:01 -05:00
James Long cd2f7393ef Propagate breaks upwards automatically, introduce `breakParent`, and deprecate `multilineGroup` (#440) 2017-01-23 18:47:11 -05:00
Christopher Chedeau d8d5c7bc55 Introduce --parser/parser option and deprecate --flow-parser/useFlowParser (#342)
The previous API was inconsistent. The new one is

```js
--parser flow
--parser babylon

{parser: 'flow'}
{parser: 'babylon'}
```

if we ever want to add new parsers in the future it'll allow that more easily.

I put a console.log in parser.js in both functions and tested that the test suite worked both with and without the change in run_spec. I also tested that both the previous and new command line options are working.

At some point in the future we'll likely want to get rid of the old api but might as well keep supporting it so we don't break anyone for now.
2017-01-19 14:35:12 -08:00
James Long 6e57b7f2dd Print dangling comments in blocks (#331) 2017-01-19 14:46:37 -05:00
Christopher Chedeau 28e886aea1 Add tests for comments (#330)
This adds a snapshot test for all of the weird things I've seen happening with comments.
2017-01-19 14:28:40 -05:00