Commit Graph

70 Commits (05b921efdd3e1e63091c0e63e47fe7c013530e7e)

Author SHA1 Message Date
Christopher Chedeau 05b921efdd Build the dist/ with rollup (#1794) 2017-05-28 21:49:41 -07:00
Christopher Chedeau 3d6acd8648 Fix log issue 2017-05-28 13:54:54 -07:00
Christopher Chedeau 882b712c5f Update typescript-eslint (#1787)
It improves a few things.
2017-05-28 13:18:29 -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
Michał Pierzchała 3a431d3fe7 Document --with-node-modules flag (#1701) 2017-05-24 03:25:45 -07:00
Michał Pierzchała 7a0df958dc Ignore node_modules when running prettier from CLI (#1683)
* Ignore node_modules by default

* Add integration test suite for --with-node-modules CLI arg

* Refactor tests to snapshots

* Disable eslint for purposely faulty files

* Fix node 4

* Fix prettier

* Gitignore only top-level node_modules
2017-05-23 14:19:04 -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
Simon Lydell 378682350f Improve ESLint setup and fix errors (#1656)
* Improve ESLint setup

- Uses `eslint:recommended` + a handful more rules.
- Uses .eslintignore so that editors can understand which files to lint.
- Uses .eslintrc<strong>.js</strong> so more editors get syntax highlighting.

* Fix ESLint errors

About half of them were fixed by `eslint . --fix`.
2017-05-21 12:11:09 -07:00
Joseph Frazier d7aaaee0dd Add `lint` script, run it on Travis, prettify source code (#1634)
* Add `lint` script with eslint-plugin-prettier, use it in `format:all`

See https://github.com/prettier/prettier/pull/1634#issuecomment-302945967

* Run `lint` script on Travis

* Run Prettier on its own source code

This was done with

    npm run format:all
2017-05-21 11:00:16 -07:00
Joseph Frazier 569380155b Add `--range-start` and `--range-end` options to format only parts of the input (#1609)
* Add `--range-start` and `--range-end` options to format only parts of the input

These options default to `0` and `Infinity`, respectively, so that the
entire input is formatted by default. However, if either option is
specified such that a node lies completely outside the resulting range,
the node will be treated as if it has a `// prettier-ignore` comment.

Related to https://github.com/prettier/prettier/pull/1577#issuecomment-300551179
Related to https://github.com/prettier/prettier/issues/1324
Related to https://github.com/prettier/prettier/issues/593

* printer: Extract hasPrettierIgnoreComment() helper

* Move isOutsideRange() to util

* Don't throw errors about comments outside range "not printing"

* Remove unnecessary check from isOutsideRange()

* Make --range-end exclusive

This lets it use the conventional way of specifying ranges in strings.

Note that if the rangeEnd in the tests is changed to 158, it will fail,
but it wouldn't have failed before this change.

* Change range formatting approach

NOTE: This doesn't pass its test yet. Note that since we're reading the
indentation from the first line, it is expected not to change. However,
a semicolon is added, and the lines outside the range are not changed.

The new approach is roughly:

* Require that the range exactly covers an integer number of lines of the input
* Detect the indentation of the line the range starts on
* Format the range's substring using `printAstToDoc`
* Add enough `indent`s to the doc to restore the detected indentation
* Format the doc to a string with `printDocToString`
* Prepend/append the original input before/after the range

See https://github.com/prettier/prettier/pull/1609#issuecomment-301582273

---

Given `tests/range/range.js`, run the following:

    prettier tests/range/range.js --range-start 165 --range-end 246

See the range's text with:

    dd if=tests/range/range.js ibs=1 skip=165 count=81 2>/dev/null

* Don't use default function parameters

Node v4 doesn't support them. See
http://node.green/#ES2015-syntax-default-function-parameters

* Hackily fix indentation of range formatting

See
https://github.com/prettier/prettier/pull/1609#issuecomment-301625368

Also update the snapshot to reflect that the indentation actually should
decrease by one space, since there were 13 spaces in the input and we
round down after dividing by tabWidth.

* Revert "printer: Extract hasPrettierIgnoreComment() helper"

See https://github.com/prettier/prettier/pull/1609#discussion_r116804853

This reverts commit 62bf068ca98f69d4a7fd0ae188b3554d409eee8d.

* Test automatically using the beginning of the rangeStart line and same for the end

See https://github.com/prettier/prettier/pull/1609#issuecomment-301862076

* Fix automatically using the beginning of the rangeStart line and same for the end

See https://github.com/prettier/prettier/pull/1609#issuecomment-301862076

* Propagate breaks after adding an indentation-triggering hardline

See c1a61ebde8 (r116805581)

* Extract getAlignmentSize(), use instead of countIndents()

See c1a61ebde8 (r116804694)

* Extract addAlignmentToDoc(), use instead of addIndentsToDoc()

See c1a61ebde8 (r116804694)

* Document that --range-start and --range-end include the entire line

* Fix rangeStart calculation

Before, it was incorrectly resulting in 1 when the originally provided
value was 0

* Extract formatRange() helper function

* Move getAlignmentSize() from printer to util

This addresses https://github.com/prettier/prettier/pull/1609#discussion_r117636241

* Move addAlignmentToDoc() from printer to doc-builders

This addresses https://github.com/prettier/prettier/pull/1609#discussion_r117636251
2017-05-21 10:14:13 -07:00
Joseph Frazier 226adb2e81 Allow `--write` to be used with `--list-different` (#1633)
* Allow `--write` to be used with `--list-different`

This makes it possible to simultaneously check for formatting errors and
fix them, which can be useful for CI services. For example, a CI service
could run:

    prettier --list-different --write $FILES || git diff --exit-code

to show the formatting errors. Before this change, it would be necessary
to do:

    prettier --list-different $FILES || (prettier --write $FILES; git diff --exit-code)

Here are some commands which are useful to verify that the outputs and
exit codes of `prettier --list-different` and `prettier --write` have
not changed:

    ./bin/prettier.js --list-different index.js src/*.js bin/*.js; echo $?
    ./bin/prettier.js --write index.js src/*.js bin/*.js; echo $?

* fixup! Allow `--write` to be used with `--list-different`
2017-05-21 08:12:00 -07:00
Lucas Azzola 1ea4ad4e30 tools(typescript): add script to run all TypeScript tests (#1606) 2017-05-14 03:26:37 -07:00
Joseph Frazier 225fa57780 Mention --debug-check in CLI section of README (#1562)
* Prevent using --write with --debug-check

This addresses https://github.com/prettier/prettier/issues/1552#issuecomment-300043294

* Exit with a non-zero code if --debug-check fails

This makes it easier to use programmatically. See
https://github.com/prettier/prettier/issues/1552#issuecomment-300042417

* Mention --debug-check in CLI section of README

This addresses https://github.com/prettier/prettier/issues/1552#issuecomment-300043294
2017-05-09 02:51:04 -07:00
Joseph Frazier 18fd014985 Run AST comparison tests on Travis (#1553)
* Run AST comparison tests on Travis

It looks like some of these currently fail, so we should probably also
sort that out.

Inspired by https://github.com/prettier/prettier/issues/1552

* tests: Use specified parser when AST_COMPARE=1

This fixes some of the tests with AST_COMPARE=1

* Move cleanAST() into prettier.__debug

This makes it available for tests to use.

* AST_COMPARE=1 uses cleanAst() instead of removeEmptyStatements()

Ths fixes some of the tests with AST_COMPARE=1

* Export parse() from src/parser.js

This makes it available for tests to use.

* tests: Use specified parser more when AST_COMPARE=1

This is a continuation of commit 86437a66d326919897fe89891a25824870f5bb79

This fixes some of the tests with AST_COMPARE=1

* massageAST: remove leadingComments/trailingComments

This fixes some of the tests with AST_COMPARE=1

* massageAST: remove `extra`

This fixes some of the tests with AST_COMPARE=1

* tests_config/run_spec.js: Rename variables for clarity

* AST_COMPARE=1 tests compare unstringified objects

This makes the test error output shorter.

* fixup! Export parse() from src/parser.js

* Revert "Run AST comparison tests on Travis"

See https://github.com/prettier/prettier/pull/1553#issuecomment-300027747

This reverts commit 49873a956c532f23fd216551a35ae35c1a18407e.

* fixup! fixup! Export parse() from src/parser.js

* parser: Require babel-code-frame only when needed

This addresses:
* https://github.com/prettier/prettier/pull/1553#discussion_r115386253
* https://github.com/prettier/prettier/pull/1553#discussion_r115386250

* parser: Don't export now-unused parseWith* functions

Addresses https://github.com/prettier/prettier/pull/1553#discussion_r115386964

* Move cleanAST/massageAST into own file, don't export

This addresses:
* https://github.com/prettier/prettier/pull/1553#discussion_r115386993
* https://github.com/prettier/prettier/pull/1553#discussion_r115386611

* Don't destructure require() result (Node v4 compat.)

* Fix copy/paste error
2017-05-08 18:16:35 -07:00
Lucas Azzola 5cc7878902 fix(typescript): fix a handful of ast(prettier(input)) issues (#1544) 2017-05-06 22:34:47 -07:00
Lucas Azzola 5fd9cd01de test(typescript): do not fail debug check on Literal -> Identifier (#1540) 2017-05-06 19:39:19 -07:00
Christopher Chedeau 7a8380772d Have --debug-check also run ast verification (#1337) 2017-04-21 16:09:33 -07:00
Simon Lydell 706640d6dd Optimize `prettier --help` for humans (#1340) 2017-04-19 09:04:47 -07:00
Pi Lanningham 5e7503dadd Add typescript as a valid parser value (#1318)
* Add typescript as a valid parser value

Now that typescript development has started, each contributor has been adding this as a supported flag themselves.

This just adds it as an allowed option, with a warning that support for this parser is experimental.

I also tried to structure this a bit future proof, in case other experimental parsers come along.

* Simplifies the code at vjeux's request

Rather than a notion of "experimental" parsers, we'll just leave typescript out of the documentation until it's supported.
2017-04-18 09:42:03 -07:00
Hawken Rives 3e5b123c37 Skip globbing filenames with node-glob when the filename is not a glob (#1307)
* add a check to skip node-glob if the filename is not a glob

* add a return to prevent extra globbing
2017-04-16 21:12:49 -07:00
John Resig 793db3150a Write out change CLI changes synchronously. Fixes #1287. (#1292) 2017-04-15 14:06:20 -07:00
franxois 19b7febe9e Fix : ReferenceError: err is not defined (#1254)
Would fix an error I had 
It catch an error "e" but try to display "err"
2017-04-13 15:49:53 -07:00
Christopher Chedeau 22f5ba0989 change semi default 2017-04-13 10:18:50 -07:00
Alex Rattray 5fa8df331e Add cli option for --no-semi (#1223) 2017-04-12 18:51:08 -07:00
Christopher Chedeau 8e1583fd16 Format the codebase using the pre-1.0 release (#1194) 2017-04-12 10:16:11 -07:00
Rafael Hengles 170e4d558a Refactored option to indent with tabs (#1026)
Refactored option to indent with tabs
2017-04-06 22:49:37 -04:00
Brian Holt eff5af6ca9 Add check method to Prettier Node API. (#1104)
* Add check method to Prettier. Make CLI use that method for list-different

* Catch in check and return false if it throws

* Remove catch/finally from Prettier list-different bin

* remove try catch in prettier bin for list-different
2017-04-03 09:54:10 -07:00
Christopher Chedeau e88e76cf92 Do not use more than one file descriptor at a time (#1048)
@cpojer made a great suggestion to use `fs.readFileSync` instead of the async version. prettier is CPU-bound and if you want parallelism you can spin up many instances of prettier.

Fixes #994
2017-03-20 10:14:33 -07:00
Davy Duperron 440ddc2e1a Remove unused code. (#1028) 2017-03-16 13:26:44 -07:00
Christopher Chedeau 205458a8d1 Introduce -l option (#854)
gofmt has this option and it's very handy for commit-hooks.

Fixes #851
2017-03-02 22:47:36 -05:00
Christopher Chedeau 7c4654b7a3 Run prettier on 0.19.0 (#793) 2017-02-23 09:57:51 -08:00
James Long 5d131027ce Make trailing-comma option support 2 different modes (#641)
* Make trailing-comma option support 2 different modes

* Add deprecation warning

* Add deprecation warning to API
2017-02-17 22:44:55 -05:00
Christopher Chedeau b93a207bb2 Run prettier on 0.17.0 2017-02-15 19:56:11 -08:00
Umidbek Karimov 997de74553 Use `readline` api to manipulate `process.stdout` output. (#687)
* User `readline` api to manipulate `process.stdout` output.

* Revert "prettification".
2017-02-14 13:44:14 -08:00
Christopher Chedeau f3d64b0ffc fix --debug-check 2017-02-13 19:25:28 -08:00
Christopher Chedeau 4dca78dbed Add option for putting > on the last line in jsx (#661)
Feel free to suggest a better name for the option. Otherwise it works great :)

Fixes #654
2017-02-13 12:57:05 -05:00
Umidbek Karimov 97fd8a8d81 Log filename with [update] or [ignore] flags during `--write` process. (#584)
* Make `--write` logs more informative.

* Add format duration to logs.

* Use colors instead of [ignore] and [update] labels.

* Remove `colors` dependency.

* Use `chalk` to colorize logs.

* Print filename before `format` and add duration info later.

* Print filename before read error.
2017-02-07 14:03:48 -05:00
Christopher Chedeau 4df5250f74 Add --debug-check cli option (#627)
This let's you run `pp(x) === pp(pp(x))` across an entire codebase quickly. I think it's small enough that we can merge it in to master while we are nailing down all the issues.

You can see the output in #610
2017-02-07 10:04:44 -05:00
Simon Lydell 17add13860 Only write to files if the change (#511)
Fixes #506.
2017-01-29 07:09:22 -08:00
Christopher Chedeau c34013b1ee Run prettier on 0.13.0 (#497) 2017-01-28 07:50:22 -08:00
Simon Lydell 239521e97d CLI improvements (#478)
* Add `--help` CLI flag

* Don't pick up unknown CLI options

This prevents people from adding new CLI options in the future, but
forgetting to add it explicitly to minimist, resulting in a false
"Ignored unknown option" warning.

* Add `-h` and `-v` option aliases

It always bugs me when those don't do `--help` and `--version` for no
reason in CLIs.

* Allow `echo 'test' | prettier` without the `--stdin` flag

* Improve CLI error handling and validation

- Handle errors the same way both when using stdin and when using files.
- Print validation errors nicely.
- Validate int options, instead of silently ignoring bad input.
- Warn about unknown parsers, falling back to babylon. If a new parser
  is added in the future, this allows graceful degradation for tools
  running an older version of prettier. (Just like how unknown options
  are warnings instead of errors.)
- Add comments.

* Run prettier on bin/prettier.js
2017-01-26 10:13:32 -08:00
James Long f6708a5cca Fix integer CLI arguments (#452) 2017-01-24 16:07:05 -05:00
James Long cd2f7393ef Propagate breaks upwards automatically, introduce `breakParent`, and deprecate `multilineGroup` (#440) 2017-01-23 18:47:11 -05:00
Simon Lydell c230562f65 Explain the `--color` option in a comment (#434) 2017-01-23 16:29:57 -05:00
Simon Lydell 4ce637d69c Don't print double newlines at EOF to stdout (#437)
If you use the `--write` option, the files will end with a single
newline as expected. But if you let prettier print to stdout instead and
redirect that into a file, it will contain _two_ newlines at the end.

Demonstration of the correct `--write` behavior:

```
$ cat tmp.js
function test() {
  return "hello";
}
$ wc -l tmp.js
3 tmp.js
$ ./bin/prettier.js tmp.js --write
tmp.js
$ cat tmp.js
function test() {
  return "hello";
}
$ wc -l tmp.js
3 tmp.js
```

Notice how an extra line is added when redirecting stdout:

```
$ ./bin/prettier.js tmp.js > tmp2.js
$ cat tmp2.js
function test() {
  return "hello";
}

$ wc -l tmp2.js
4 tmp2.js
```

With this commit things work as expected:

```
$ ./bin/prettier.js tmp.js > tmp2.js
$ cat tmp2.js
function test() {
  return "hello";
}
$ wc -l tmp2.js
3 tmp2.js
```

Fixes #377.
2017-01-23 13:10:26 -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
Simon Lydell 6b3d3bc066 Add glob support to the CLI (#363)
* Add glob support to the CLI

Fixes #145. Fixes #362.

* Update README.md regarding globs

* Use exact dependency for glob
2017-01-23 10:37:57 -05:00
Simon Lydell 40748a2dc9 Fix CLI options (#369)
- 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.
2017-01-21 16:48:18 -08:00
Simon Lydell b8ecf6e5ad s/jscodefmt/prettier/ (#370) 2017-01-21 16:42:13 -08:00
Simon Lydell 4611020214 Warn if using deprecated CLI options (#364)
* Warn if using deprecated CLI options

* Warn if using deprecated API options
2017-01-21 09:23:29 -08:00