Commit Graph

875 Commits (db0f2ee428fd521085b0c8b9883f0105ddf13450)

Author SHA1 Message Date
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
Lucas Azzola 486a89bfdc fix(typescript): workaround - don't wrap directives in parens, fixes #2074 (#2107) 2017-06-11 19:10:56 -07:00
Lucas Azzola e8f12cf621 TypeScript: Remove parens from type annotations where possible (#2098)
* fix(typescript): remove parens from union types where possible, fixes #1885

* fix(typescript): ensure parenthesis removal is stable

* fix(typescript): don't strip parens around function type
2017-06-11 18:06:47 -07:00
Lucas Azzola 9c608d2ec1 fix(typescript): use printFunctionParams for call/construct signature, fixes #2105 (#2106) 2017-06-11 16:06:38 -07:00
Lucas Azzola ec7d99439d fix(typescript): print arrow function type params on same line as params, fixes #2099 (#2101) 2017-06-11 07:30:51 -07:00
Lucas Azzola 4395e09b78 fix(import): allow break with both default named import, fixes #2050 (#2096) 2017-06-10 21:55:21 -07:00
Lucas Azzola a79a444bac fix(variable-declarator): indent first variable in one-var scenario, fixes #2072 (#2095) 2017-06-10 21:19:50 -07:00
Lucas Azzola fdb7467de7 feat(html): add support for HTML fragments (#2093) 2017-06-10 20:10:34 -07:00
Lucas Azzola b4f0ff346e feat(mutliparser): add react-relay and graphql-tag parsing (#2092) 2017-06-10 08:59:27 -07:00
Lucas Azzola d1b94c540c Implement parser switching (HTML, Vue, styled-components) (#2086)
* feat(multiparser): implement switching from html -> css,js,ts

* feat(multiparser): use quasi value instead of originalText
2017-06-10 08:03:39 -07:00
Lucas Azzola 77f0c05d2a fix(method-chain): break on non-literal computed member expression, fixes #2075 (#2087) 2017-06-10 07:47:22 -07:00
Lucas Azzola 1e08f03554 fix(typescript): break on multiple interface extends, fixes #2043 (#2085) 2017-06-10 07:46:59 -07:00
Lucas Azzola f7819d5695 CSS: implement prettier-ignore (#2089)
* fix(css): implement prettier-ignore, fixes #2007

* refactor(css): move prettier-ignore to printNodeSequence
2017-06-10 07:45:59 -07:00
Lucas Azzola a9409f11e7 fix(css): print @else on same line as }, fixes #2053 (#2088) 2017-06-10 07:40:05 -07:00
Lucas Azzola d00956d51d feat(html): add parse5/htmlparser2 printer (#2083) 2017-06-09 19:22:59 -07:00
Christopher Chedeau f399e90bc2 Preserve lines after directives (#2070)
Looks like we were only doing it at the top level, we should do it everywhere.

Fixes #2067
2017-06-08 17:10:01 -07:00
Christopher Chedeau 939734b75e Use ifBreak instead of shouldBreak (#2063)
shouldBreak is only a heuristic to enforce a break, if it breaks "naturally", then it won't be applied.

Fixes #2055
2017-06-08 11:47:49 -07:00
Tim Griesser df26cc05cc Support graphql ListType (#2061) 2017-06-08 09:34:35 -07:00
Christopher Chedeau 883e637fa9 Support the Keyword type (#2056)
This is an issue in https://github.com/eslint/typescript-eslint-parser/issues/315

Fixes #2049
2017-06-08 09:25:28 -07:00
Tim Griesser e9045e4bcf Support graphql NonNullType (#2060) 2017-06-08 09:20:41 -07:00
Pier Paolo Ramon 126727380d CSS: Every selector is now printed in its own line (#2047)
Changes the behaviour for line breaking inside selector lists (those
separated by ',') so that every selector is in its own line.

Fixes #1962
2017-06-08 07:46:24 -07:00
Christopher Chedeau 924363193b Check if the next group has comments (#2040)
Fixes #1958
Fixes #1959
2017-06-07 15:20:11 -07:00
Christopher Chedeau 1edb1f261b Include typeAnnotation inside of node location (#2039)
We should remove this hack once https://github.com/eslint/typescript-eslint-parser/issues/314 lands

Fixes #1946
2017-06-07 15:05:46 -07:00
Christopher Chedeau 42b0368e3d Fix decorator comments for export class (#2038)
We need to do the same location trick for export class unfortunately.

Fixes #1956
2017-06-07 14:55:55 -07:00
Christopher Chedeau b730f4eccf Add group around TSMethodSignature (#2037)
This ensures that the comment size is not taken into account during measurement

Fixes #1976
2017-06-07 14:20:55 -07:00
Christopher Chedeau 09f147decd Do not throw for unprinted comments inside of JSX (#2036)
Fixes #1990
2017-06-07 12:57:03 -07:00
Christopher Chedeau ccda4c7115 Break decorator before content (#2032)
Fixes #1996
2017-06-07 12:40:47 -07:00
Christopher Chedeau e16478ef80 Fix less &:extends when parsed with scss (#2034)
This is the only breakage that people ever reported with CSS so I'm happy adding one edge case like this.

Fixes #1967
2017-06-07 12:40:36 -07:00
Christopher Chedeau cabae6d877 Fix parenthesis for call expression inside of member expression inside of new (#2035)
Fixes #2033
2017-06-07 12:40:27 -07:00
Christopher Chedeau c7cacf0529 Break if string | null (#2028)
We were a bit too aggressive around the `| null` heuristic.

Fixes #2002
2017-06-07 11:51:32 -07:00
Christopher Chedeau c9c8512170 Only prevent lone `set` if there's no type annotations (#2026)
If there are, it's not ambiguous.

Fixes #2023
2017-06-07 11:51:25 -07:00
Christopher Chedeau 5ba0b0ec87 Add semi for functions without body (#2025)
We don't need to be overly restrictive and should add the semi if there's no body

Fixes #2003
2017-06-07 11:51:17 -07:00
Alexandre BODIN 8ec1b40961 GraphQL: Add support for union types (#2014) 2017-06-07 10:33:46 -07:00
Sashko Stubailo 0583fd4ce6 Add alias and null value (#2017)
Add kitchen sink test case
2017-06-06 18:06:15 -07:00
Sashko Stubailo 88f962beee Add support for GraphQL directies (#2015) 2017-06-06 17:42:47 -07:00
Joseph Frazier 0cfd772fd2 Protect SwitchCase statements from ASI (#2008)
* Add no-semi test with SequenceExpression

Adapted from https://github.com/prettier/prettier/issues/2006

* Protect SequenceExpressions from ASI

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

* Add test for ASI protection of SequenceExpression outside SwitchCase

See https://github.com/prettier/prettier/pull/2008#issuecomment-306614217

* Revert "Protect SequenceExpressions from ASI"

This reverts commit ca644fad8072c90a977ad8a2f29674bc72f679e8.

* Protect SwitchCase statements from ASI

See https://github.com/prettier/prettier/pull/2008#issuecomment-306614217

Fixes https://github.com/prettier/prettier/issues/2006
2017-06-06 14:28:51 -07:00
Sashko Stubailo 7c051804a8 Add support for GraphQL object values (#2010) 2017-06-06 14:28:15 -07:00
Sashko Stubailo c441f87fc0 Add GraphQL fragment support (#2005) 2017-06-06 12:35:40 -07:00
Rodrigo Pombo d4774edd03 Parse CSS in template literals (styled-jsx) (#1984)
* Add basic styled-jsx parsing

* Use eval("require")

* Remove template literal raw and cooked values when it contains CSS

* Set postcss parser to options and group subtree parts

* Gracefully give up TemplateLiteral formatting when it fails
2017-06-06 08:28:13 -07:00
Sashko Stubailo 6d00644c78 Add GraphQL variable definition support (#1995) 2017-06-06 08:26:04 -07:00
Daniel Tschinder df7311a7f3 Add parens around all string literals at top level (#1999) 2017-06-06 08:24:13 -07:00
Sashko Stubailo c80e82c7d7 Add support for list values (#1994)
Run the linter
2017-06-05 23:45:15 -07:00
Christopher Chedeau c46a20c8c1 re-run tests 2017-06-05 22:26:35 -07:00
Sashko Stubailo df08dc4a56 Add support for arguments with soft line break (#1993) 2017-06-05 22:25:12 -07:00
Sashko Stubailo 8cb259eac7 GraphQL: Add support for printing operation names (#1992)
* Add support for printing GraphQL operation names

* Remove extra empty line at the end
2017-06-05 22:24:01 -07:00
Sashko Stubailo 76c5733b55 GraphQL: Add support for formatting fields in selection sets (#1991)
* GraphQL: Add support for formatting fields in selection sets

(Doesn't yet handle arguments or directives)

* Remove extra space
2017-06-05 22:23:03 -07:00
Joseph Frazier c863cbeac8 Translate cursor relative to nearest node, not SourceElement (#1989)
* Add test demonstrating SourceElement-relative cursor translation

See https://github.com/prettier/prettier/issues/1981#issuecomment-306326739

* Translate cursor relative to nearest node, not SourceElement

This partially address https://github.com/prettier/prettier/issues/1981

See https://github.com/prettier/prettier/issues/1981#issuecomment-306326739

* Add test demonstrating incorrect cursor translation

Since the `cursorOffset` option (introduced in #1637) works by tracking
the cursor position relative to an AST node (rather than a CST token),
it can produce incorrect results.

See https://github.com/prettier/prettier/issues/1981
2017-06-05 18:51:53 -07:00
Karl O'Keeffe 73432c2ce7 Position JSX whitespace (`{" "}`) at the end of lines (#1964)
* Position JSX whitespace at the end of lines

Currently we place JSX whitespace at the beginning of lines.

* Use `isLiteral` when determining JSX whitespace
2017-06-05 21:52:54 +01:00
Jon Wong e33d6773d6 Adding support for `graphql` (#1982)
* Initial GraphQL implementation

This is the bare minimum amount of code to make graphql work. Turns out it's pretty easy to add support for other languages :)

* Fixing support for `graphql` parsing

There was a few structural changes with the addition of CSS and Typescript that we had to take into account.

* Fixing file glob, adding `graphql` to yarn.lock

* Removing extraneous file

* Adding `graphql` parser, style changes

* Splitting out graphql printer

* Removing merge conflict

* Fixing yarn.lock

* Addressing code review, using `createError`

* Adding release config

* Using exact version

* Removing destructuring
2017-06-05 12:34:08 -07:00
Lucas Azzola 642ac7e230 fix(typescript): print semi in class index signatures, fixes #1960 (#1961) 2017-06-04 07:21:33 -07:00
Lucas Azzola 6697122e28 fix(decorators): do not inline methods with decorators with babylon, fixes #1933 (#1934) 2017-06-03 05:24:24 -07:00
Lucas Azzola 649cec7fdd TypeScript: fix trailing comma in enum (#1938)
* fix(typescript): fix trailing comma in enum, fixes #1932

* fix(typescript): use --trailing-comma es5 for enums
2017-06-03 05:20:36 -07:00
Lucas Azzola c57f2ab507 fix(typescript): no semi after export default abstract class, fixes #1926 (#1937) 2017-06-03 05:19:39 -07:00
Lucas Azzola 2ef169eb67 fix(typescript): print semi with inline interfaces/types, fixes #1930 (#1936) 2017-06-03 05:18:59 -07:00
Christopher Chedeau 2b6c1cd27e Update typescript-eslint (#1924)
This applies https://github.com/eslint/typescript-eslint-parser/pull/308 and https://github.com/eslint/typescript-eslint-parser/pull/303 to fix issues on prettier.

Fixes #1870
2017-06-02 16:19:15 -07:00
Christopher Chedeau dae7203be6 Fix As parenthesis for real (#1923)
Instead of doing it inside of object, we need to have them use the existing mechanism that checks if the left-most character is a paren.
2017-06-02 16:08:45 -07:00
Christopher Chedeau 3264da9cab Fix leading comment (#1920)
It turns out that leading comment is attached to the CallExpression which is the last one and not the first one if they fit in a single line, so we want to not check that one.

Fixes #1892
2017-06-02 15:50:43 -07:00
Christopher Chedeau db0be7fd5b fix conflict 2017-06-02 15:44:50 -07:00
Christopher Chedeau 323d64fb84 Port intersection inlining logic to TypeScript (#1919)
Fixes #1880
2017-06-02 15:40:33 -07:00
Christopher Chedeau 0d239477cb Use semi-colon for object separator (#1918)
Looks like the convention for typescript separator is `;` whereas for flow it's `,`. Let's migrate to that.

Fixes #1896
Fixes #1879
Fixes #1874
2017-06-02 15:32:51 -07:00
Christopher Chedeau 74f0d3979b Add parenthesis for yield and await inside of `as` (#1915)
Fixes #1913
2017-06-02 15:10:02 -07:00
Christopher Chedeau 2f9ea794db Always expand enums (#1914)
We do the same for classes, and it's usually written that way even if it fits in one line, even in the TS docs: https://www.typescriptlang.org/docs/handbook/enums.html

Fixes #1873
2017-06-02 15:09:55 -07:00
Christopher Chedeau a255977009 Do not make variable multilines inside of for loop (#1912)
Fixes #1876
2017-06-02 15:00:16 -07:00
Christopher Chedeau e58dd2bf0b Add semi for ts function declarations without body (#1911)
Fixes #1891
2017-06-02 14:49:37 -07:00
Christopher Chedeau 6933d16125 Inline decorators with no arguments (#1910)
Fixes #1898
2017-06-02 14:40:39 -07:00
Christopher Chedeau 0e35d3758a Fix <this.x /> (#1886)
Would be nice to get it properly handled in https://github.com/eslint/typescript-eslint-parser/issues/307

Fixes #1877
2017-06-02 14:27:27 -07:00
Christopher Chedeau 74f7f96fe4 Move accessibility before static (#1908)
I looked through all the places where we display accessibility and all of them now have a consistent way of printing all those modifiers.

Fixes #1877
2017-06-02 14:27:19 -07:00
Kevin Gibbons 4915e0228e Require semicolons before class props named 'in' or 'instanceof' (#1878) 2017-06-02 10:52:11 -07:00
Lucas Azzola 83c5253429 fix(typescript): look at source to determine export = statement (#1868) 2017-06-02 08:32:40 -07:00
Christopher Chedeau faf2ac5f9e Hack to get export default work (#1866)
Fixes #1854
2017-06-02 08:12:03 -07:00
Lucas Azzola 38eec13a03 TypeScript: temp fix for missing readonly keyword (#1865)
* fix(typescript): temp fix for missing readonly keyword, fixes #1845

* fix(typescript): use non-master commit from tsep
2017-06-02 08:09:24 -07:00
Lucas Azzola 8511e798dc feat(decorators): inline more types of decorators (#1864) 2017-06-02 07:38:44 -07:00
Lucas Azzola 9ed94c77ab fix(typescript): do not print trailing comma in TS tuple type, fixes #1858 (#1863) 2017-06-02 07:16:41 -07:00
Lucas Azzola ede078c0d3 fix(typescript): print static before accessibility, fixes #1855 (#1862) 2017-06-02 07:16:11 -07:00
Christopher Chedeau bc4075ac65 Add parenthesis around object inside of as (#1852)
Fixes #1851
2017-06-01 21:10:05 -07:00
Christopher Chedeau 3f8c4d6974 Fix yet another form of class with the same ast (#1849)
Fixes #1847
2017-06-01 19:02:15 -07:00
Joseph Frazier 4bfeb9064d Add `cursorOffset` option for cursor translation (#1637)
* Add `formatWithCursor` API with `cursorOffset` option

This addresses https://github.com/prettier/prettier/issues/93 by
adding a new option, `cursorOffset`, that tells prettier to determine
the location of the cursor after the code has been formatted. This is
accessible through the API via a new function, `formatWithCursor`, which
returns a `{formatted: string, cursorOffset: ?number}`.

Here's a usage example:

```js
require("prettier").formatWithCursor(" 1", { cursorOffset: 2 });
// -> { formatted: '1;\n', cursorOffset: 1 }
```

* Add `--cursor-offset` CLI option

It will print out the offset instead of the formatted output. This
makes it easier to test. For example:

    echo ' 1' | prettier --stdin --cursor-offset 2
    # prints 1

* Add basic test of cursor translation

* Document `cursorOffset` option and `formatWithCursor()`

* Print translated cursor offset to stderr when --cursor-offset is given

This lets us continue to print the formatted code, while also
communicating the updated cursor position.

See https://github.com/prettier/prettier/pull/1637#discussion_r119735496

* doc-print cursor placeholder in comments.printComments()

See https://github.com/prettier/prettier/pull/1637#discussion_r119735149

* Compare array index to -1 instead of >= 0 to determine element presence

See https://github.com/prettier/prettier/pull/1637#discussion_r119736623

* Return {formatted, cursor} from printDocToString() instead of mutating options

See https://github.com/prettier/prettier/pull/1637#discussion_r119737354
2017-06-01 15:52:29 -07:00
Karl O'Keeffe f4ef1c2c97 JSX whitespace on the same line as text (#1829)
This tweaks our JSX formatting to only put a JSX whitespace `{" "}` on a line by itself when it comes before or after a multiline element.

When preceding a text or single line element it appear on the same line as that element.
2017-05-31 10:16:50 -07:00
Karl O'Keeffe a680cd8534 Fix unstable JSX output by ensuring we follow `fill` rules. (#1827)
* Fix unstable JSX output by ensuring we follow `fill` rules.

This changes makes us more strict about ensuring our JSX children follow the alternating content/whitespace format expected by the `fill` primitive.

Previously there were some cases where could get out of sync which would throw out the formatting.

* Simplify whitespace wrangling

# Conflicts:
#	src/printer.js
2017-05-31 09:42:36 -07:00
Christopher Chedeau fa708d102a Remove trailing commas for generics in TypeScript (#1832)
Fixes #1820
2017-05-31 09:22:00 -07:00
Christopher Chedeau 3097f9b2fe Do not indent nested ternaries (#1822)
This avoids making it seems like it is indented by 4 characters instead of two. The downside is that if the condition is multi-line it's not going to be properly aligned, but I feel it's a better trade-offs. If you are doing nested ternaries, you usually have small conditions.
2017-05-30 10:08:06 -07:00
Christopher Chedeau 73ff0ef9ea Add group around implements (#1821)
This shouldn't break if it fits in one line.

Fixes #1818
2017-05-30 09:45:07 -07:00
Joseph Frazier ea27846d1f Don't force line break in empty loop bodies (#1815)
This makes them consistent with empty function bodies.

Fixes https://github.com/prettier/prettier/issues/1356
2017-05-30 09:39:10 -07:00
Christopher Chedeau eeb8616e3c Add parenthesis for single arg types with generics (#1814)
Fixes #1813
2017-05-29 16:59:45 -07:00
Christopher Chedeau 2ab3d6d6ac Workaround bug with comment ending in */ (#1810)
Reported in the original repo: https://github.com/postcss/postcss-scss/issues/64
2017-05-29 13:05:20 -07:00
Christopher Chedeau 59e4e83355 Break before combinators and indent (#1809) 2017-05-29 12:55:14 -07:00
Christopher Chedeau f34492d7e5 Strip leading space for first combinators (#1807) 2017-05-29 12:49:25 -07:00
Christopher Chedeau 27df0f81f5 Preserve space before parens (#1806) 2017-05-29 12:41:04 -07:00
Christopher Chedeau dc287d32a5 Remove extra newline from empty @media (#1802) 2017-05-29 08:52:24 -07:00
Christopher Chedeau c4e32f4c78 Remove double spaces in @font-face (#1801) 2017-05-29 08:49:51 -07:00
Christopher Chedeau 873123223f Fix empty urls (#1800) 2017-05-29 08:43:19 -07:00
Christopher Chedeau 6a9e053221 Workaround a bug where the comment location is off for scss (#1799)
Workarounds https://github.com/postcss/postcss-scss/issues/63
2017-05-29 08:39:41 -07:00
Christopher Chedeau bf5f9cc05d Add support for media-url (#1797) 2017-05-29 08:20:03 -07:00
Lucas Azzola c9159f7862 Break on Property with long value and Literal key (#1786)
* fix(object-expression): break on object literal with long value

* fix(object-expression): add isStringLiteral check
2017-05-29 07:31:24 -07:00
Christopher Chedeau 310fba57cc Add a test with all the less docs and fix issues (#1792)
The docs go over a bunch of edge cases, might as well have it as a test :)

http://lesscss.org/features/

I just had to remove

```css
.weird-element {
  content: ^//* some horrible but needed css hack;
}
```

but i'm not sure if it's real less.
2017-05-28 16:05:26 -07:00
Christopher Chedeau 5a828bedd1 Update eslint-typescript again (#1791)
Now fixes more issues ;)
2017-05-28 16:05:07 -07:00
Christopher Chedeau 97775dc94c Add support for SCSS (#1790)
We use a heuristic to figure out if it's a SCSS or Less file. And if it doesn't work, we try again with the other one. We do the same for JSX and TypeScript.

Fixes #1784
2017-05-28 15:01:47 -07:00
Christopher Chedeau 882b712c5f Update typescript-eslint (#1787)
It improves a few things.
2017-05-28 13:18:29 -07:00
Lucas Azzola e270032d37 chore(typescript): upgrade typescript-eslint-parser to 992f1fa (fixes #1769) (#1785) 2017-05-28 08:05:33 -07:00
Lucas Azzola 2f840f940f test(typescript): disable failing tests (#1782) 2017-05-27 18:22:32 -07:00
Lucas Azzola 2a58e6bc06 test(typescript): enable typescript-verify on most tests (#1781) 2017-05-27 17:57:07 -07:00
Christopher Chedeau 383aaca0e4 Fix decorators location (#1779)
Babylon has a bug (I guess) with locations for classes where decorators are involved. Instead of the class starting at the first decorator, it starts at the beginning of the `class` keyword. By moving the location to the first comment, it solves --some-- of the issues with decorator comments.
2017-05-27 14:51:00 -07:00
Christopher Chedeau 37eda1f17a Only inline urls with a single argument (#1778) 2017-05-27 14:09:40 -07:00
Christopher Chedeau 6f72e278e9 Do not break before `+` (#1777)
We really want to parse those as binary expressions but it's not going to be trivial. A quick fix that should help is to not break before those.
2017-05-27 13:41:19 -07:00
Christopher Chedeau 375540efac Remove many spaces from media query (#1776)
The issue is really that the media query parser fails to parse the inner queries and just gives a raw string for the expression, but it should be safe to remove extra spaces. I can't make it rmeove spaces inside () that way unfortunately :(
2017-05-27 13:32:27 -07:00
Christopher Chedeau 48d35503c4 Remove space around media `and` (#1775) 2017-05-27 13:24:08 -07:00
Christopher Chedeau adb26e20ad Add space around ~ combinator (#1774) 2017-05-27 13:20:32 -07:00
Christopher Chedeau 54497a0865 Skip extra `;` (#1773) 2017-05-27 13:20:19 -07:00
Christopher Chedeau bb0c184855 Fix broken tests
If you put a space, `{loose: true}` is going to parse them as word + paren instead of func. It doesn't impact correctness and i'm not really sure how to clean the ast, so let's just make the test pass. I haven't seen this anywhere in real code.
2017-05-27 13:16:11 -07:00
Christopher Chedeau 3c110b0230 Indent groups (#1772)
This way it looks less weird when they break
2017-05-27 13:01:38 -07:00
Christopher Chedeau f3823276eb Enable loose mode for postcss-values-parser (#1766)
Fixes https://github.com/shellscape/postcss-values-parser/issues/29
2017-05-27 11:15:05 -07:00
Lucas Azzola 2c62b5bebe feat(number): trim extra trailing zeroes (#1768) 2017-05-27 10:46:45 -07:00
Lucas Azzola 91bcde9859 fix(typescript): fix unstable optional method (#1770) 2017-05-27 07:24:54 -07:00
Lucas Azzola 59a760c5d8 TypeScript: fix namespace comment printing (#1765)
* fix(typescript): fix namespace comment printing

* fix(typescript): remove extra newline after comment

* chore(lint): lint changes
2017-05-26 21:30:42 -07:00
Lucas Azzola 934216d852 fix(typescript): allow parens with TSFunctionType and ignore empty specifiers (#1764) 2017-05-26 20:47:48 -07:00
Lucas Azzola b265713763 fix(typescript): handle ExperimentalSpreadOperator, optional method, and abstract interface (#1763) 2017-05-26 20:19:17 -07:00
Christopher Chedeau f66d1b05d6 Add support for insensitive attribute match (#1760)
I had no idea this existed!
2017-05-26 19:30:29 -07:00
Christopher Chedeau 1255c5c51b Ignore trailing comma from the ast (#1761)
This doesn't change the semantics
2017-05-26 19:30:21 -07:00
Christopher Chedeau 044a58bb6f Add support for value-colon (#1753) 2017-05-26 15:58:09 -07:00
Christopher Chedeau 7ad38c525e Skip params in --debug-check (#1754) 2017-05-26 15:58:00 -07:00
Christopher Chedeau 2a33309e3d Print selector-string (#1759) 2017-05-26 15:54:39 -07:00
Christopher Chedeau 855a701309 Preserve comments for selectors (#1755) 2017-05-26 14:55:41 -07:00
Christopher Chedeau b11b89d2af Inline url() even if unquoted (#1752) 2017-05-26 13:45:15 -07:00
Christopher Chedeau 2f6d12aaa8 Inline url('string') (#1751)
We don't want them to break even if they go beyond 80 columns
2017-05-26 13:42:32 -07:00
Christopher Chedeau f9e324c4d0 Add the ability for SequenceExpression to break (#1749)
Fixes #1742
2017-05-26 12:29:00 -07:00
Christopher Chedeau 5f73ba29ed Add parens for expressions inside TSNonNullExpression (#1748)
Fixes #1695
2017-05-26 12:03:27 -07:00
Christopher Chedeau ba69e9fe22 Remove ast-types from comments traversal (#1744)
It's very annoying to have to have a static definition of the ast, we should instead just traverse the objects to discover it. We just need to make sure not to have any cycles, but it's also good for debugging anyway.

This fixed a few comments already :)
2017-05-26 11:44:32 -07:00
Christopher Chedeau 5e0fa00725 Upgrade typescript-eslint to 31ad3c (#1738)
Fixes #1737
2017-05-25 19:04:15 -07:00
Christopher Chedeau 2e92e4e17d Update to flow 0.47 (#1736)
Fixes #1469
Fixes #1457
Fixes #425
2017-05-25 17:00:36 -07:00
Christopher Chedeau c8f255d6d5 Port a handful of heuristics we had to work on typescript nodes as well (#1735) 2017-05-25 16:49:25 -07:00
Christopher Chedeau 267c6edaf5 Inline import type (#1729)
A long time ago I introduced the ability to break for import statements. Then, later on, we removed the ability for require to break and go over the 80 column mark. In order to be consistent, we should do the same for import statements as well.
2017-05-25 16:27:04 -07:00
Christopher Chedeau d06c46d045 Break on implements instead of extends (#1730)
There can often be something that breaks inside of `extends` so it's looking weird to break twice. It now only breaks on `implements` and make sure to put each element on its own line.

Fixes #1520
2017-05-25 16:26:56 -07:00
Christopher Chedeau ffc1b3702e Remove parenthesis for JSX inside of arrow functions (#1733)
I didn't intend for putting parenthesis there in the first place but it slept through. Since it was going to trigger a ton of changes I held up changing it, but I feel like we need to do it sooner than later otherwise we're not going to be able to do it. A lot of people writing functional components are going to be very happy about this change :)
2017-05-25 16:26:49 -07:00
Christopher Chedeau fe49650700 Inline | null and | void (#1734)
TypeScript doesn't have the concept of `?` for nullable options and instead you have to write `| null` and `| void`. This is annoying to have it use the long form, so we're now inlining them.

While working on this, I found out a few issues with the way we deal with those:
- We only align objects if the parent is a union. This means that if you have `Array<{ object }>`, the object is not aligned properly. The fix is to move the alignment logic to the union, and not the child.
- When doing so, it messes up with the comment alignment, so we have to manually handle children comment printing in the union code.

It doesn't yet fix #1727 because the hardcoded type names are different, i'll follow up in a PR.
2017-05-25 16:26:40 -07:00
Christopher Chedeau 62679f20f0 Indent conditions inside of !() (#1731)
This has been a longstanding issue and I thought it would be hard to do, but turns out it is pretty easy!

Fixes #774
2017-05-25 16:26:34 -07:00
Christopher Chedeau 198ba3c065 Force \n for multiple variable declarations (#1723)
This keeps being requested and we're not using it at Facebook, so I don't particularly care which way it should be printed. We now force multiline if there's at least one declaration with a value. We don't want to break all the variables that are just declared.

Fixes #1607
2017-05-25 16:26:26 -07:00
Christopher Chedeau 9a93822bbc Use the same breaking rules for assignment and object values (#1721)
Fixes #1417
Fixes #1604
2017-05-24 18:19:15 -07:00
Christopher Chedeau 449bc41316 Use expandLast for nested arrow functions (#1720)
We don't always want to automatically forward this option but we can always forward it to `n.body`. If it's an arrow function, it's doing the intended behavior, otherwise, it's not going to ignore it and not forward it. What we don't want is for arrow -> blockStatement -> arrow to get it, but we're good.

Fixes #1652
2017-05-24 18:19:07 -07:00
Joseph Frazier 9d7515907e Pass entire text through formatWithShebang() to format() (#1718)
* Verify shebang tests against Babylon as well as Flow/Typescript

* Pass entire text through formatWithShebang() to format()

That way, we won't have to do any arithmetic on range and cursor
locations when there is a shebang. See here for details:

https://github.com/prettier/prettier/pull/1637#issuecomment-303846507

This required changing comment printing such that comments that are
actually shebangs are just ignored.

* Add curly brace after `if`

See https://github.com/prettier/prettier/pull/1718#discussion_r118382574
2017-05-24 18:59:25 -04:00
Christopher Chedeau a6068c0532 Temporary fix comments inside of flow type spread (#1716)
This is breaking on fb code. While benjamn/ast-types#221 is getting merged in ast-type, we can add it in prettier so it fixes the issue.

Fixes #1632
2017-05-24 15:21:30 -07:00
Christopher Chedeau 58194e5375 Fix empty lines with comments between switch cases (#1709)
The function isPreviousLineEmpty comment doesn't skip comments (on purpose, see comment above that method :P) so the detection is messed up. Turns out, it's easier to just use isNextLineEmpty like everywhere else.

Fixes #1708
2017-05-24 13:20:05 -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
Christopher Chedeau 5d3a750b92 Put JSX comments inside of the parenthesis (#1712)
This is very unfortunate that we have to change the generic function that prints code but we want the JSXElement node to handle its comments printing itself in order to write them inside of the parenthesis instead of outside.

Fixes #555
2017-05-24 13:19:47 -07:00
Christopher Chedeau 7f1006056e Indent based on whitespace only for templates (#1714)
I implemented indenting based on any character that's before the `${` but it was not the right behavior. Instead people want to indent based on the indentation of this line. It's easy to fix :)

Fixes parts of #1626
2017-05-24 13:19:34 -07:00
Christopher Chedeau 78ba808322 Initial CSS support (#1636)
I wanted to see how hard it would be to add support for CSS inside of prettier. Turns out, it's not that hard. I spent a few hours printing post-css values and getting all the stylefmt unit tests to not throw.
2017-05-24 10:52:12 -07:00
Christopher Chedeau 1df1389764 Add parenthesis for as inside of class extends (#1699)
Looks like we didn't properly support it for all the binary operators :p
2017-05-23 18:39:01 -07:00
Christopher Chedeau 7cd0b78fe7 Properly add parenthesis for as inside of binary expression (#1698)
The binary logic doesn't work if the node isn't a binary or logical expression
2017-05-23 18:36:14 -07:00
Christopher Chedeau cd60e020c3 Add support for TSQualifiedType in ast-types (#1696)
The comment doesn't print in the right location so there's likely more types missing but at least it's not crashing anymore
2017-05-23 18:18:46 -07:00
Christopher Chedeau a446cf18ce Add parenthesis for ternary inside of as (#1697) 2017-05-23 18:18:34 -07:00
Christopher Chedeau 44d02b8680 Support empty declare module (#1693) 2017-05-23 17:13:47 -07:00
Christopher Chedeau 5671f32c07 Handle comments at the beginning of ClassProperty value (#1692)
We didn't call the helper that handles this use case
2017-05-23 17:13:40 -07:00
Christopher Chedeau 8ad31635b6 Make the comment chain comment detection more robust (#1691)
I kept adding places where comments could be but more use cases kept creeping in. So now this is a holistical approach, we check all the comments that can be between two nodes. This way we should be good to go :)
2017-05-23 16:31:10 -07:00
Christopher Chedeau f8ff7e5cd2 Add TSArrayType to ast-types (#1690) 2017-05-23 16:31:01 -07:00
Christopher Chedeau 7ab73e688d Add parenthesis around arrow functions in a cast (#1689) 2017-05-23 16:30:47 -07:00
Christopher Chedeau 474eed0ecb Do not add double space for export function without body with a comment (#1688) 2017-05-23 16:30:35 -07:00
Christopher Chedeau cf716e5538 Handle trailing comments in member chains (#1686) 2017-05-23 13:58:52 -07:00
Christopher Chedeau 47d9686402 Do not indent for switch case with `;` (#1687)
The indentation detection didn't discard empty statements
2017-05-23 13:58:43 -07:00
Christopher Chedeau bcc3eb9272 Add TSTupleType to ast-types (#1685)
Otherwise this throws
2017-05-23 13:14:30 -07:00
Christopher Chedeau 1e351436da Add parenthesis for binary expression inside of as (#1682) 2017-05-23 13:14:22 -07:00
Christopher Chedeau f69c5496c6 Break if the first group has an end of line comment (#1681)
This was unstable
2017-05-23 13:14:13 -07:00
Christopher Chedeau c46f230a40 Fix TSAbstractClass base (#1676)
This lets the comment traversal code discover it
2017-05-23 12:34:40 -07:00
Christopher Chedeau c2007ef2a4 Handle TypeScript decorators (#1678)
See this issue for creating the same decorators as babylon ( https://github.com/eslint/typescript-eslint-parser/issues/250 ), in the meantime, this is making a ton of files in vscode crash. So we can just support both :)
2017-05-23 12:33:54 -07:00
Christopher Chedeau 069df25475 Fix last comment of import (#1677)
We used to use isBlock as a heuristic but the real thing we want is to know if the comment is on its own line. Now it works with both :)
2017-05-23 12:33:46 -07:00
Christopher Chedeau 19486bdd17 Add parenthesis for ternaries inside of a cast (#1680) 2017-05-23 12:33:32 -07:00
Christopher Chedeau f50e42d8e9 Do not put softline before TSImportEqualsDeclaration (#1679)
I'm not sure why it was there but it makes the transform not stable
2017-05-23 12:33:26 -07:00
Christopher Chedeau e37ea2c1f6 Add expression node in TSAsExpression (#1675)
This lets the comment traversal code discover it
2017-05-23 12:33:18 -07:00
Christopher Chedeau 2c5dd4cc04 Print typescript class generics (#1673)
This is the only issue with the typescript codebase itself :)
2017-05-23 12:33:10 -07:00
Christopher Chedeau fc88aa185b Add parenthesis around binary expression inside of update expression (#1672)
This is not valid code but TypeScript parses it. It's easy to fix so might as well so we have one less failing test.
2017-05-23 12:33:03 -07:00
Karl O'Keeffe 2512497288 Hug JSX text after multi line tags (#1671)
* Hug JSX text after multi line tags

* Hug text to beginning of tags
2017-05-23 11:56:21 -07:00
Karl O'Keeffe 95e981dd21 Preserve unusual unicode whitespace (#1658)
* Keep unusual unicode spaces

This no longer converts unusual unicode whitespace characters (such as a non-breaking space) into normal spaces.

* Tweak comment based on PR feedback
2017-05-23 09:09:29 -07:00
Joseph Frazier 330601c77d Find nearest node when formatting range (#1659)
* Move range extension code into helper functions

* Add findNodeByOffset() helper

This was adapted from cbc1929c64

* Test extending formatted range to entire node

* Fix extending formatted range to entire node

* Fix style errors

* Add run_file test function

This makes it possible to use different options on a per-file basis,
which is useful for things like range formatting tests.

* Test extending the format range to nearest parseable node

This means you can select the range of a `catch` clause, attempt to
format it, and have the `try` formatted as well, rather than throwing an
error.

* Fix extending the format range to nearest parseable node

This means you can select the range of a `catch` clause, attempt to
format it, and have the `try` formatted as well, rather than throwing an
error.

* Test that external indentation is left alone when formatting a range

* Preserve external indentation when formatting a range

* Dedupe range formatting traversal callbacks

* Simplify range formatting traversal using ast-types

See https://github.com/prettier/prettier/pull/1659#issuecomment-302974798

* Make range formatting traversal more efficient

There's less unnecessary parsing now.

* Fix style errors

* Add test where range expanding fails

* Fix test where range expanding fails

This makes sure that the range contains the entirety of the nodes
containing each of the range's endpoints.

* Add test for expanding range to beginning of line

* Pass test for expanding range to beginning of line

This makes it so that indentation before the range is added to the
formatted range.

* Don't parse/stringify AST to detect pre-range indentation

See https://github.com/prettier/prettier/pull/1659#discussion_r117790671

* When formatting a range, find closest statement rather than parsing

The `isStatement` implementation came from `docs/prettier.min.js`.

See https://github.com/prettier/prettier/pull/1659#issuecomment-303154770

* Add test for range-formatting a FunctionDeclaration's argument object

* Include FunctionDeclaration when searching for nearest node to range-format

From the spec, a Program is a series of SourceElements, each of which is
either a Statement or a FunctionDeclaration. See
https://www.ecma-international.org/ecma-262/5.1/#sec-A.5

* Remove unnecessary try-catch

See https://github.com/prettier/prettier/pull/1659#discussion_r117810096

* Add tests with multiple statements

See https://github.com/prettier/prettier/pull/1659#discussion_r117810753

* Remove unnecessary arguments from findNodeByOffset()

* Contract format range to ensure it starts/ends on nodes

* Specify test ranges in the fixtures

See https://github.com/prettier/prettier/pull/1659#discussion_r117811186

* Remove unnecessary comments from range fixtures

* Remove run_file test function

It's no longer used. This essentially reverts
8241216e68f2e0da997a4f558b03658d642c89a2

* Update range formatting docs

Clarify that the range expands to the nearest statement, and not to the
end of the line.

* Don't overwrite test options when detecting range

Now that multiple files share the same object again, we shouldn't be
re-assigning to it.

* Reuse already-read fixtures for AST_COMPARE=1 tests

* Remove `run_file` global from test eslintrc

* Undo package.json churn

`yarn` reformatted it before, but the whitespace visually sets off the
comment, so let's put it back how it was before.

See https://github.com/prettier/prettier/pull/1659#discussion_r117864655

* Remove misleading comments from isSourceElement

See https://github.com/prettier/prettier/pull/1659#discussion_r117865196

* Loop backwards through string instead of reversing it

See https://github.com/prettier/prettier/pull/1659#discussion_r117865759

* Don't recompute indent string when formatting range

See https://github.com/prettier/prettier/pull/1659#discussion_r117867268

* Rename findNodeByOffset to findNodeAtOffset

"Find x by y" is the common usage for finding an `x` by a key `y`.
However, since "by" has positional meaning, let's use "at" instead.

See https://github.com/prettier/prettier/pull/1659#discussion_r117865121

* Always trimRight() in formatRange and explain why

See https://github.com/prettier/prettier/pull/1659#discussion_r117864635

* Test formatting a range that crosses AST levels

See https://github.com/prettier/prettier/pull/1659#issuecomment-303243688

* Fix formatting a range that crosses AST levels

See https://github.com/prettier/prettier/pull/1659#issuecomment-303243688

* Remove unnecessary try-catch

See e52db5e9f9 (r117878763)

* Add test demonstrating range formatting indent detection

* Detect alignment from text on line before range, but don't reformat it

This avoids reformatting non-indentation that happens to precede the
range on the same line, while still correctly indenting the range based
on it.

See https://github.com/prettier/prettier/pull/1659#discussion_r117881430
2017-05-23 07:43:58 -07:00
Brian Ng 316c76172b Add test for flow class field override (#1669) 2017-05-22 15:54:20 -07:00
Karl O'Keeffe 4083d92ad1 Fix fill wrapping when text starts with whitespace (#1666) 2017-05-22 15:32:57 -07:00
Brian Ng d608b7ba28 Bump Babylon and enable additional tests (#1668) 2017-05-22 15:31:53 -07:00
Joseph Frazier 374a68201c Don't removeLines() from TemplateLiteral quasis (#1664)
* Add tests for newlines in template literals

These are related to the following issues:

* https://github.com/prettier/prettier/issues/1662
* https://github.com/prettier/prettier/issues/821
* https://github.com/prettier/prettier/issues/1183
* https://github.com/prettier/prettier/issues/1626

* Don't removeLines() from TemplateLiteral quasis

This fixes https://github.com/prettier/prettier/issues/1662
and fixes https://github.com/prettier/prettier/issues/1626

It also affects https://github.com/prettier/prettier/issues/821 and
https://github.com/prettier/prettier/issues/1183
2017-05-22 09:00:01 -07:00