Commit Graph

749 Commits (850d51a03375dd0f2e85d94651e7560cb437e17f)

Author SHA1 Message Date
Christopher Chedeau 850d51a033 Tweak name 2017-06-27 17:30:13 -07:00
Joseph Frazier b47c0b4e04 Allow formatting ranges of JSON (#2298)
* Add test for https://github.com/prettier/prettier/issues/2297

* Add JSON types to isSourceElement()

See https://github.com/prettier/prettier/issues/2297

* Avoid expanding format range when possible

This fixes https://github.com/prettier/prettier/issues/2297

* Move JSON range test into tests/range_json

This is so that the AST_COMPARE=1 tests don't try to parse JSON with Flow.

* Check parser in isSourceElement()

Otherwise JSON formatting would format weird things inside of JS.
See https://github.com/prettier/prettier/pull/2298#discussion_r124407056

* Use arrow functions instead of .bind()

See https://github.com/prettier/prettier/pull/2298#discussion_r124409783

* Add test of range-formatting JSON identifier

See https://github.com/prettier/prettier/pull/2298/files#r124410750

* Allow range-formatting JSON identifier

See https://github.com/prettier/prettier/pull/2298/files#r124410750

* Fix lint
2017-06-27 16:04:42 -07:00
Joseph Frazier 5f063ab168 Fix locEnd() for postcss parser (#2295)
* Fix locEnd() for postcss parser

`node.source` doesn't have an `end`, but `node.nodes[-1].source` does.

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

* Add regression test for https://github.com/prettier/prettier/issues/2267

See https://github.com/prettier/prettier/pull/2295#issuecomment-311484343

* Make locEnd() postcss fix more focused

This passes the tests again.

* Use getLast() instead of .slice()

See https://github.com/prettier/prettier/pull/2295#discussion_r124398553

* Move CSS range test into tests/range_css

This is so that the AST_COMPARE=1 tests don't try to parse CSS with Flow.
2017-06-27 15:12:22 -07:00
Christopher Chedeau 6f4eade422 Go back to babylon beta 13 (#2289)
Fixes #2286
2017-06-27 11:51:23 -07:00
Christopher Chedeau 073a2071a8 Inline import('x') to avoid having trailing , (#2288)
Fixes #2287
2017-06-27 11:40:20 -07:00
Christopher Chedeau 803b7ff6b9 Add \n at the end of .graphql files (#2285)
We need to use the same hack as CSS unfortunately.

Fixes #2275
2017-06-27 09:49:15 -07:00
Christopher Chedeau e707bdd52f Print comments attached to {' '} (#2283)
We had two occurrences on fb codebase where people attached comments on `{' '}` which would be swallowed. Instead of dropping the comments, let's make the algorithm a bit less naive.
2017-06-27 09:35:03 -07:00
Karl O'Keeffe be600337eb Improve JSX output when there is a single expression (#2274) 2017-06-26 19:05:34 -07:00
Karl O'Keeffe 95a5f11be9 Tweak to support FB translation pipeline (#2272)
This also improves formatting when there are consecutive tag/expression elements with no white space between them.
2017-06-26 09:30:52 -07:00
Karl O'Keeffe fe7fb260a4 Fix a case where significant whitespace was dropped (#2270) 2017-06-26 03:03:13 -07:00
Cyril Junod 400f34624f Add module, class and typed nodes to sourceElements (#2266)
babylon flow typescript parsers

+ tests to cover module and class
2017-06-25 09:07:52 -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
Christopher Chedeau f71fcd0b65 Fix variance location (#2261)
Turns out when we refactored for TS, we moved it before "static" and it no longer parses. Oops.
2017-06-25 11:46:29 +10:00
Christopher Chedeau 38d4661ba9 Forward originalText in multiparser (#2260)
If we don't, all the places where we read from the original text (mostly comments) are reading into random places.

Fixes #2258
2017-06-24 17:54:09 -07:00
Christopher Chedeau 8def8e13b0 Add caching for printing (#2259)
* Add caching for printing

For printing the last argument expansion, we need to print the same node a bit differently with a flag. It's not easy to re-print just the node that is different so we end up printing all the sub-tree twice. Since it is often nested, it means that we run into an exponential complexity.

I finally found a simple solution: we can use a Map to store the nodes and their printed values. It is really cheap to do so (I can't notice a time difference with or without on normal code) and allows us to stop going through the two sub-trees in the bad case.

Fixes #1250

* Update printer.js
2017-06-24 14:01:02 -07:00
Christopher Chedeau 6663bab8a3 Support trailing commas for function calls (#2257) 2017-06-24 12:23:34 -07:00
Christopher Chedeau cc85769231 Add support for --trailing-comma for GraphQL (#2254)
Fixes #2251
2017-06-24 11:20:45 -07:00
Christopher Chedeau 59b3a9bc56 Add support for --no-bracket-spacing for GraphQL (#2255)
Fixes #2252
2017-06-24 11:06:56 -07:00
Christopher Chedeau 2f96357884 Attach comments in multiparser (#2253)
Otherwise it doesn't print comments :(
2017-06-24 11:06:50 -07:00
Joseph Frazier 4b955a14c4 Return original input when specified range is unformattable (#2250)
* Add failing test for range-formatting whitespace

See https://github.com/prettier/prettier/issues/2247#issue-238322065

* Return original input when specified range is unformattable

Fixes https://github.com/prettier/prettier/issues/2247
2017-06-24 09:41:10 -07:00
Cyril Junod b0413fc1fe Range Formatter: Remove Program/File from valid Ancestor (#2248)
* Remove Program/File from valid Ancestor

Fix edge range formatting entire file.

* Fix lint with `yarn lint -- --fix`

See https://travis-ci.org/prettier/prettier/jobs/246569943#L242-L260
2017-06-24 12:30:27 -04:00
Lucas Azzola 3474056b36 fix(typescript): handle ObjectPattern instead of ObjectExpression inside BinaryExpression, fixes #2237 (#2238) 2017-06-23 07:53:42 -07: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 39954f7951 Handle conditions inside of a ternary (#2228)
Fixes #2122
2017-06-21 18:17:30 -07:00
Christopher Chedeau ec1b672bbf Don't indent + in object value (#2227)
This is a "regression" from when we started using the same heuristic for `=` and `:`

Fixes #2129
2017-06-21 15:58:12 -07:00
Christopher Chedeau c36dd48339 First try scss when there's an @import with comma (#2225)
It's annoying that the less parser is so lenient that it drops things instead of throwing :(

Fixes #2125
2017-06-21 15:45:55 -07:00
Christopher Chedeau aac0d1dda0 Support inline template for new calls (#2222)
I merged the implementation of new and call expressions in this PR.

Fixes #2215
2017-06-21 15:45:44 -07:00
Joseph Frazier 63c47c85e0 JSON: Print trailing newline (#2226)
* JSON: Append trailing newline

This partially addresses https://github.com/prettier/prettier/issues/2223

* Dedupe printing of JSON trailiing newline

* JSON: Add tests for newlines after boolean/null/number/string

* JSON: Fix tests for newlines after boolean/null/number/string, simplify code

This fixes https://github.com/prettier/prettier/issues/2223
2017-06-21 15:40:19 -07:00
Christopher Chedeau 08060e7c94 Use fill for CSS values (#2224)
This looks less weird to have them flow as they fit than breaking on every single line.

Fixes #2213
2017-06-21 15:13:51 -07:00
Karl O'Keeffe 04c41f6758 Fix up empty JSX element detection (#2220) 2017-06-21 13:48:02 -07:00
Christopher Chedeau 313519d8ae Allow long media rules to break (#2219)
Fixes #2198
2017-06-21 13:02:16 -07:00
Brian Ng 7cef77433a Bump babylon version (#2205) 2017-06-20 08:36:14 -07:00
Lucas Azzola 43521565e7 CSS: lowercase hex colors, closes #2139 (#2203) 2017-06-20 23:07:31 +10:00
suǝʞǝǝpʇ 16db5971f0 Fix css-modules composes breaking with long line width (#2190)
* fix(css): css-modules composes breaking

* refactor(css): to use removeLines in case of composes
2017-06-18 23:41:10 -10:00
Jon Wong 1511609c01 Support for printing GraphQL comments (#2187)
* save point

* prettier

* Reverting change to check SOF and EOF

* Removing console.log

* Fixing grouping

* Adding comment output

* Basic comments

* Printing comments

* Getting comments to work

* Adding in basic handling of graphql comments

* Removing parser normalization (i assume it'll be added with language support)

* Lint error

* Reverting special cases

* Dealing with off-by-one error
2017-06-18 07:37:52 -10:00
Joseph Frazier 69808cebfb Format JSON literals like object literals (#2181)
* Format JSON literals like object literals

Following up on commit a42db30 (https://github.com/prettier/prettier/issues/2173),
this change makes it so that literals in JSON files are formatted as
they would be in an object literal. This normalizes trailing zeroes,
exponential notation, unnecessarily escaped characters, etc.

It works by using babylon to parse literals as expressions, and putting
the result in the AST instead of a `json-literal` node. Since JSON
string literals have to be double-quoted, there's a new
`__prettier__isJson` flag on each node, which is used by the printer to
force double-quotes.

* json: Check options.parser when formatting StringLiteral

This is instead of having a custom AST property. See
https://github.com/prettier/prettier/pull/2181#discussion_r122578251

* multiparser: Use babylon.parseExpression

See https://github.com/prettier/prettier/pull/2181#discussion_r122578354

* json: Don't split result AST node into two

See https://github.com/prettier/prettier/pull/2181#discussion_r122578499

* json: Don't use babylon parser to build expression nodes

See https://github.com/prettier/prettier/pull/2181#discussion_r122579052

* Revert "multiparser: Use babylon.parseExpression"

This reverts commit 3d7a4362880d39fac0cea88b5d0186a29538cf6f.

* Remove unused parameter from babylon parser

* json: Remove unused constructorTypes
2017-06-17 20:49:22 -04:00
Joseph Frazier a42db30946 Add JSON support (#2173)
* Add JSON support

This fixes https://github.com/prettier/prettier/issues/1973 by using
[json-to-ast] to parse JSON, then converting the AST into a
mostly-Babylon AST, so as to piggyback on the existing printer logic.

Identifiers and literals are currently printed verbatim from the input,
but this could be improved upon later (at least for literals, not sure
about identifiers).

[json-to-ast]: https://github.com/vtrushin/json-to-ast

* Rename `json-to-ast` parser to `json`

See https://github.com/prettier/prettier/pull/2173#discussion_r122574644

* json: Use createError to have a pretty error

See https://github.com/prettier/prettier/pull/2173#discussion_r122574748

* json: Transform AST without mutations

See https://github.com/prettier/prettier/pull/2173#discussion_r122574690
2017-06-17 10:36:42 -10:00
Lucas Azzola bff28023b3 Comments: attach method name comments with estree parsers, fixes #2141 (#2170) 2017-06-17 22:22:39 +10:00
Lucas Azzola c669a1db72 TypeScript: group function params in call/construct signatures, fixes #2163 (#2169) 2017-06-17 12:09:18 +10:00
Stephen Wan a005211c96 Fix extra semicolon in ambient typescript declaration emit (#2165) (#2167)
* fix(typescript): add failing ambient declaration test

* fix(typescript): fix failing test case for ambient function exports
2017-06-16 08:20:59 -10:00
Lucas Azzola 7e60d70f97 fix(typescript): always break interfaces onto multiple lines, fixes #2159 (#2161) 2017-06-15 14:34:54 -10:00
Lucas Azzola 9e7b02d50e Print {} in import if it's in the source, fixes #2145 (#2150) 2017-06-15 20:19:01 +10:00
Lucas Azzola a2328edced TypeScript: keep parens around with yield/await non-null assertion, fixes #2137 (#2149) 2017-06-15 09:34:12 +10:00
Joseph Frazier 48bbd9cb6b Don't insert a cursor placeholder for a value-less path (#2136)
* Test that no cursor placeholder is inserted for a value-less path

This was taken from https://github.com/prettier/prettier/issues/2131

* Don't insert a cursor placeholder for a value-less path

This fixes https://github.com/prettier/prettier/issues/2131
2017-06-14 09:05:06 -07:00
Lucas Azzola b7e1c366e0 Vue: pretty-print binding syntax (#2108)
* feat(vue): pretty-print :class attributes

* feat(vue): switch to brand new custom parser API! 🎉

* refactor(vue): move AST manipulation to post-parse
2017-06-14 22:33:15 +10:00
James Henry 9b772cb4fd Add test for comment in TypeScript type parameters (#2132) 2017-06-14 20:10:03 +10:00
Christopher Chedeau abfdbe1896 Upgrade typescript to 2.4 (#2120)
Fixes #2117
2017-06-13 09:47:40 -07:00
Rodrigo Pombo 8dd0cb2a05 Format CSS in template literals with expressions (#2102)
* Add support for styled-jsx with expressions

* Lint

* Fix require for Node 4

* Re-create template-literal document when replacing placeholders

* Add support for styled-components with expressions

* Fix merge

* Move css library detection to functions
2017-06-13 09:45:16 -07:00
adanoff 44b5da8ade Add `graphql.experimental` parsing for react-relay (#2116) 2017-06-12 17:16:17 -07:00
Christopher Chedeau 62b9e29af5 Fix comments in non strict mode (#2114)
It turns out that by returning there, we didn't go through the line `delete ast.tokens` and the comments attachment logic would attach it to a random token. It's great that this fail-safe caught this bug!

Fixes #2111
2017-06-12 09:50:51 -07:00