Commit Graph

114 Commits (f207e665bc5af4c95f361d65387638c33521c492)

Author SHA1 Message Date
Simon Lydell dc93bdc983 Sync the Flow tests (#1163)
* Extract custom tests from tests/flow/

Approach:

1. Remove all .js files in tests/flow except .snap.js files.
2. Copy over all .js files from tests/ in the flow repo.
3. Go through the diff looking for deletions.
   - It was easy to see which deletions were due to changes in the tests
     due to updates in the flow repo.
   - For the rest of the deletions, I used `git blame` to verify that
     they had been added by us since the flow tests were copied over.

This makes tests/flow/ simply a copy of the tests from the flow repo,
making it easier to sync with the upstream flow tests in the future.

* Add a script for syncing the flow tests

* Sync the flow tests
2017-04-08 07:52:38 -07:00
Christopher Chedeau 41dee70795 Allow to break after = for strings (#1142)
This is not a general fix but it solves some issues that people are seeing where prettier goes > 80 columns.

Fixes #1141
Also fixes part of #1110
2017-04-07 10:51:02 -07:00
Brian Ng 651cce2066 Import new_spread flow tests (#1138) 2017-04-05 17:39:11 -07:00
Brian Ng 2e96574941 Switch to new test syntax for flow predicate tests (#1137) 2017-04-05 16:32:55 -07:00
Joseph Savona fe5de43cd7 Fix printing of Flow type number literals (#1128) (#1132) 2017-04-05 13:22:23 -07:00
Brian Ng d9a4efcc35 Bump babylon to 7.0.0-beta.8 (#1118)
* Bump babylon version

* Enable flow predicate tests in babylon
2017-04-04 14:21:56 -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 dc0fbf7822 Special logic for flow intersection (#1018)
We started using the same logic for union and intersection but then added a special case for `a & {}`. It turns out that they should be handled completely differently in practice.

The heuristic i'm using is if you go from object to non-object or vis-versa, then inline, otherwise go to the next line and indent (like &&).

Fixes #864
Fixes #1017
2017-03-22 10:01:34 -07:00
Brian Ng 42910424a1 Fix parsing flow variance on babylon 7 (#1069)
* Enable babylon on variance tests

* Fix parsing flow variance in babylon 7
2017-03-22 09:37:26 -07:00
Brian Ng 41977c293e Add support for ForOfStatement with await flag (#1073)
* Enable babylon on flow async_iteration tests

* Add support for ForOfStatement with await flag
2017-03-22 09:23:57 -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 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 a502bf9f45 Add ability for flow generics to break (#1041)
While looking at an instability on the React codebase ( ba1299acc2 (diff-2550aa3d377452ae29361f5e53c51c10) ), I realized that we don't let them break which makes the code look weird.

So I added the ability for them to break :)
2017-03-20 12:35:30 -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
Royce Townsend 9eb389b9f4 Group first arg for inline functions (#947)
* Group first arg for inline functions

* Group first arg. Unless there are comments.

* Group first arg. Allow second arg to be empty object/array.

This implementation has a side effect of disallowing empty objects/arrays in the should group last arg heuristic.
2017-03-18 11:20:07 -07:00
James Long eeae443b43 Separate `if` and `else` groups (fixes #616) (#1032) 2017-03-17 14:02:35 -07:00
Davy Duperron 915967b974 No parenthesis for Flow shorthand with one arg (#972)
* Implement checking for not printing the parens in this case.

* Add test case.

* Remove parent type checking.

* Fix test accordingly.

* Refactor to a better heuristic.

* Add new test cases.

* Fix implementation.

* Fix unnecessary function call.

* Add new test cases 🚀.

* Use isObjectTypePropertyAFunction and create isTypeAnnotationAFunction.

* Add missing space.

* Add new test cases.
2017-03-16 10:32:59 -07:00
Brian Ng a2b560887c Add support for breaks in TupleTypeAnnotation (#1003) 2017-03-15 09:35:22 -07:00
Alex Rattray 24c314da01 Recursively find leading comments inside ReturnStatements (#955) 2017-03-08 17:06:18 -08:00
Kevin Gibbons 7443f4cbd6 Parenthesize function expressions in expression position (#941)
* refactor needsParens for function expressions

* snapshots

* comment
2017-03-07 18:53:42 -08:00
Davy Duperron 1b6ddf9a7f Fix binary op as body in arrow expression (#921)
* Implement new logic for wrapping binary op in arrowFunctionExpression.

* Add new test cases.

* Reuse new helper function in order to fix #917.

* Add new test case.

* Extend heuristic to dive deeper into mixed types.

* Add new test.

* Enhance logic to cover more cases.

* Add new test cases.

* Disable Flow as it gets BindExpression as an unexpected token.

* Simplify getCombinedDeepest function.

* Add missing case.

* Extract all conditions in switch cases to one top level condition.

* Refactor implementation to make it cleaner and also handle ExpressionStatement.

* Update related test cases.

* Add new test case.

* Make condition less expensive.

* Clean up unecessary conditions, simplify condition involving startsWithOpenCurlyBrace.

* Update and add new test cases for better coverage.

* Remove unecessary condition, refactor canBeFirstInStatement to drop some useless parens.

* Update test cases accordingly 🚀.
2017-03-07 13:24:47 -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
Brian Ng f6bbc2ed2e Fix bug with importing empty type (#904) 2017-03-05 19:08:42 -08:00
Brian Ng 226fd85d8f Fix paren removal on UnionTypeAnnotation (#878) 2017-03-03 17:37:00 -08:00
Christopher Chedeau 735f07fa4f Do not put \n after label (#860)
This is a leftover from the recast prototype, it hasn't been touched since then. I have never seen anyone not put the label on the same line.

Fixes #859
2017-03-03 16:46:40 -05:00
Christopher Chedeau fa9dca362e Add parenthesis around assignments (#862)
This is a neat trick from the React codebase. It helps highlight the fact that this is an assignment and not a comparison which is subtle to realize.

Fixes #861
2017-03-03 16:46:11 -05:00
Christopher Chedeau 705ac7d3cf Do not break require calls (#841)
This has come up a couple times on the issue tracker on the react-native-web discussion about Twitter internals. It seems like there's a concensus that people don't break long require calls and they'd rather have it go > 80 columns.

Fixes #303
Fixes #752
2017-03-02 22:21:02 -05:00
Davy Duperron 04f09ddc80 Fix function call args (#809)
* Implement new heuristic for arguments in printArgumentsList function.

* Remove unwanted modification.

* Add new helper function.

* Update tests according.

* Add new tests for #682, #677, #253.

* Move heuristic in groupLastArg condition.

* Update tests.
2017-02-25 09:56:13 -08: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
Christopher Chedeau 1d02ca7849 [RFC] Add parenthesis around && inside of || (#780)
It seems like precedence for this combination of operators is very unclear to people (myself included) and consistently adding parenthesis there would be good.

Fixes #773
2017-02-23 09:27:00 -08:00
Christopher Chedeau 694816517a Do not expand empty catch (#783)
I originally wanted to expand it but I think that it may not be a good decision because it's very common for them to be empty and you want it to take as little space as possible.

I tried to remove all those conditions but I think that there are valid places where we always want to expand like empty if/for/while loops.

Fixes #778
2017-02-23 07:29:05 -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 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
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 7046d978a4 [RFC] Preserve new lines between array elements (#707)
This has come up many times in the past and while going through the fb codebase, there are a few instances where we group array elements logically using empty lines and it's a shame that they are gone.
2017-02-15 20:52:28 -05:00
Brian Ng e0f82cd7d9 Ensure importKind is printed (#718) 2017-02-15 12:56:34 -08:00
Christopher Chedeau 9a5090a473 Do not inline new as last argument (#705)
We inline function definitions but we shouldn't inline new expressions,

835befebf5 introduced both call expressions and new expressions. Call expressions have been removed but looks like new haven't.
2017-02-15 09:37:01 -05: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
Davy Duperron 1415040263 [WIP] Fix Flow DeclareTypeAlias (#669)
* Inject source text into printAstToDoc function.

* Add getNodeSource helper function.

* Fix Flow drawback with missing DeclareTypeAlias by checking the node source.

* Add new test.

* Fix getNodeSource helper function.

* Revert text injection.

* Refactor DeclareTypeAlias printing.

* Drop expensive getNodeSource helper function 🗑️.

* Refactor declareTypeAlias case in printer.

* Update tests.

* Implement the same logic for DeclareInterface.

* Update the tests.

* Fix missing semicolons and typo.

* Put Flow specific node detection code into own helper function 🚀.

* Refactor isFlowNodeStartingWithDeclare helper function.

* Simplify isFlowNodeStartingWithDeclare helper function.

* Rename test spec.

* Update tests.
2017-02-13 09:17:20 -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 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
Alex Rattray 91d4f400a4 [JSX] jsx-whitespace breaks with parent (fixes #622) (#626) 2017-02-07 13:47:34 -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
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 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 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
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 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 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 57c8c99e7b Fix syntax error in empty object with dangling comment (#533) 2017-01-31 06:52:05 -08:00
Christopher Chedeau cb347378c6 Fix range for object newline detection (#520)
It turns out that the range is not inclusive. It incorrectly included a \n after and would expand way more objects than we intended to.

I found it while running prettier on the codebase.

I'll make 0.14.1 for this.
2017-01-30 09:54:19 -08:00
Simon Lydell d27482e2fc Print numbers in a uniform way (#498)
* Print numbers in a uniform way

- Still preserve the radix (binary, octal, hexadecimal or decimal) used
  in the original source code.
- Still preserve scientific notation.
- Add 0 to fractions. `.1` -> `0.1`
- Remove trailing dots from integers. `1.` -> `1`
- Always print the radix letters lowercase. `0b`, `0o`, `0x`
- Always print scientific notation lowercase. `1e1`
- Always print hexadecimal digits uppercase. `0x123ABCDEF`
- Remove unneeded plus in scientific notation. `1e+1` -> `1e1`
- Remove unneeded zeroes in scientific notation. `1e-001` -> `1e-1`
- Preserve leading zeroes in non-decimal radix. This can be useful when
  working in binary, and having both `0b111000` and `0b000111` for
  example.

* Always print numbers lowercase

* Remove trailing dot in scientific notation

* Update snapshots
2017-01-30 09:36:23 -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 956779ec17 Preserve empty line after comment (#493)
The logic was already working, it was just special-cased to the first comment of the file! Presumably because the new line detection logic used to be broken ;)

I manually checked the first 10 snapshots and they are all legit, so I assume that all of them are.

Fixes #356
2017-01-27 14:05:41 -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
Christopher Chedeau b4f1649d1a Fix spurious whitespace (#463)
This was introduced by #314 where `line` should have been `softline`. By the way, I was going to propose renaming `line` to `line_or_space` and `softline` to `line_or_nothing` which should make it more explicit what is going on.

Fixes #461
2017-01-25 16:24:16 -05:00
Christopher Chedeau 3375cfe605 Move tests around (#454)
* new_tests

* move_all_clobbered_tests

* remove all the tests that no longer exist

* re-run flow tests

* Move all the flow tests to tests/flow and prettier to tests/

* Move prettier tests to their own folders

* Add jsfmt files

* run prettier snapshot tests
2017-01-24 14:35:37 -08:00