Commit Graph

95 Commits (bafa76f6131ec3e2a61e18a971336f73908f42e2)

Author SHA1 Message Date
Adam Pearce 09ac476a32 Nicer line wrapping for d3 (#4285)
* d3 counts as a constructor

* adds test

* Tweak wording

* docs: fix linting

* updates test

* no $ special case

* renames function

* shorter function name

* removes trailing white space
2018-04-24 06:54:45 -04:00
Lucas Duailibe 57bc700602
Stop marking all comments in named exports as leading comments (#4292) 2018-04-10 17:05:49 -03:00
Lucas Duailibe f3626611a3
Handle ContinueStatement and BreakStatement comments (#4279) 2018-04-07 13:38:37 -03:00
Lucas Duailibe 88489a9afe Handle comments before `else` 2018-04-05 16:06:18 -03:00
Lucas Duailibe 4d23e9081d
Print flow type annotation comments as comments (#3449) 2017-12-15 05:11:48 -02:00
Lucas Duailibe 2c5a792d9a fix(jsx): Break opening elements with single text attribute if there's a comment (#3154)
* fix(jsx): Break opening elements with single text attribute if there's a comment

* Single text attribute with a comment on the JSXElement name

* Consolidate rules of comments in JSX
2017-11-07 08:05:59 +11:00
Lucas Duailibe 38b15f7899 Improve template literal expresions line breaks (#3124) 2017-10-31 14:31:27 -06:00
Lucas Azzola c0aaa0fa52 Don't print JSX bracket on same line when it has trailing comments (#3088) 2017-10-23 17:13:12 +11:00
Marshall Bowers 445f51709e Fix indentation for JSDoc comments (#2470)
* Fix TypeScript comment snapshot

* Add failing test for TypeScript method comments

* Remove trailing space in comment

* Add snapshot for comment with many lines

* Add TODO marking potential location for comment re-indentation

* Update snapshot

* Indent comments based on the parent node

* Only adjust indentation if the comment looks like a JSDoc comment

* Only strip leading spaces when adjusting comment indentation

* Update snapshot

* Add test to show incorrect indentation based on parent node

* Update snapshot

* Update snapshot

* Update comment indentation to not rely on the parent node

* Extract code for getting the indent size

* Add test where the parent indentation changes as well

* Fix lint warning regarding lexical declarations within case block

* Update lockfile

* Update snapshot

* Add test case for block comment inside of a method

* Replace spread operator with `concat`

* Use `align` instead of `addAlignmentToDoc`

* Add test for mismatched indentation within JSDoc comment

* Always add a leading space to subsequent lines in a JSDoc comment

* Refactor JSDoc comment printing into a separate function

* Remove unneeded `align`

* Replace `forEach` with `map` for better style

* Only `trimLeft` when the end comment token is on the last line of the comment

* Remove unneeded `docBuilders`

* Add tests for JSDoc comments in JSX

* Update snapshots
2017-10-08 20:34:24 +11:00
Lucas Azzola 23f7e92162 Handle comments between function name and open paren (#2979) 2017-10-06 20:07:58 +11:00
jackyho112 302de60bdc Fix comment style when placed after if statement test (#2675)
* Fix comment style when placed after the test of an if statement

* Add tests

* Do a bit of refactoring

* Fix lint errors

* Handle skipping comments

* Account for multiple-line comments

* Add more tests

* Change function name

* Add more tests

* Refactor

* Add some comments

* Do away with getPreviousNonSpaceNonCommentCharacter

* Improve comment and code on handling if statement comments

* Refactor

* Fix a bug
2017-09-08 10:12:10 -07:00
Lucas Duailibe 15989df6e1 Group last argument if it's an empty object with a comment (#2758) 2017-09-05 12:22:25 -07:00
Roman Kurbatov 01387bc9d1 fixes dynamic imports (#2748) 2017-09-05 11:57:36 +10:00
Christopher Chedeau f7bd74ed98 Fix fatal with empty jsx (#2663) 2017-08-24 18:02:44 -07:00
Yang Su 26842e4d69 Handle closure compiler type cast syntax correctly (#2484)
* Handle closure compiler type cast syntax correctly

Fixes https://github.com/prettier/prettier/issues/1445

* Move closure type cast detection to needParens in fast-path.js

* every => some and added additional check for leading comment
2017-07-25 11:21:25 +10:00
Lucas Azzola 00d9aa208e Trim trailing whitespace from comments, closes #2422 (#2494) 2017-07-16 17:45:27 +10:00
Karl O'Keeffe 5e76ea7bdd Allow single expressions in JSX to be inline 2017-07-10 20:02:30 +01:00
Kevin Gibbons 28b3a4b925 Break sequence expressions (#2388)
* Add SequenceExpression tests

* Clean up printing of SequenceExpressions

* Remove test and lint
2017-07-04 22:50:57 +02:00
Karl O'Keeffe be600337eb Improve JSX output when there is a single expression (#2274) 2017-06-26 19:05:34 -07:00
Christopher Chedeau 9a55f5b2d6 Fix unstable arrow comments (#2262)
It outputted which is completely wrong

```js
<FlatList
  renderItem={info => <span>{info.item.widget.missingProp // $FlowExpectedError - bad widgetCount type 6, should be Object
    }</span>}
/>;
```
2017-06-25 12:30:09 +10:00
Karl O'Keeffe 0cc0ebc3bc Allow JSX lines to be recombined (#1831)
* Allow JSX lines to be recombined

* Add test case for `<br />` breaking expression

* Fix issue where JSX whitespace could be dropped

* Newlines were incorrectly being preserved when JSX contained text
2017-06-22 09:19:44 -07:00
Christopher Chedeau 5a9bea1c2f Fix comments inside of template literals (#1713)
We completely butcher comments inside of template literals. The fix is to not be able to attach comments to TemplateExpression nodes and to avoid comments that are ine one expression to be moved to another expression. We now have the boundary to prevent outputting invalid code.

Fixes #1617
2017-05-24 13:19:56 -07:00
Konstantin Pschera 54ad598acc Move Comments to the end in TemplateLiteral (#1598)
Closes #1589
2017-05-12 10:04:22 -07:00
Joseph Frazier d7ec9dc706 Don't let trailing template literal comments escape (#1580)
* Don't let trailing template literal comments escape

This might not be the best-looking solution, but it's a start, and it
passes `AST_COMPARE=1 npm test`

Fixes https://github.com/prettier/prettier/issues/1559

* Add trailing space when changing line comment to block

* Be more discerning about template literal comment placement

Try to maintain the previous behavior if possible.

* Revert "Add trailing space when changing line comment to block"

This reverts commit 1eb42c24819a296c93a79b92a358d30a2aacc16c.
2017-05-10 21:07:55 -07:00
Christopher Chedeau c63e21b52b Do not put parens for single argument with end of line comment (#1518)
Fixes #1517
2017-05-05 21:44:42 -07:00
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