prettier 1.5.2 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/jlongster/prettier)
4
4
  [![Build Status](https://travis-ci.org/prettier/prettier.svg?branch=master)](https://travis-ci.org/prettier/prettier)
5
+ [![Codecov](https://img.shields.io/codecov/c/github/prettier/prettier.svg)](https://codecov.io/gh/prettier/prettier)
5
6
  [![NPM version](https://img.shields.io/npm/v/prettier.svg)](https://www.npmjs.com/package/prettier)
6
7
  [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](#badge)
7
8
 
@@ -33,14 +34,7 @@ conforms to a consistent style. (See this [blog post](http://jlongster.com/A-Pre
33
34
  + [CLI](#cli)
34
35
  + [ESLint](#eslint)
35
36
  + [Pre-commit Hook](#pre-commit-hook)
36
- * [Option 1. lint-staged](#option-1-lint-staged)
37
- * [Option 2. pre-commit](#option-2-pre-commit)
38
- * [Option 3. bash script](#option-3-bash-script)
39
37
  + [API](#api)
40
- - [`prettier.format`](#prettierformatsource--options)
41
- - [`prettier.check`](#prettierchecksource--options)
42
- - [`prettier.formatWithCursor`](#prettierformatwithcursorsource--options)
43
- - [Custom Parser API](#custom-parser-api)
44
38
  + [Excluding code from formatting](#excluding-code-from-formatting)
45
39
  * [Options](#options)
46
40
  + [Print Width](#print-width)
@@ -54,6 +48,10 @@ conforms to a consistent style. (See this [blog post](http://jlongster.com/A-Pre
54
48
  + [Range](#range)
55
49
  + [Parser](#parser)
56
50
  + [Filepath](#filepath)
51
+ * [Configuration File](#configuration-file)
52
+ + [Basic Configuration](#basic-configuration)
53
+ + [Configuration Overrides](#configuration-overrides)
54
+ + [Configuration Schema](#configuration-schema)
57
55
  * [Editor Integration](#editor-integration)
58
56
  + [Atom](#atom)
59
57
  + [Emacs](#emacs)
@@ -158,7 +156,7 @@ Since coming up with a coding style and enforcing it is a big undertaking, it of
158
156
 
159
157
  ### Ride the hype train
160
158
 
161
- Purely technical aspects of the projects aren't the only thing people look into when choosing to adopt Prettier. Who built and uses it and how quickly it spreads through the community have a non trivial impact.
159
+ Purely technical aspects of the projects aren't the only thing people look into when choosing to adopt Prettier. Who built and uses it and how quickly it spreads through the community has a non-trivial impact.
162
160
  - “The amazing thing, for me, is: 1) Announced 2 months ago. 2) Already adopted by, it seems, every major JS project. 3) 7000 stars, 100,000 npm downloads/mo”
163
161
  - “Was built by the same people as React & React Native.”
164
162
  - “I like to be part of the hot new things.”
@@ -168,14 +166,14 @@ A few of the [many projects](https://www.npmjs.com/browse/depended/prettier) usi
168
166
 
169
167
  <table>
170
168
  <tr>
171
- <td><p align="center"><a href="https://facebook.github.io/react/"><img src="images/react-200x100.png" alt="React" width="200" height="100"><br>React</a></p></td>
172
- <td><p align="center"><a href="https://facebook.github.io/jest/"><img src="images/jest-200x100.png" alt="Jest" width="200" height="100"><br>Jest</a></p></td>
173
- <td><p align="center"><a href="https://yarnpkg.com"><img src="images/yarn-200x100.png" alt="Yarn" width="200" height="100"><br>Yarn</a></p></td>
169
+ <td><p align="center"><a href="https://facebook.github.io/react/"><img src="website/static/images/react-200x100.png" alt="React" width="200" height="100"><br>React</a></p></td>
170
+ <td><p align="center"><a href="https://facebook.github.io/jest/"><img src="website/static/images/jest-200x100.png" alt="Jest" width="200" height="100"><br>Jest</a></p></td>
171
+ <td><p align="center"><a href="https://yarnpkg.com"><img src="website/static/images/yarn-200x100.png" alt="Yarn" width="200" height="100"><br>Yarn</a></p></td>
174
172
  </tr>
175
173
  <tr>
176
- <td><p align="center"><a href="https://babeljs.io/"><img src="images/babel-200x100.png" alt="Babel" width="200" height="100"><br>Babel</a></p></td>
177
- <td><p align="center"><a href="https://zeit.co/"><img src="images/zeit-200x100.png" alt="Zeit" width="200" height="100"><br>Zeit</a></p></td>
178
- <td><p align="center"><a href="https://webpack.js.org/api/cli/"><img src="images/webpack-200x100.png" alt="Webpack-cli" width="200" height="100"><br>Webpack-cli</a></p></td>
174
+ <td><p align="center"><a href="https://babeljs.io/"><img src="website/static/images/babel-200x100.png" alt="Babel" width="200" height="100"><br>Babel</a></p></td>
175
+ <td><p align="center"><a href="https://zeit.co/"><img src="website/static/images/zeit-200x100.png" alt="Zeit" width="200" height="100"><br>Zeit</a></p></td>
176
+ <td><p align="center"><a href="https://webpack.js.org/api/cli/"><img src="website/static/images/webpack-200x100.png" alt="Webpack-cli" width="200" height="100"><br>Webpack-cli</a></p></td>
179
177
  </tr>
180
178
  </table>
181
179
 
@@ -186,7 +184,7 @@ Linters have two categories of rules:
186
184
 
187
185
  **Formatting rules**: eg: [max-len](http://eslint.org/docs/rules/max-len), [no-mixed-spaces-and-tabs](http://eslint.org/docs/rules/no-mixed-spaces-and-tabs), [keyword-spacing](http://eslint.org/docs/rules/keyword-spacing), [comma-style](http://eslint.org/docs/rules/comma-style)...
188
186
 
189
- Prettier makes this whole category of rules not needed anymore! Prettier is going to reprint the entire program from scratch in a consistent way, so it's not possible for the programmer to make a mistake there anymore :)
187
+ Prettier alleviates the need for this whole category of rules! Prettier is going to reprint the entire program from scratch in a consistent way, so it's not possible for the programmer to make a mistake there anymore :)
190
188
 
191
189
  **Code-quality rules**: eg [no-unused-vars](http://eslint.org/docs/rules/no-unused-vars), [no-extra-bind](http://eslint.org/docs/rules/no-extra-bind), [no-implicit-globals](http://eslint.org/docs/rules/no-implicit-globals), [prefer-promise-reject-errors](http://eslint.org/docs/rules/prefer-promise-reject-errors)...
192
190
 
@@ -236,11 +234,11 @@ expands the globs rather than your shell, for cross-platform usage.
236
234
  The [glob syntax from the glob module](https://github.com/isaacs/node-glob/blob/master/README.md#glob-primer)
237
235
  is used.
238
236
 
237
+ #### `--with-node-modules`
238
+
239
239
  Prettier CLI will ignore files located in `node_modules` directory. To opt-out from this behavior use `--with-node-modules` flag.
240
240
 
241
- If you're worried that Prettier will change the correctness of your code, add `--debug-check` to the command.
242
- This will cause Prettier to print an error message if it detects that code correctness might have changed.
243
- Note that `--write` cannot be used with `--debug-check`.
241
+ #### `--list-different`
244
242
 
245
243
  Another useful flag is `--list-different` (or `-l`) which prints the filenames of files that are different from Prettier formatting. If there are differences the script errors out, which is useful in a CI scenario.
246
244
 
@@ -248,6 +246,35 @@ Another useful flag is `--list-different` (or `-l`) which prints the filenames o
248
246
  prettier --single-quote --list-different "src/**/*.js"
249
247
  ```
250
248
 
249
+ #### `--find-config-path` and `--config`
250
+
251
+ If you are repeatedly formatting individual files with `prettier`, you will incur a small performance cost
252
+ when prettier attempts to look up a [configuration file](#configuration-file). In order to skip this, you may
253
+ ask prettier to find the config file once, and re-use it later on.
254
+
255
+ ```bash
256
+ prettier --find-config-path ./my/file.js
257
+ ./my/.prettierrc
258
+ ```
259
+
260
+ This will provide you with a path to the configuration file, which you can pass to `--config`:
261
+
262
+ ```bash
263
+ prettier --config ./my/.prettierrc --write ./my/file.js
264
+ ```
265
+
266
+ You can also use `--config` if your configuration file lives somewhere where prettier cannot find it,
267
+ such as a `config/` directory.
268
+
269
+ If you don't have a configuration file, or want to ignore it if it does exist,
270
+ you can pass `--no-config` instead.
271
+
272
+ #### `--debug-check`
273
+
274
+ If you're worried that Prettier will change the correctness of your code, add `--debug-check` to the command.
275
+ This will cause Prettier to print an error message if it detects that code correctness might have changed.
276
+ Note that `--write` cannot be used with `--debug-check`.
277
+
251
278
  ### ESLint
252
279
 
253
280
  If you are using ESLint, integrating Prettier to your workflow is straightforward:
@@ -257,7 +284,7 @@ Just add Prettier as an ESLint rule using [eslint-plugin-prettier](https://githu
257
284
  ```js
258
285
  yarn add --dev prettier eslint-plugin-prettier
259
286
 
260
- // .eslintrc
287
+ // .eslintrc.json
261
288
  {
262
289
  "plugins": [
263
290
  "prettier"
@@ -312,25 +339,25 @@ and add this config to your `package.json`:
312
339
  }
313
340
  }
314
341
  ```
342
+ There is a limitation where if you stage specific lines this approach will stage the whole file after regardless. See this [issue](https://github.com/okonet/lint-staged/issues/62) for more info.
315
343
 
316
344
  See https://github.com/okonet/lint-staged#configuration for more details about how you can configure lint-staged.
317
345
 
318
346
 
319
347
  ##### Option 2. [pre-commit](https://github.com/pre-commit/pre-commit)
320
348
 
321
- Copy the following config in your pre-commit config yaml file:
349
+ Copy the following config into your `.pre-commit-config.yaml` file:
322
350
 
323
351
  ```yaml
324
352
 
325
- - repo: https://github.com/awebdeveloper/pre-commit-prettier
353
+ - repo: https://github.com/prettier/prettier
326
354
  sha: '' # Use the sha or tag you want to point at
327
355
  hooks:
328
356
  - id: prettier
329
- additional_dependencies: ['prettier@1.4.2']
330
357
 
331
- ```
358
+ ```
332
359
 
333
- Find more info from [here](https://github.com/awebdeveloper/pre-commit-prettier).
360
+ Find more info from [here](http://pre-commit.com).
334
361
 
335
362
  ##### Option 3. bash script
336
363
 
@@ -341,20 +368,17 @@ Alternately you can save this script as `.git/hooks/pre-commit` and give it exec
341
368
  jsfiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.jsx\?$' | tr '\n' ' ')
342
369
  [ -z "$jsfiles" ] && exit 0
343
370
 
344
- diffs=$(node_modules/.bin/prettier -l $jsfiles)
345
- [ -z "$diffs" ] && exit 0
371
+ # Prettify all staged .js files
372
+ echo "$jsfiles" | xargs ./node_modules/.bin/prettier --write
346
373
 
347
- echo "here"
348
- echo >&2 "Javascript files must be formatted with Prettier. Please run:"
349
- echo >&2 "node_modules/.bin/prettier --write "$diffs""
374
+ # Add back the modified/prettified files to staging
375
+ echo "$jsfiles" | xargs git add
350
376
 
351
- exit 1
377
+ exit 0
352
378
  ```
353
379
 
354
380
  ### API
355
381
 
356
- The API has three functions: `format`, `check`, and `formatWithCursor`.
357
-
358
382
  ```js
359
383
  const prettier = require("prettier");
360
384
  ```
@@ -385,6 +409,31 @@ prettier.formatWithCursor(" 1", { cursorOffset: 2 });
385
409
  // -> { formatted: '1;\n', cursorOffset: 1 }
386
410
  ```
387
411
 
412
+ #### `prettier.resolveConfig([filePath [, options]])`
413
+
414
+ `resolveConfig` can be used to resolve configuration for a given source file.
415
+ The function optionally accepts an input file path as an argument, which defaults to the current working directory.
416
+ A promise is returned which will resolve to:
417
+ * An options object, providing a [config file](#configuration-file) was found.
418
+ * `null`, if no file was found.
419
+
420
+ The promise will be rejected if there was an error parsing the configuration file.
421
+
422
+ If `options.useCache` is `false`, all caching will be bypassed.
423
+
424
+ ```js
425
+ const text = fs.readFileSync(filePath, "utf8");
426
+ prettier.resolveConfig(filePath).then(options => {
427
+ const formatted = prettier.format(text, options);
428
+ })
429
+ ```
430
+
431
+ #### `prettier.clearConfigCache()`
432
+
433
+ As you repeatedly call `resolveConfig`, the file system structure will be cached for performance.
434
+ This function will clear the cache. Generally this is only needed for editor integrations that
435
+ know that the file system has changed since the last format took place.
436
+
388
437
  #### Custom Parser API
389
438
 
390
439
  If you need to make modifications to the AST (such as codemods), or you want to provide an alternate parser, you can do so by setting the `parser` option to a function. The function signature of the parser function is:
@@ -445,11 +494,13 @@ matrix(
445
494
  Prettier ships with a handful of customizable format options, usable in both the CLI and API.
446
495
 
447
496
  ### Print Width
448
- Specify the length of line that the printer will wrap on.
497
+ Specify the line length that the printer will wrap on.
449
498
 
450
- **We strongly recommend against using more than 80 columns.**
451
-
452
- Prettier works by cramming as much content as possible until it reaches the limit, which happens to work well for 80 columns but makes lines that are very crowded. When a bigger column count is used in styleguides, it usually means that code is allowed to go beyond 80 columns, but not to make every single line go there, like Prettier would do.
499
+ > **For readability we recommend against using more than 80 characters:**
500
+ >
501
+ >In code styleguides, maximum line length rules are often set to 100 or 120. However, when humans write code, they don't strive to reach the maximum number of columns on every line. Developers often use whitespace to break up long lines for readability. In practice, the average line length often ends up well below the maximum.
502
+ >
503
+ > Prettier, on the other hand, strives to fit the most code into every line. With the print width set to 120, prettier may produce overly compact, or otherwise undesirable code.
453
504
 
454
505
  Default | CLI Override | API Override
455
506
  --------|--------------|-------------
@@ -493,12 +544,13 @@ Default | CLI Override | API Override
493
544
  `false` | `--single-quote` | `singleQuote: <bool>`
494
545
 
495
546
  ### Trailing Commas
496
- Print trailing commas wherever possible.
547
+ Print trailing commas wherever possible when multi-line. (A single-line array,
548
+ for example, never gets trailing commas.)
497
549
 
498
550
  Valid options:
499
551
  * `"none"` - No trailing commas.
500
552
  * `"es5"` - Trailing commas where valid in ES5 (objects, arrays, etc.)
501
- * `"all"` - Trailing commas wherever possible (function arguments). This requires node 8 or a [transform](https://babeljs.io/docs/plugins/syntax-trailing-function-commas/).
553
+ * `"all"` - Trailing commas wherever possible (including function arguments). This requires node 8 or a [transform](https://babeljs.io/docs/plugins/syntax-trailing-function-commas/).
502
554
 
503
555
  Default | CLI Override | API Override
504
556
  --------|--------------|-------------
@@ -516,7 +568,7 @@ Default | CLI Override | API Override
516
568
  `true` | `--no-bracket-spacing` | `bracketSpacing: <bool>`
517
569
 
518
570
  ### JSX Brackets
519
- Put the `>` of a multi-line JSX element at the end of the last line instead of being alone on the next line.
571
+ Put the `>` of a multi-line JSX element at the end of the last line instead of being alone on the next line (does not apply to self closing elements).
520
572
 
521
573
  Default | CLI Override | API Override
522
574
  --------|--------------|-------------
@@ -546,7 +598,7 @@ Built-in parsers:
546
598
  * [`flow`](https://github.com/facebook/flow/tree/master/src/parser)
547
599
  * [`typescript`](https://github.com/eslint/typescript-eslint-parser) _Since v1.4.0_
548
600
  * [`postcss`](https://github.com/postcss/postcss) _Since v1.4.0_
549
- * [`json`](https://github.com/vtrushin/json-to-ast) _Since v1.5.0_
601
+ * [`json`](https://github.com/babel/babylon/tree/f09eb3200f57ea94d51c2a5b1facf2149fb406bf#babylonparseexpressioncode-options) _Since v1.5.0_
550
602
  * [`graphql`](https://github.com/graphql/graphql-js/tree/master/src/language) _Since v1.5.0_
551
603
 
552
604
  [Custom parsers](#custom-parser-api) are also supported. _Since v1.5.0_
@@ -568,6 +620,88 @@ Default | CLI Override | API Override
568
620
  --------|--------------|-------------
569
621
  None | `--stdin-filepath <string>` | `filepath: "<string>"`
570
622
 
623
+ ## Configuration File
624
+
625
+ Prettier uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for configuration file support.
626
+ This means you can configure prettier via:
627
+
628
+ * A `.prettierrc` file, written in YAML or JSON.
629
+ * A `prettier.config.js` file that exports an object.
630
+ * A `"prettier"` key in your `package.json` file.
631
+
632
+ The configuration file will be resolved starting from the location of the file being formatted,
633
+ and searching up the file tree until a config file is (or isn't) found.
634
+
635
+ The options to the configuration file are the same the [API options](#options).
636
+
637
+ ### Basic Configuration
638
+
639
+ JSON:
640
+
641
+ ```json
642
+ // .prettierrc
643
+ {
644
+ "printWidth": 100,
645
+ "parser": "flow"
646
+ }
647
+ ```
648
+
649
+ YAML:
650
+
651
+ ```yaml
652
+ # .prettierrc
653
+ printWidth: 100
654
+ parser: flow
655
+ ```
656
+
657
+ ### Configuration Overrides
658
+
659
+ Prettier borrows eslint's [override format](http://eslint.org/docs/user-guide/configuring#example-configuration).
660
+ This allows you to apply configuration to specific files.
661
+
662
+ JSON:
663
+
664
+ ```json
665
+ {
666
+ "semi": false,
667
+ "overrides": [{
668
+ "files": "*.test.js",
669
+ "options": {
670
+ "semi": true
671
+ }
672
+ }]
673
+ }
674
+ ```
675
+
676
+ YAML:
677
+
678
+ ```yaml
679
+ semi: false
680
+ overrides:
681
+ - files: "*.test.js"
682
+ options:
683
+ semi: true
684
+ ```
685
+
686
+ `files` is required for each override, and may be a string or array of strings.
687
+ `excludeFiles` may be optionally provided to exclude files for a given rule, and may also be a string or array of strings.
688
+
689
+ To get prettier to format its own `.prettierrc` file, you can do:
690
+
691
+ ```json
692
+ {
693
+ "overrides": [{
694
+ "files": ".prettierrc",
695
+ "options": { "parser": "json" }
696
+ }]
697
+ }
698
+ ```
699
+
700
+ For more information on how to use the CLI to locate a file, see the [CLI](#cli) section.
701
+
702
+ ### Configuration Schema
703
+
704
+ If you'd like a JSON schema to validate your configuration, one is available here: http://json.schemastore.org/prettierrc.
571
705
 
572
706
  ## Editor Integration
573
707
 
@@ -583,7 +717,7 @@ for on-demand formatting.
583
717
 
584
718
  ### Vim
585
719
 
586
- Vim users can simply install either [sbdchd](https://github.com/sbdchd)/[neoformat](https://github.com/sbdchd/neoformat) or [mitermayer](https://github.com/mitermayer)/[vim-prettier](https://github.com/mitermayer/vim-prettier), for more details see [this directory](https://github.com/prettier/prettier/tree/master/editors/vim)
720
+ Vim users can simply install either [sbdchd](https://github.com/sbdchd)/[neoformat](https://github.com/sbdchd/neoformat), [w0rp](https://github.com/w0rp)/[ale](https://github.com/w0rp/ale), or [prettier](https://github.com/prettier)/[vim-prettier](https://github.com/prettier/vim-prettier), for more details see [this directory](https://github.com/prettier/prettier/tree/master/editors/vim).
587
721
 
588
722
  ### Visual Studio Code
589
723
 
@@ -591,7 +725,7 @@ Can be installed using the extension sidebar. Search for `Prettier - JavaScript
591
725
 
592
726
  Can also be installed using `ext install prettier-vscode`.
593
727
 
594
- [Check its repository for configuration and shortcuts](https://github.com/esbenp/prettier-vscode)
728
+ [Check its repository for configuration and shortcuts](https://github.com/prettier/prettier-vscode)
595
729
 
596
730
  ### Visual Studio
597
731
 
@@ -617,7 +751,7 @@ features enabled, but you can also use the
617
751
  `parser` API or `--parser` CLI [option](#options).
618
752
 
619
753
  All of JSX and Flow syntax is supported. In fact, the test suite in
620
- `tests` *is* the entire Flow test suite and they all pass.
754
+ `tests/flow` *is* the entire Flow test suite and they all pass.
621
755
 
622
756
  Prettier also supports [TypeScript](https://www.typescriptlang.org/), CSS, [LESS](http://lesscss.org/), [SCSS](http://sass-lang.com), [JSON](http://json.org/), and [GraphQL](http://graphql.org/).
623
757
 
@@ -636,9 +770,13 @@ passes `prettier` output to `standard --fix`
636
770
  - [`prettier-miscellaneous`](https://github.com/arijs/prettier-miscellaneous)
637
771
  `prettier` with a few minor extra options
638
772
  - [`neutrino-preset-prettier`](https://github.com/SpencerCDixon/neutrino-preset-prettier) allows you to use Prettier as a Neutrino preset
639
- - [`prettier_d`](https://github.com/josephfrazier/prettier_d.js) runs Prettier as a server to avoid Node.js startup delay
773
+ - [`prettier_d`](https://github.com/josephfrazier/prettier_d.js) runs Prettier as a server to avoid Node.js startup delay. It also supports configuration via `.prettierrc`, `package.json`, and `.editorconfig`.
640
774
  - [`Prettier Bookmarklet`](https://prettier.glitch.me/) provides a bookmarklet and exposes a REST API for Prettier that allows to format CodeMirror editor in your browser
641
775
  - [`prettier-github`](https://github.com/jgierer12/prettier-github) formats code in GitHub comments
776
+ - [`rollup-plugin-prettier`](https://github.com/mjeanroy/rollup-plugin-prettier) allows you to use Prettier with Rollup
777
+ - [`markdown-magic-prettier`](https://github.com/camacho/markdown-magic-prettier) allows you to use Prettier to format JS [codeblocks](https://help.github.com/articles/creating-and-highlighting-code-blocks/) in Markdown files via [Markdown Magic](https://github.com/DavidWells/markdown-magic)
778
+ - [`tslint-plugin-prettier`](https://github.com/ikatyang/tslint-plugin-prettier) runs Prettier as a TSLint rule and reports differences as individual TSLint issues
779
+ - [`tslint-config-prettier`](https://github.com/alexjoverm/tslint-config-prettier) use TSLint with Prettier without any conflict
642
780
 
643
781
  ## Technical Details
644
782