forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move files around in preparation for refactor * Update paths in build script * Extract generic printing logic from the JavaScript printer * Conform printer API * Fixup decorator handling * Fix multiparser * Create plugin entry for markdown * Create plugin entry for javascript/typescript * Create plugin entry for html * Create plugin entry for graphql * Create plugin entry for css/less/scss * Move JSON to JS plugin entry * Integrate plugins into getSupportInfo() * Move astFormat to parser definition * Move util to common * Implement parser loading * remark -> mdast * Rename cli/cli -> cli/index * Rename builder -> doc package, fix printer resolution * Fix doc shape assumption in CSS-in-JS logic * Fix third-party.js prod resolution * Fixup build-docs script * Distribute multiparser code * Remove requirement to forward options * Flatten closure * Remove debug directory * Expose doc * Add external plugins * Pass options to loadPlugins * Export getParsers * Pin resolve version * Use getSupportInfo in Markdown embed * Document plugin API * Update build-docs * Add CLI for plugins * Lint docs * Fixup build.js * Add vue language * Fixup multiparser for vue * Upgrade rollup and rollup-plugin-commonjs * Fixup third-party build * Change AST format in docs
- Loading branch information
Showing
72 changed files
with
1,474 additions
and
993 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
--- | ||
id: plugins | ||
title: Plugins | ||
--- | ||
|
||
# IN DEVELOPMENT | ||
|
||
> The plugin API is unreleased and the API may change! | ||
Plugins are ways of adding new languages to Prettier. Prettier's own implementations of all languages are expressed using the plugin API. The core `prettier` package contains JavaScript and other web-focussed languages built in. For additional languages you'll need to install a plugin. | ||
|
||
## Using Plugins | ||
|
||
There are three ways to add plugins to Prettier: | ||
|
||
* Via the CLI. | ||
* Via the API. | ||
* With a configuration file. | ||
|
||
### Configuration File (Recommended) | ||
|
||
In your [configuration file](./configuration.md), add the `plugins` property: | ||
|
||
```json | ||
{ | ||
"plugins": ["prettier-python"] | ||
} | ||
``` | ||
|
||
### CLI | ||
|
||
With the [CLI](./cli.md), pass the `--plugin` flag: | ||
|
||
```bash | ||
prettier --write main.py --plugin prettier-python | ||
``` | ||
|
||
> Tip: You can pass multiple `--plugin` flags. | ||
## Official Plugins | ||
|
||
* [`prettier-python`](https://github.com/prettier/prettier-python) | ||
* [`prettier-php`](https://github.com/prettier/prettier-php) | ||
|
||
## Developing Plugins | ||
|
||
Prettier plugins are regular JavaScript modules with three exports, `languages`, `parsers` and `printers`. | ||
|
||
### `languages` | ||
|
||
Languages is an array of language definitions that your plugin will contribute to Prettier. It can include all of the fields specified in [`prettier.getSupportInfo()`](./api.md#prettiergetsupportinfo-version). | ||
|
||
It **must** include `name` and `parsers`. | ||
|
||
```js | ||
export const languages = [ | ||
{ | ||
// The language name | ||
name: "InterpretedDanceScript", | ||
// Parsers that can parse this language. | ||
// This can be built-in parsers, or parsers you have contributed via this plugin. | ||
parsers: ["dance-parse"] | ||
} | ||
]; | ||
``` | ||
|
||
### `parsers` | ||
|
||
Parsers convert code as a string into an [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree). | ||
|
||
The key must match the name in the `parsers` array from `languages`. The value contains a parse function and an AST format name. | ||
|
||
```js | ||
export const parsers = { | ||
"dance-parse": { | ||
parse, | ||
// The name of the AST that | ||
astFormat: "dance-ast" | ||
} | ||
}; | ||
``` | ||
|
||
The signature of the `parse` function is: | ||
|
||
```ts | ||
function parse(text: string, parsers: object, options: object): AST; | ||
``` | ||
|
||
### `printers` | ||
|
||
Printers convert ASTs into a Prettier intermediate representation, also known as a Doc. | ||
|
||
The key must match the `astFormat` that the parser produces. The value contains an object with a `print` function and (optionally) an `embed` function. | ||
|
||
```js | ||
export const printers = { | ||
"dance-ast": { | ||
print, | ||
embed | ||
} | ||
}; | ||
``` | ||
|
||
Printing is a recursive process of coverting an AST node (represented by a path to that node) into a doc. The doc is constructed using the [builder commands](https://github.com/prettier/prettier/blob/master/commands.md): | ||
|
||
```js | ||
const { concat, join, line, ifBreak, group } = require("prettier").doc.builders; | ||
``` | ||
|
||
The signature of the `print` function is: | ||
|
||
```ts | ||
function print( | ||
// Path to the AST node to print | ||
path: FastPath, | ||
options: object, | ||
// Recursively print a child node | ||
print: (path: FastPath) => Doc | ||
): Doc; | ||
``` | ||
|
||
Check out [prettier-python's printer](https://github.com/prettier/prettier-python/blob/034ba8a9551f3fa22cead41b323be0b28d06d13b/src/printer.js#L174) as an example. | ||
|
||
Embedding refers to printing one language inside another. Examples of this are CSS-in-JS and Markdown code blocks. Plugins can switch to alternate languages using the `embed` function. Its signature is: | ||
|
||
```ts | ||
function embed( | ||
// Path to the current AST node | ||
path: FastPath, | ||
// Print a node with the current printer | ||
print: (path: FastPath) => Doc, | ||
// Parse and print some text using a different parser. | ||
// You should set `options.parser` to specify which parser to use. | ||
textToDoc: (text: string, options: object) => Doc, | ||
// Current options | ||
options: object | ||
): Doc | null; | ||
``` | ||
|
||
If you don't want to switch to a different parser, simply return `null` or `undefined`. | ||
|
||
## Testing Plugins | ||
|
||
Since plugins can be resolved using relative paths, when working on one you can do: | ||
|
||
```js | ||
const prettier = require("prettier"); | ||
const code = "(add 1 2)"; | ||
prettier.format(code, { | ||
parser: "lisp", | ||
plugins: ["."] | ||
}); | ||
``` | ||
|
||
This will resolve a plugin relative to the current working direcrory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"use strict"; | ||
|
||
module.exports = [ | ||
"language-js/parser-babylon", | ||
"language-js/parser-flow", | ||
"language-js/parser-typescript", | ||
"language-graphql/parser-graphql", | ||
"language-css/parser-postcss", | ||
"language-html/parser-parse5", | ||
"language-markdown/parser-markdown", | ||
"language-vue/parser-vue" | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.