Commit Graph

87 Commits (master)

Author SHA1 Message Date
Davy Duperron 260a141dbe [WIP] Fix comments in template literals (#643)
* Add handleTemplateLiteralComment helper function.

* Fix handleTemplateLiteralComment function.

* Extend handleTemplateLiteralComment to deal with trailing comments 🚀.

* Add test.

* Make handle comments function naming more consistent, fix merge conflicts.

* Update tests.

* Add better comment injection in Template Literal.

* Pass options to attach function.

* Update tests to match new implementation.

* Fix let -> var in findExpressionIndexForComment for NodeJS v4.

* Reorder after merge conflicts.

* Drop old tests for dangling arrays.

* Replace redundant conditional by a boolean 🚀.

* Refactor implementation.
2017-02-23 09:23:56 -08:00
Christopher Chedeau b93a207bb2 Run prettier on 0.17.0 2017-02-15 19:56:11 -08:00
Christopher Chedeau c4b83c7653 Ensure that all comments are printed (#571)
This currently fails for 20 tests. I'm putting this up as a pull request so we can merge it once all the comments are properly printed :)
2017-02-13 09:04:21 -08:00
Christopher Chedeau 6a05a55852 Remove last trailing line for directives-only files (#609)
* Remove last trailing line for directives-only files

There are two hardlines that are added that do not need to.

- The first one is when there's an empty line afterwards, we want to remove it. The solution I opted for to fix this one is to trimRight the originalText so that globally it's never going to return yes for the isNextLineEmpty.
- The second one is at the end of Program, but we already printed it inside of the directive itself, so we can just add a condition to make sure it's only printed when there's a body or a comment, but not a directive.

Fixes #527

* Add comment
2017-02-07 10:39:04 -05:00
Christopher Chedeau df831d7b59 Fix files with only comments on the flow parser (#598)
Babylon has a two top level nodes: File and Program whereas flow just has Program. This causes two things two happen that prevents comments from being displayed:

1) Because there's a single node, none of following/preceding/enclosing exist. We ran into a TOOD case that we now need to fill. We just need to attach comments to the only node we have: the ast.

2) Both the raw comments and the computed comments are set on the `.comments` field of the object, however, it is being reset after calling `attach`, so we lose the computed comments :( The fix is to use a local variable and delete the comments before calling `attach`.
2017-02-04 21:36:29 -05:00
Christopher Chedeau c34013b1ee Run prettier on 0.13.0 (#497) 2017-01-28 07:50:22 -08: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
Simon Lydell bb1884320b Use babel-code-frame for syntax errors (#367)
* Use babel-code-frame for syntax errors

* Support the `--color` option more explicitly

* Update rollup config to handle babel-code-frame

* Use exact dependencies
2017-01-23 09:10:51 -08:00
Charles Pick 9a71a907e5 add formatAST() for formatting ASTs directly (#393) 2017-01-22 14:00:23 -08:00
Christopher Chedeau d20a580655 Run prettier on the codebase (#354) 2017-01-20 10:12:37 -08:00
Christopher Chedeau c468fde106 Add debugging support for doc IR (#347)
This PR adds two things:

`--debug-print-doc` command that prints the formatted doc

```js
echo "<div>&lt;</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
  groupConditional(
    group([
      group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
      "&lt;",
      "</",
      "div",
      ">"
    ]),
    [
      group([
        group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
        "&lt;",
        "</",
        "div",
        ">"
      ]),
      group([
        group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
        indent(2, groupBreak([ hardline(), "&lt;" ])),
        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 :)
2017-01-20 09:54:32 -08:00
Christopher Chedeau d8d5c7bc55 Introduce --parser/parser option and deprecate --flow-parser/useFlowParser (#342)
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.
2017-01-19 14:35:12 -08:00
Christopher Chedeau 658998c5b1 Refactor index.js (#340)
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;
}
```
2017-01-19 14:18:22 -08:00
Christopher Chedeau aef3e387f8 Extract parser.js (#338)
Before, we would parse things inline in both index.js and run_spec. Extracting it should make it easier to manage and understand what is going on.
2017-01-19 13:23:04 -08:00
Christopher Chedeau ad96fce6c2 Split pp.js into doc-{printer,builders,utils}.js (#334)
- doc-printer.js is now the direct implementation of the Wadler paper
- doc-builders.js are a lot of utils to generate the IR the above file needs
- doc-utils.js are small utils to traverse list of docs.
2017-01-19 12:46:57 -08:00
Christopher Chedeau ab046c6850 Remove Printer module in favor of single function (#333)
I pushed the options handling and doc->string process one level above.
2017-01-19 12:43:10 -08:00
Christopher Chedeau 0f54791582 Run prettier through the codebase (#316)
It's a good idea to run it right after we do a release when the number of outstanding pull requests is low :)
2017-01-18 15:31:46 -08:00
Jeffrey Horn 40973641a3 add version flag (#294)
* add version flag

* set version on the api
2017-01-18 15:45:44 -05:00
Benjamin Tan bc9b1fde19 Add newline after shebang if necessary. (#215) 2017-01-17 17:16:40 -05:00
Christopher Chedeau 05be0eb31b Add flow parser experimental options (#221)
The Nuclide codebase uses features that are still proposals which require a flag to be enabled. Babylon parses them fine without any flags.

Let's enable them by default as it doesn't cost much, you either are using those features and you don't want the parser to break, or you are not and you don't care.

After this and #218, none of the nuclide files are throwing exceptions! (yay!)
2017-01-15 23:46:27 -05:00
Benjamin Tan 3f31a87da1 Remove legacy Recast code and simplify API. (#191) 2017-01-14 23:25:30 -05:00
James Long 3af7da5748 Reprint all the files! 2017-01-13 15:03:53 -05:00
Christopher Chedeau df99ae56b0 Add test for shebang and move to index.js (#170)
When looking into adding a test, I realized that the logic was inside of bin/prettier.js and therefore only applying to the cli. Moving it to index.js and adding a test so that it's more robust :)
2017-01-13 13:39:07 -05:00
James Long 56abf5fc46 Update options to babylon 2017-01-10 17:30:17 -05:00
James Long 4abb8ce544 Support back to node v4 2017-01-10 12:18:22 -05:00
James Long 5c53a2d59c Use babylon directly and convert recast's comment algorith to use our own API 2017-01-09 17:37:45 -05:00
James Long bcd44b4368 Keep blank lines from original source 2017-01-09 09:46:09 -05:00
James Long 7b0ec6d935 Break ternary ops across newlines if needed 2017-01-05 12:32:56 -05:00
James Long 67e2f6d482 Simplify how options are normalized (fixes default options) 2017-01-04 20:45:28 -05:00
James Long 8326963f2d Add option to include spaces inside object/array literals, default to false 2017-01-04 17:23:07 -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 0465bb5790 Add flow parser as an option, default to babylon 2016-12-30 21:23:50 -05:00
James Long e972a7f0b6 Fix object and predicate annotations 2016-12-30 13:32:43 -05:00
James Long 599e5821c1 Optimize jest testing and implement more fixes 2016-12-27 13:28:04 -05:00
James Long 9b4535e9f8 Merge in forked recast printer that uses Wadler's algorithm 2016-12-23 13:38:10 -05:00
James Long 35d8546d27 Add Atom support 2016-11-30 10:05:38 -05:00
James Long d8313b03ce Add ability to call as API 2016-11-29 15:23:00 -05:00