Commit Graph

29 Commits (56948a953e567196a8a652cbb8cb1a84f99e5488)

Author SHA1 Message Date
Lucas Duailibe f119d4a90f
Change run_spec to have explicit parser list (#3356) 2017-11-30 03:31:52 -03: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
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
Christopher Chedeau 8613046fd2 Respect template inline-ness (#1497)
In 1.3.0, we shipped a change that makes template literal always inlined as single arguments of a function. The problem with template literals is that they whitespace is significant so we can't change it. There are two cases:

```js
call(`
  template
  template
`);
```

and

```js
call(
  `template
   template`
);
```

If you always make the same decision to inline, you're going to be wrong for the other use case. The solution that I found that works is to figure out if there's a `\n` before the backtick `` ` ``. If that's the case, then don't inline, otherwise do. We're trying to avoid looking at the source as much as possible but this is one example where we actually don't have a choice if we want to keep the output sane.

1.3.0 made the jest codebase significantly worse because of this. The issue is that once things have been moved, this heuristic won't be able to undo it. So people need to have this fix applied before they run 1.3.0, otherwise it's going to damage their codebase unless they manually change everything back, which is a pain. So I'm going to land this as a hotfix in 1.3.1.

Fixes #1492
2017-05-03 14:35:58 -07:00
Christopher Chedeau c521406385 [RFC] Do not indent calls with a single template literal argument (#873)
I'm not sure that this is a good idea. A current workaround is to remove the parenthesis.

Fixes #786
2017-05-01 14:27:00 -07:00
Christopher Chedeau 3161bd0787 Upgrade flow parser to 0.45 (#1447)
This fixes all the unicode issues and spacing between elements in array.

Fixes #1336
Fixes #1427
Fixes #770
2017-04-27 14:59:18 -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 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
James Long cea254857a Eagerly evaluate `ifBreak` when processing template literals (fixes #795) (#798) 2017-02-23 21:21:13 -08:00
James Long 5f78e78081 Do not format template literals (#749) 2017-02-23 09:00:29 -08:00
Rogelio Guzman d45e157563 Upgrade to Jest 19 (#762)
* Upgrade to Jest 19

* Use exact versions for Jest
2017-02-21 13:59:19 -08:00
Christopher Chedeau 7046d978a4 [RFC] Preserve new lines between array elements (#707)
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.
2017-02-15 20:52:28 -05: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 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
Simon Lydell 8ed75acf46 Output strings with the minimum amount of escaped quotes (#390)
* Add tests for quotes

* Update test snapshots

* Output strings with the minimum amount of escaped quotes

* Update test snapshots

* Move tests/prettier/quotes.js into tests/quotes/strings.js

* Update test snapshots
2017-01-22 12:32:43 -08:00
Christopher Chedeau 080b7f8ec4 Swap quotes (#355)
- During the first iteration, we printed the unescaped values which let to printing invalid JavaScript characters and bad things like invisible characters.
- During the second iteration, we escaped everything, which generated valid JavaScript but you lost your emojis and chinese/cyrillic characters

In this iteration, which I hope will be the last one, we maintain the string exactly as encoded and only swap quotes. The swap quotes implementation is a bit convoluted but I think it works.
2017-01-20 14:47:52 -08:00
Christopher Chedeau b7455951e7 Workaround flow bug parsing astral unicode characters (#277)
According to @mroch, "Flow is using CESU-8, not UTF-8. http://www.unicode.org/reports/tr26/ ". While this is being fixed in flow, we can easily work around it inside of prettier. The downside of this approach is that we can't convert those strings to single or double quotes anymore.
2017-01-17 16:47:20 -05:00
Christopher Chedeau c9af5a6c3b Escape strings using jsesc (#229)
The current implementation with `JSON.stringify()` is clever but unfortunately generates incorrect JavaScript. Using `jsesc` seems like a better and safer option. https://github.com/mathiasbynens/jsesc It doesn't have any dependencies and is pretty small.

I opted for escaping all the non ascii characters, so we don't display emojis anymore. I don't think that the world is ready yet for having random unicode characters inside of source files, there still are so many parts of the toolchain that breaks with them. If we want to revert back on this decision, there's a `minimal` option on jsesc which only escapes values that need to in order to generate valid JavaScript file (assuming the encoding of the file is set to utf8).

Also, while working on React Native, we've seen that there is an optimization inside of jsc for js files that are all ascii: it doesn't do a copy for the conversion to ucs16.

Fixes #163
2017-01-16 11:03:59 -05:00
Christopher Chedeau 6f5df0e2b6 Change test to workaround babylon bug (#184)
Babylon has a bug where it doesn't escape DirectiveLiteral properly. Except for `'use strict';`, this never happens in real world code, so let's put strings in a array in order to workaround this bug and have the same output on both parsers.

https://github.com/babel/babylon/issues/289
2017-01-13 23:17:02 -05:00
Christopher Chedeau afca3d7e7a Minimize string escapes
If there you are opting in for double quote but there's a string with a double quote in it, it's better to swap to a single quote to avoid having too many `\`. Note that if there are both single and double quotes in the string, we should use the default string instead.

Fixes #139
2017-01-12 07:44:16 -08:00
James Long 00fad62c91 Regenerate snapshots 2017-01-11 10:16:38 -05:00
James Long a4643f1bae Add special case for comment at top of file; regenerate snapshots 2017-01-09 21:49:26 -05:00
James Long bcd44b4368 Keep blank lines from original source 2017-01-09 09:46:09 -05:00
James Long c9e24eb477 Add spaces around certain statements, add --write option, and more 2016-12-30 23:01:07 -05:00
James Long 88dc2681f6 Make all tests pass the crash test and fix a few more bugs 2016-12-27 21:40:04 -05:00
James Long 9acd34d67d update snapshots 2016-12-27 13:29:31 -05:00
Christopher Chedeau cf45afba61 Add testing
- This brings in the flow test suite that contains a ton of JavaScript parsing edge cases
- This creates snapshot tests using the pretty printer for all of them
- If uncomment `RUN_AST_TESTS` line in `tests/run_specs.js`, it checks ast(pretty_print(x)) == ast(x). Right now, "178 failed, 197 passed, 375 of 377 total". So half of the tests are not passing, most of them are crashes and many of the rest are subtle issues.
2016-12-23 19:51:53 +00:00