Commit Graph

252 Commits (e6236681834b39b8e484ac1da14b2ba27293b70c)

Author SHA1 Message Date
Christopher Chedeau e623668183 Make all the member expressions but the last one part of the first group (#589)
Fixes #587
2017-02-03 16:18:54 -05:00
Christopher Chedeau 57ab92ed33 Support dangling comments in ClassBody (#570)
Comments were dropped there
2017-02-03 16:16:32 -05:00
Christopher Chedeau 4bd194e2ce Use breakParent inside of last arrow expansion (#559)
Fixes #549
Fixes #553
2017-02-03 16:15:21 -05:00
Christopher Chedeau b2a8f2de56 Add parenthesis around single argument arrow if comments (#573)
Fixes #568
2017-02-03 16:14:40 -05:00
Christopher Chedeau b7103ddbf4 Run prettier on 0.15.0 (#558) 2017-02-03 08:50:51 -08:00
Davy Duperron 6efad59300 565 jsx parentheses fix (#580)
* Add JSXElement in ShouldPrintSameLine helper function.

* Add tests 🚀.
2017-02-02 16:25:34 -08:00
Alex Rattray 86d1264097 Fix ternary indent bug (#577)
* Add test.js files back to source control

* Fix ternary indent bug (#564)
2017-02-02 10:37:59 -08:00
Simon Lydell 69b7bd3fa8 Revert "Print \x and \u escapes in strings and regexes lowercase (#522)" (#574)
This reverts commit 7148184d65.

There are four types of literals where escapes were normalized:

1. Strings ('\xAb' and "\xAb")
2. Regexes (/\xAb/)
3. Untagged template literals (`\xAb`)
4. Tagged template literals (tag`\xAb`)

However, changing the case of the escapes alters the runtime behavior of
in two of the above cases.

```js
/\xAb/.source === '\\xAb' // true
String.raw`\xAb` === '\\xAb' // true
```

So for regexes and tagged template literals the escapes must not be
changed. Instead of enforcing lowercase escapes in only 50% of the
different cases, it was decided not to bother with escapes at all.

Closes #562.
2017-02-02 09:54:10 -08:00
Christopher Chedeau 5042168df6 Make comments inside of MemberExpression look good (#556)
This is using the same technique as #544 and will conflict with it when we try to merge both :)

Fixes #200
2017-02-01 16:26:55 -08:00
Christopher Chedeau b6c9e42705 Whitelist UnaryExpression for parentless objects (#545)
It seems like unary are unlikely to need parenthesis

Fixes #542
2017-02-01 16:24:22 -08:00
Christopher Chedeau e56fd38604 Make comments between if & else to look good (#544)
While trying to figure out how to handle both MemberExpression comments and IfStatement comments, I ended up doing this one as well... Sorry @yamafaktory :(

The logic is a bit annoying but works.

Fixes #487
2017-02-01 16:23:49 -08:00
Alex Rattray be8f13dd24 [JSX] Handle non-breaking space (#557) 2017-02-01 15:29:42 -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 d3fa519492 Break nested calls (#517)
If there's a break inside of a call, we want to force it in the group, otherwise it may get the indentation wrong. See the real-world use case in #513

Fixes #513
2017-01-31 11:45:47 -05: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
Simon Lydell 7148184d65 Print \x and \u escapes in strings and regexes lowercase (#522)
* Print \x and \u escapes in strings and regexes lowercase

Theoretically, we would want to do this for escapes int identifiers as
well. However, neither flow nor babylon preserves escapes in
identifiers. For example, `\u0061.\u{0061}` cannot be distinguished from
`a.a`. Nobody uses such escapes in real code anyway. It could also be
considered a feature that such escapes are converted to real unicode
characters.

* Update snapshots

* Normalize escapes in template literals

* Update snapshots
2017-01-31 06:56:26 -08:00
Amjad Masad 045a351fce Fix closing call expression commented out (#530)
* [Failing test] Comments in call expression

```js
foo(
 // Hi
)
```

prints

```js
foo
// Hi();
```

* add one more failing case

* Don't group last args that has comments attached

* Update snapshot
2017-01-31 06:52:53 -08: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 d5e0fd5c65 Do not always put an empty lines after directives (#505)
It turns out that I need to call skipNewLine before calling hasNewLine in order to figure out if there's an empty line right after. Oops.

Fixes #504
2017-01-30 09:10:54 -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 9cfc645659 Don't print trailing commas for object destructuring and rest (#512)
Fixes #507
2017-01-30 10:07:03 -05:00
Christopher Chedeau 98a052f9a1 Remove extra group when printing object values (#502)
It turns out that in an unlikely turn of event, the inner group can be inline and not print the opening paren but the outer group breaks and outputs the closing paren which generates invalid JavaScript.

I tried removing the group altogether and no tests failed, so I'm assuming the group wasn't needed in the first place. If it was, we should add tests to cover this.

Fixes #501
2017-01-30 10:03:40 -05:00
Christopher Chedeau c34013b1ee Run prettier on 0.13.0 (#497) 2017-01-28 07:50:22 -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
Alex Rattray a9b7016c71 [JSX] Handle each line of text separately, allow up to one extra newline (#455)
* [JSX] Treat each line of text in a paragraph as its own group

* [JSX] Allow one extra newline
2017-01-27 14:20:01 -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
Christopher Chedeau 8f26568bc8 Properly print comments for BinaryExpression (#494)
We don't call the generic print on the BinaryExpression itself, so we need to manually print those comments. It's going to be useful for my work on the MemberExpression :)
2017-01-27 14:03:44 -05:00
James Long aaea8b1150 Simplify arrow functions that use blocks (fixes #268) (#496) 2017-01-27 13:39:17 -05:00
Christopher Chedeau 612574d02d Stop using conditionalGroup inside of UnionTypeAnnotation (#491)
It's actually not needed to use conditionalGroup as we can use ifBreak for it. I was able to do it just for cleanup and found out that it also fixed two of the bugs we have with comments. That's great :p

Fixes #485
Fixes #486
2017-01-26 21:41:50 -05:00
Christopher Chedeau cde322bff8 Add CallExpression to the last argument expansion whitelist (#470)
Fixes #274
2017-01-26 17:24:50 -05:00
Alex Rattray 60fa5dae29 Don't break up JSXOpeningElement if it only has a single text attribute (#488) 2017-01-26 17:22:56 -05:00
Christopher Chedeau 6ade2a1e4a Maintain windows line ending (#472)
It's nice that we print line endings in a single place, it was super easy to preserve them :)

Fixes #92
2017-01-26 14:16:42 -08:00
Christopher Chedeau 0957c8672a Indent computed member (#471)
Fixes #136
2017-01-26 14:58:47 -05:00
Alex Rattray 72c9424c53 Indent ternaries (#484) 2017-01-26 11:58:40 -08: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 573936aacc [RFC] Hug Conditionals in JSX (#473)
Conditionals are very common in JSX and it is unfortunate that they take up so much vertical space in the current prettier.

This pull request does a few tweaks:
 - It hugs ConditionalExpression (ternary) and LogicalExpression (&&) inside of `{}` in a jsx child, not an attribute
 - It doesn't output parenthesis if your parent is a LogicalExpression (&&)

Fixes #317
2017-01-26 14:51:08 -05:00
Davy Duperron 03b4ed2fcf Added parens around in operator in for loops 🚀. (#468) 2017-01-25 14:24:25 -08:00
Christopher Chedeau 620cb7be19 Add break points for class declaration (#466)
```js
class A extends B implements C {
  method() {}
}
```

now outputs

```js
class A
  extends B
  implements C {
  method() {}
}
```

Fixes #147
2017-01-25 16:36:55 -05:00
Christopher Chedeau 888c7a52dd Put decorators on the same line (#459)
Mobx is the only popular JavaScript library that I know about which uses decorators. They put things on the same line so we should follow their conventions.

The logic implemented here is the following: if there is one decorator, it's on the same line. If there is more than one, they are each on their own line.

Fixes #325
2017-01-25 16:30:09 -05:00
Christopher Chedeau 1334dbc781 Preserve new lines after directives (#464)
It's annoying that the line after `"use strict";` gets eaten away on babylon. This fixes it :)

Fixes #216
2017-01-25 16:26:09 -05: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
Henry Zhu 6e68f7495f Support printing import("a") (#458) 2017-01-25 07:33:48 -08:00
Christopher Chedeau e13bb7dbba [RFC] Do not put spacing inside of arrays with bracketSpacing option enabled (#446)
Given the discussion on #296, it seems like there's debate between spaces around `{}` but no one puts spaces around `[]`. So changing the behavior to respect this.
2017-01-24 11:38:12 -08:00
Davy Duperron 409d4b6eb5 Ability to break on `:` for objects (#314) 2017-01-24 14:20:24 -05:00
James Long 3aa267d5e8 Remove `multilineGroup` (#450) 2017-01-24 13:54:01 -05:00
Christopher Chedeau d753e56a2a Better error message for assertDoc (#449)
Right now it says that the boolean expression fails which is not super useful. Instead, now it prints the actual value.
2017-01-24 13:53:54 -05:00
Christopher Chedeau fb52e7a5c8 Do not put a newline on empty `{}` for functions (#447)
The original intent of it was for `if then else` and `try catch` as they aren't likely to be empty, but it accidentally caught function bodys, which have many valid reasons to be empty. Let's special case those out.
2017-01-24 12:37:01 -05:00
Christopher Chedeau 4c82f4a2ab Refactor traversal (#442)
- Introduce findInDocs that avoids some annoying boilerplate when you want to get a value while traversing the docs
- Implement a hasStopped function that stops the traversal when you found the value you needed
2017-01-23 20:16:26 -05:00