Clean up API documentation
parent
3474056b36
commit
d8be8be57a
32
README.md
32
README.md
|
@ -36,8 +36,10 @@ conforms to a consistent style. (See this [blog post](http://jlongster.com/A-Pre
|
||||||
* [Option 3. bash script](#option-3-bash-script)
|
* [Option 3. bash script](#option-3-bash-script)
|
||||||
+ [Options](#options)
|
+ [Options](#options)
|
||||||
+ [API](#api)
|
+ [API](#api)
|
||||||
|
- [`prettier.format`](#prettierformatsource--options)
|
||||||
|
- [`prettier.check`](#prettierchecksource--options)
|
||||||
|
- [`prettier.formatWithCursor`](#prettierformatwithcursorsource--options)
|
||||||
- [Custom Parser API](#custom-parser-api)
|
- [Custom Parser API](#custom-parser-api)
|
||||||
* [Example](#example)
|
|
||||||
+ [Excluding code from formatting](#excluding-code-from-formatting)
|
+ [Excluding code from formatting](#excluding-code-from-formatting)
|
||||||
* [Editor Integration](#editor-integration)
|
* [Editor Integration](#editor-integration)
|
||||||
+ [Atom](#atom)
|
+ [Atom](#atom)
|
||||||
|
@ -357,24 +359,32 @@ Prettier ships with a handful of customizable format options, usable in both the
|
||||||
|
|
||||||
### API
|
### API
|
||||||
|
|
||||||
The API has three functions, exported as `format`, `check`, and `formatWithCursor`. `format` usage is as follows:
|
The API has three functions: `format`, `check`, and `formatWithCursor`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const prettier = require("prettier");
|
const prettier = require("prettier");
|
||||||
|
|
||||||
const options = {} // optional
|
|
||||||
prettier.format(source, options);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
`check` checks to see if the file has been formatted with Prettier given those options and returns a Boolean.
|
#### `prettier.format(source [, options])`
|
||||||
|
|
||||||
|
`format` is used to format text using prettier. Options may be provided to override the defaults listed above.
|
||||||
|
|
||||||
|
```js
|
||||||
|
prettier.format("foo ( );", { semi: false });
|
||||||
|
// -> "foo()"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `prettier.check(source [, options])`
|
||||||
|
|
||||||
|
`check` checks to see if the file has been formatted with Prettier given those options and returns a `Boolean`.
|
||||||
This is similar to the `--list-different` parameter in the CLI and is useful for running Prettier in CI scenarios.
|
This is similar to the `--list-different` parameter in the CLI and is useful for running Prettier in CI scenarios.
|
||||||
|
|
||||||
|
#### `prettier.formatWithCursor(source [, options])`
|
||||||
|
|
||||||
`formatWithCursor` both formats the code, and translates a cursor position from unformatted code to formatted code.
|
`formatWithCursor` both formats the code, and translates a cursor position from unformatted code to formatted code.
|
||||||
This is useful for editor integrations, to prevent the cursor from moving when code is formatted. For example:
|
This is useful for editor integrations, to prevent the cursor from moving when code is formatted. For example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const prettier = require("prettier");
|
|
||||||
|
|
||||||
prettier.formatWithCursor(" 1", { cursorOffset: 2 });
|
prettier.formatWithCursor(" 1", { cursorOffset: 2 });
|
||||||
// -> { formatted: '1;\n', cursorOffset: 1 }
|
// -> { formatted: '1;\n', cursorOffset: 1 }
|
||||||
```
|
```
|
||||||
|
@ -388,9 +398,6 @@ If you need to make modifications to the AST (such as codemods), or you want to
|
||||||
|
|
||||||
Prettier's built-in parsers are exposed as properties on the `parsers` argument.
|
Prettier's built-in parsers are exposed as properties on the `parsers` argument.
|
||||||
|
|
||||||
|
|
||||||
##### Example
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
prettier.format("lodash ( )", {
|
prettier.format("lodash ( )", {
|
||||||
parser(text, { babylon }) {
|
parser(text, { babylon }) {
|
||||||
|
@ -398,7 +405,8 @@ prettier.format("lodash ( )", {
|
||||||
ast.program.body[0].expression.callee.name = "_";
|
ast.program.body[0].expression.callee.name = "_";
|
||||||
return ast;
|
return ast;
|
||||||
}
|
}
|
||||||
}); // ==> "_();\n"
|
});
|
||||||
|
// -> "_();\n"
|
||||||
```
|
```
|
||||||
|
|
||||||
The `--parser` CLI option may be a path to a node.js module exporting a parse function.
|
The `--parser` CLI option may be a path to a node.js module exporting a parse function.
|
||||||
|
|
Loading…
Reference in New Issue