Skip to content

Commit

Permalink
Release 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Jul 5, 2023
1 parent 44979b0 commit 186e096
Show file tree
Hide file tree
Showing 15 changed files with 444 additions and 227 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Don't fill the form below manually! Let a program create a report for you:
-->

**Prettier 2.8.8**
**Prettier 3.0.0**
[Playground link](https://prettier.io/playground/#.....)

```sh
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ BEFORE SUBMITTING AN ISSUE:

**Environments:**

- Prettier Version: 2.8.8
- Prettier Version: 3.0.0
- Running Prettier via: <!-- CLI, Node.js API, Browser API, etc. -->
- Runtime: <!-- Node.js v14, Chrome v83, etc. -->
- Operating System: <!-- Windows, Linux, macOS, etc. -->
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 3.0.0

[diff](https://github.com/prettier/prettier/compare/3.0.0-alpha.6...3.0.0)

🔗 [Release Notes](https://prettier.io/blog/2023/07/05/3.0.0.html)

# 2.8.8

This version is a republished version of v2.8.7.
Expand Down
28 changes: 14 additions & 14 deletions docs/browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Required options:

- **[`parser`](options.md#parser) (or [`filepath`](options.md#file-path))**: One of these options has to be specified for Prettier to know which parser to use.

- **`plugins`**: Unlike the `format` function from the [Node.js-based API](api.md#prettierformatsource--options), this function doesn’t load plugins automatically. The `plugins` option is required because all the parsers included in the Prettier package come as plugins (for reasons of file size). These plugins are files in <https://unpkg.com/browse/prettier@2.8.8/plugins/>
- **`plugins`**: Unlike the `format` function from the [Node.js-based API](api.md#prettierformatsource--options), this function doesn’t load plugins automatically. The `plugins` option is required because all the parsers included in the Prettier package come as plugins (for reasons of file size). These plugins are files in <https://unpkg.com/browse/prettier@3.0.0/plugins/>

You need to load the ones that you’re going to use and pass them to `prettier.format` using the `plugins` option.

Expand All @@ -29,8 +29,8 @@ See below for examples.
### Global

```html
<script src="https://unpkg.com/prettier@2.8.8/standalone.js"></script>
<script src="https://unpkg.com/prettier@2.8.8/plugins/graphql.js"></script>
<script src="https://unpkg.com/prettier@3.0.0/standalone.js"></script>
<script src="https://unpkg.com/prettier@3.0.0/plugins/graphql.js"></script>
<script>
(async () => {
const formatted = await prettier.format("type Query { hello: String }", {
Expand All @@ -47,8 +47,8 @@ Note that the [`unpkg` field](https://unpkg.com/#examples) in Prettier’s `pack

```html
<script type="module">
import * as prettier from "https://unpkg.com/prettier@2.8.8/standalone.mjs";
import pluginGraphql from "https://unpkg.com/prettier@2.8.8/plugins/graphql.mjs";
import * as prettier from "https://unpkg.com/prettier@3.0.0/standalone.mjs";
import pluginGraphql from "https://unpkg.com/prettier@3.0.0/plugins/graphql.mjs";
const formatted = await prettier.format("type Query { hello: String }", {
parser: "graphql",
Expand All @@ -61,8 +61,8 @@ Note that the [`unpkg` field](https://unpkg.com/#examples) in Prettier’s `pack

```js
define([
"https://unpkg.com/prettier@2.8.8/standalone.js",
"https://unpkg.com/prettier@2.8.8/plugins/graphql.js",
"https://unpkg.com/prettier@3.0.0/standalone.js",
"https://unpkg.com/prettier@3.0.0/plugins/graphql.js",
], async (prettier, ...plugins) => {
const formatted = await prettier.format("type Query { hello: String }", {
parser: "graphql",
Expand All @@ -89,8 +89,8 @@ This syntax doesn’t necessarily work in the browser, but it can be used when b
### Worker

```js
importScripts("https://unpkg.com/prettier@2.8.8/standalone.js");
importScripts("https://unpkg.com/prettier@2.8.8/plugins/graphql.js");
importScripts("https://unpkg.com/prettier@3.0.0/standalone.js");
importScripts("https://unpkg.com/prettier@3.0.0/plugins/graphql.js");
(async () => {
const formatted = await prettier.format("type Query { hello: String }", {
parser: "graphql",
Expand All @@ -105,8 +105,8 @@ If you want to format [embedded code](options.md#embedded-language-formatting),

```html
<script type="module">
import * as prettier from "https://unpkg.com/prettier@2.8.8/standalone.mjs";
import pluginBabel from "https://unpkg.com/prettier@2.8.8/plugins/babel.mjs";
import * as prettier from "https://unpkg.com/prettier@3.0.0/standalone.mjs";
import pluginBabel from "https://unpkg.com/prettier@3.0.0/plugins/babel.mjs";
console.log(
await prettier.format("const html=/* HTML */ `<DIV> </DIV>`", {
Expand All @@ -122,9 +122,9 @@ The HTML code embedded in JavaScript stays unformatted because the `html` parser

```html
<script type="module">
import * as prettier from "https://unpkg.com/prettier@2.8.8/standalone.mjs";
import pluginBabel from "https://unpkg.com/prettier@2.8.8/plugins/babel.mjs";
import pluginHtml from "https://unpkg.com/prettier@2.8.8/plugins/html.mjs";
import * as prettier from "https://unpkg.com/prettier@3.0.0/standalone.mjs";
import pluginBabel from "https://unpkg.com/prettier@3.0.0/plugins/babel.mjs";
import pluginHtml from "https://unpkg.com/prettier@3.0.0/plugins/html.mjs";
console.log(
await prettier.format("const html=/* HTML */ `<DIV> </DIV>`", {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier",
"version": "3.0.0-alpha.6",
"version": "3.0.0",
"description": "Prettier is an opinionated code formatter",
"bin": "./bin/prettier.cjs",
"repository": "prettier/prettier",
Expand Down
63 changes: 28 additions & 35 deletions website/versioned_docs/version-stable/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ original_id: api
If you want to run Prettier programmatically, check this page out.

```js
const prettier = require("prettier");
import * as prettier from "prettier";
```

## `prettier.format(source, options)`

`format` is used to format text using Prettier. `options.parser` must be set according to the language you are formatting (see the [list of available parsers](options.md#parser)). Alternatively, `options.filepath` can be specified for Prettier to infer the parser from the file extension. Other [options](options.md) may be provided to override the defaults.

```js
prettier.format("foo ( );", { semi: false, parser: "babel" });
// -> "foo()"
await prettier.format("foo ( );", { semi: false, parser: "babel" });
// -> 'foo()\n'
```

## `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 `--check` or `--list-different` parameter in the CLI and is useful for running Prettier in CI scenarios.
`check` checks to see if the file has been formatted with Prettier given those options and returns a `Promise<boolean>`. This is similar to the `--check` or `--list-different` parameter in the CLI and is useful for running Prettier in CI scenarios.

## `prettier.formatWithCursor(source [, options])`

Expand All @@ -30,7 +30,7 @@ prettier.format("foo ( );", { semi: false, parser: "babel" });
The `cursorOffset` option should be provided, to specify where the cursor is. This option cannot be used with `rangeStart` and `rangeEnd`.

```js
prettier.formatWithCursor(" 1", { cursorOffset: 2, parser: "babel" });
await prettier.formatWithCursor(" 1", { cursorOffset: 2, parser: "babel" });
// -> { formatted: '1;\n', cursorOffset: 1 }
```

Expand All @@ -46,10 +46,9 @@ The promise will be rejected if there was an error parsing the configuration fil
If `options.useCache` is `false`, all caching will be bypassed.

```js
const text = fs.readFileSync(filePath, "utf8");
prettier.resolveConfig(filePath).then((options) => {
const formatted = prettier.format(text, options);
});
const text = await fs.readFile(filePath, "utf8");
const options = await prettier.resolveConfig(filePath);
const formatted = await prettier.format(text, options);
```

If `options.editorconfig` is `true` and an [`.editorconfig` file](https://editorconfig.org/) is in your project, Prettier will parse it and convert its properties to the corresponding Prettier configuration. This configuration will be overridden by `.prettierrc`, etc. Currently, the following EditorConfig properties are supported:
Expand All @@ -59,8 +58,6 @@ If `options.editorconfig` is `true` and an [`.editorconfig` file](https://editor
- `indent_size`/`tab_width`
- `max_line_length`

Use `prettier.resolveConfig.sync(filePath [, options])` if you’d like to use sync version.

## `prettier.resolveConfigFile([filePath])`

`resolveConfigFile` can be used to find the path of the Prettier configuration file that will be used when resolving the config (i.e. when calling `resolveConfig`). A promise is returned which will resolve to:
Expand All @@ -73,13 +70,10 @@ The promise will be rejected if there was an error parsing the configuration fil
The search starts at `process.cwd()`, or at `filePath` if provided. Please see the [cosmiconfig docs](https://github.com/davidtheclark/cosmiconfig#explorersearch) for details on how the resolving works.

```js
prettier.resolveConfigFile(filePath).then((configFile) => {
// you got the path of the configuration file
});
const configFile = await prettier.resolveConfigFile(filePath);
// you got the path of the configuration file
```

Use `prettier.resolveConfigFile.sync([filePath])` if you’d like to use sync version.

## `prettier.clearConfigCache()`

When Prettier loads configuration files and plugins, the file system structure is cached for performance. This function will clear the cache. Generally this is only needed for editor integrations that know that the file system has changed since the last format took place.
Expand All @@ -88,36 +82,33 @@ When Prettier loads configuration files and plugins, the file system structure i

`getFileInfo` can be used by editor extensions to decide if a particular file needs to be formatted. This method returns a promise, which resolves to an object with the following properties:

```typescript
```ts
{
ignored: boolean,
inferredParser: string | null,
ignored: boolean;
inferredParser: string | null;
}
```

The promise will be rejected if the type of `filePath` is not `string`.

Setting `options.ignorePath` (`string`) and `options.withNodeModules` (`boolean`) influence the value of `ignored` (`false` by default).
Setting `options.ignorePath` (`string | string[]`) and `options.withNodeModules` (`boolean`) influence the value of `ignored` (`false` by default).

If the given `filePath` is ignored, the `inferredParser` is always `null`.

Providing [plugin](plugins.md) paths in `options.plugins` (`string[]`) helps extract `inferredParser` for files that are not supported by Prettier core.

When setting `options.resolveConfig` (`boolean`, default `false`), Prettier will resolve the configuration for the given `filePath`. This is useful, for example, when the `inferredParser` might be overridden for a subset of files.

Use `prettier.getFileInfo.sync(filePath [, options])` if you’d like to use sync version.
When setting `options.resolveConfig` (`boolean`, default `true`) to `false`, Prettier will not search for configuration file. This can be useful if this function is only used to check if file is ignored.

## `prettier.getSupportInfo()`

Returns an object representing the options, parsers, languages and file types Prettier supports.
Returns a promise which resolves to an object representing the options, parsers, languages and file types Prettier supports.

The support information looks like this:

```typescript
```ts
{
languages: Array<{
name: string;
since?: string;
parsers: string[];
group?: string;
tmScope?: string;
Expand All @@ -135,16 +126,17 @@ The support information looks like this:

<a name="custom-parser-api"></a>

## Custom Parser API (deprecated)
## Custom Parser API (removed)

_Will be removed in v3.0.0 (superseded by the Plugin API)_
_Removed in v3.0.0 (superseded by the Plugin API)_

Before [plugins](plugins.md) were a thing, Prettier had a similar but more limited feature called custom parsers. It will be removed in v3.0.0 as its functionality is a subset of what the Plugin API does. If you used it, please check the example below on how to migrate.
Before [plugins](plugins.md) were a thing, Prettier had a similar but more limited feature called custom parsers. It’s been removed in v3.0.0 as its functionality was a subset of what the Plugin API did. If you used it, please check the example below on how to migrate.

❌ Custom parser API (deprecated):
❌ Custom parser API (removed):

```js
import { format } from "prettier";

format("lodash ( )", {
parser(text, { babel }) {
const ast = babel(text);
Expand All @@ -159,21 +151,22 @@ format("lodash ( )", {

```js
import { format } from "prettier";
import * as parserBabel from "prettier/parser-babel.js";
import * as pluginBabel from "prettier/plugins/babel";

const myCustomPlugin = {
parsers: {
"my-custom-parser": {
parse(text) {
const ast = parserBabel.parsers.babel.parse(text);
async parse(text) {
const ast = await pluginBabel.parsers.babel.parse(text);
ast.program.body[0].expression.callee.name = "_";
return ast;
},
astFormat: "estree",
},
},
};
format("lodash ( )", {

await format("lodash ( )", {
parser: "my-custom-parser",
plugins: [myCustomPlugin],
});
Expand All @@ -182,4 +175,4 @@ format("lodash ( )", {

> Note: Overall, doing codemods this way isn’t recommended. Prettier uses the location data of AST nodes for many things like preserving blank lines and attaching comments. When the AST is modified after the parsing, the location data often gets out of sync, which may lead to unpredictable results. Consider using [jscodeshift](https://github.com/facebook/jscodeshift) if you need codemods.
As part of the deprecated Custom parser API, it is possible to pass a path to a module exporting a `parse` function via the `--parser` option. Use the `--plugin` CLI option or the `plugins` API option instead to [load plugins](plugins.md#using-plugins).
As part of the removed Custom parser API, it was previously possible to pass a path to a module exporting a `parse` function via the `--parser` option. Use the `--plugin` CLI option or the `plugins` API option instead to [load plugins](plugins.md#using-plugins).
Loading

0 comments on commit 186e096

Please sign in to comment.