* WIP immediate feedback
* typescript parser is drop-in replacement for flow parser
* Add new TypeScript Parser snapshots where drop-in replacement possible
* Snapshot updates after rebasing
* Remove unnecessary stripping of properties on TypeScript parser AST
* Remove annotated issues
* Move TS dependencies to dev for now
* 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 🚀.
* 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.
* Add new helper function to convert comments as blocks in ExportNamedDeclaration.
* Add new test.
* Switch to a leading position.
* Update test accordingly.
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
This happens very frequently that naming a test makes the entire line go > 80 columns and requires to indent everything which takes a lot more space. We've had this being reported multiple times and it also affects a lot of tests inside of fb.
Fixes#159
(Note, this is built on-top of #841 but github doesn't handle stacked pull requests well...)
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
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
In #605 I restricted it to binary operations as I didn't know how it would affect boolean operations but it turns out the same technique solves problems that people are reporting. So doesn't make sense to restrict it.
Fixes#824
It turns out that my fix was not correct. We should not add parenthesis for FunctionDeclaration instead of checking if the function expression is named.
Fixes#819
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
Since this is extremely rare, I just took the easy way out and if you are adding a comment inside the "as" node, I just move it outside. We could be more fancy but that works.
Fixes#620
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#303Fixes#752
I'm not really sure what a general rule is for those, but starting with LogicalExpressions should be good. Outside of identifiers, function calls and logical expressions, there aren't a lot of things you'd put there in real code anyway.
Fixes#822
Instead of trying to figure out a complicated way to preserve their correct position, it's easier to just migrate those comments before the class.
Fixes#693Fixes#694
Printing a merged group indented was actually not the right fix. The right fix was to print them in a single line. It used to have this behavior when I was mutating the first group but now that I don't anymore I need to reproduce this condition.
Fixes#823
We need to add parenthesis around function expressions if they are named otherwise the name leak, but if the function is not named then it's just superfluous.
The conditional change is due to a bad use of switch case where it would fall through the next one. We don't want parenthesis there.
* 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.
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
The idea is that if you reach the end of the `}` inside of a template literal, we have to flush the trailing comma, otherwise it would generate invalid code. We also need the same special case for JSX.
I don't like adding yet another type of document but it seems like the most elegant way to solve the problem.
Fixes#623
* 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.
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
In addition to Observable, $ or _, we should also add `this` to the list of things that are likely factory and not objects you want to apply things on directly.
I added a way to make the function call break on its own line when it doesn't fit in one line and indent. But I think that only having the indentation is enough, having the call on its own line feels weird.
Fixes#777
Previously we would blindly put leading comments of the next expression, but we didn't account for a real trailing comment. By checking if the leadingNode is on the same line, we can correctly put it there when needed
Fixes#685
* 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.
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
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.
I've started the discussion around it but I think that it was a mistake. It feels weird to add parenthesis on object values sometimes but not others. I think that it's best to let prettier do its work on staying under 80 columns based on the rules we added.
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.
This was surprisingly easy to write!
Technically, it doesn't ignore the next line but the next expression or block but I'm guessing that people are already used to `// eslint-disable-next-line` so it's going to be an easier learning curve.
Fixes#120
* 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.
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
* 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.
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
* 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