This one is really weird
```js
if (() => {} ? 1 : 0) {}
```
parses fine but
```js
if (() => {
} ? 1 : 0) {}
```
is a syntax error. Let's always add a parenthesis.
Note that no one is every going to write this in practice, this is really useless :p
We actually need this `;` for EmptyStatement, otherwise it applies to the next block of code. (Creating a label with an empty statement is completely useless, but it triggers a lot in the fuzz testing tool)
Fixes#376
We avoid adding a `;` for a variable declaration in for loop but this is only meant if the var declaration is inside of the `()` part of the for loop. Not if the body part.
Fixes#385
* 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
- Add missing options to minimist. This removes "Ignored unknown
option:" warnings for options that actually exists.
- Put more interesting options closer to the top.
- Consistenly use full sentences.
- 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.
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
The previous API was inconsistent. The new one is
```js
--parser flow
--parser babylon
{parser: 'flow'}
{parser: 'babylon'}
```
if we ever want to add new parsers in the future it'll allow that more easily.
I put a console.log in parser.js in both functions and tested that the test suite worked both with and without the change in run_spec. I also tested that both the previous and new command line options are working.
At some point in the future we'll likely want to get rid of the old api but might as well keep supporting it so we don't break anyone for now.
Extracted out parse and attachComments. Moved the options normalization in the exported function.
This way, we have a super clean format function :)
```js
function format(text, opts) {
const ast = parse(text, opts);
attachComments(text, ast, opts)
const doc = printAstToDoc(ast, opts)
const str = printDocToString(doc, opts.printWidth)
return str;
}
```
Make all the files have a single module.exports call at the end of the file.
I added a missing `"use strict";` and removed a few unused functions from utils at the same time.