From 57f702f7656e6fc03873f8121480c321b2f44c8c Mon Sep 17 00:00:00 2001 From: Dom Porada Date: Wed, 21 Jan 2026 18:07:32 +0100 Subject: [PATCH 1/4] Include available `printers` in plugin type declarations (#18706) Co-authored-by: fisker --- changelog_unreleased/api/18706.md | 15 +++++++++++ scripts/build/build-types.js | 45 ++++++++++++++++++++++++++----- tests/dts/unit/cases/parsers.ts | 4 +++ 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 changelog_unreleased/api/18706.md diff --git a/changelog_unreleased/api/18706.md b/changelog_unreleased/api/18706.md new file mode 100644 index 000000000..fc7009e78 --- /dev/null +++ b/changelog_unreleased/api/18706.md @@ -0,0 +1,15 @@ +#### Include available `printers` in plugin type declarations (#18706 by @porada) + + +```ts +// Input +import * as prettierPluginEstree from "prettier/plugins/estree"; + +// Prettier stable +// Property 'printers' does not exist on type 'typeof import("prettier/plugins/estree")'. ts(2339) +prettierPluginEstree.printers.estree; //=> any + +// Prettier main +prettierPluginEstree.printers.estree; //=> Printer +prettierPluginEstree.printers["estree-json"]; //=> Printer +``` diff --git a/scripts/build/build-types.js b/scripts/build/build-types.js index 86f1c9ef0..95b329b73 100644 --- a/scripts/build/build-types.js +++ b/scripts/build/build-types.js @@ -36,15 +36,19 @@ async function buildPluginTypes({ packageConfig, file: { input, output } }) { ); const plugin = pluginModule.default ?? pluginModule; const parserNames = Object.keys(plugin.parsers ?? {}); + const printerNames = Object.keys(plugin.printers ?? {}); + const typesImportPath = + packageConfig.packageName === "prettier" ? "../index.js" : "prettier"; - // We only add `parsers` to types file, printers should not be used alone - // For `estree` plugin, we just export an empty object to ensure it treated as a module - const code = - parserNames.length === 0 - ? "export {};" - : outdent` - import { Parser } from "${packageConfig.packageName === "prettier" ? "../index.js" : "prettier"}"; + // Export available `parsers` and `printers` as types. If none are available, + // export an empty module object to ensure the file is treated as a module + const types = []; + const declarations = []; + if (parserNames.length > 0) { + types.push("Parser"); + declarations.push( + outdent` export declare const parsers: { ${parserNames .map( @@ -53,6 +57,33 @@ async function buildPluginTypes({ packageConfig, file: { input, output } }) { ) .join("\n")} }; + `, + ); + } + + if (printerNames.length > 0) { + types.push("Printer"); + declarations.push( + outdent` + export declare const printers: { + ${printerNames + .map( + (printerName) => + `${" ".repeat(2)}${toPropertyKey(printerName)}: Printer;`, + ) + .join("\n")} + }; + `, + ); + } + + const code = + declarations.length === 0 + ? "export {};" + : outdent` + import { ${types.join(", ")} } from "${typesImportPath}"; + + ${declarations.join("\n\n")} `; await writeFile( diff --git a/tests/dts/unit/cases/parsers.ts b/tests/dts/unit/cases/parsers.ts index 725f6022c..9a690607f 100644 --- a/tests/dts/unit/cases/parsers.ts +++ b/tests/dts/unit/cases/parsers.ts @@ -1,4 +1,5 @@ import * as prettier from "../../../../dist/prettier/index.js"; +import { expectType } from "ts-expect"; import * as prettierPluginEstree from "../../../../dist/prettier/plugins/estree.js"; import * as prettierPluginBabel from "../../../../dist/prettier/plugins/babel.js"; @@ -104,3 +105,6 @@ prettier.format("hello world", { prettierPluginYaml, ], }); + +expectType(prettierPluginEstree.printers.estree); +expectType(prettierPluginEstree.printers["estree-json"]); From 90983f40dce5e20beea4e5618b5e0426a6a7f4f0 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 22 Jan 2026 01:43:10 +0800 Subject: [PATCH 2/4] Release 3.8.1 --- .github/ISSUE_TEMPLATE/formatting.md | 2 +- .github/ISSUE_TEMPLATE/integration.md | 2 +- CHANGELOG.md | 20 +++++++++++ package.json | 2 +- .../versioned_docs/version-stable/browser.md | 36 +++++++++---------- 5 files changed, 41 insertions(+), 21 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/formatting.md b/.github/ISSUE_TEMPLATE/formatting.md index cdaae2cfc..8a13880bf 100644 --- a/.github/ISSUE_TEMPLATE/formatting.md +++ b/.github/ISSUE_TEMPLATE/formatting.md @@ -26,7 +26,7 @@ Don't fill the form below manually! Let a program create a report for you: --> -**Prettier 3.8.0** +**Prettier 3.8.1** [Playground link](https://prettier.io/playground/#.....) ```sh diff --git a/.github/ISSUE_TEMPLATE/integration.md b/.github/ISSUE_TEMPLATE/integration.md index 043eba44d..2278a95d6 100644 --- a/.github/ISSUE_TEMPLATE/integration.md +++ b/.github/ISSUE_TEMPLATE/integration.md @@ -20,7 +20,7 @@ BEFORE SUBMITTING AN ISSUE: **Environments:** -- Prettier Version: 3.8.0 +- Prettier Version: 3.8.1 - Running Prettier via: - Runtime: - Operating System: diff --git a/CHANGELOG.md b/CHANGELOG.md index d19455804..09f8d0c77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,23 @@ +# 3.8.1 + +[diff](https://github.com/prettier/prettier/compare/3.8.0...3.8.1) + +#### API: Include available `printers` in plugin type declarations ([#18706](https://github.com/prettier/prettier/pull/18706) by [@porada](https://github.com/porada)) + + +```ts +// Input +import * as prettierPluginEstree from "prettier/plugins/estree"; + +// Prettier 3.8.0 +// Property 'printers' does not exist on type 'typeof import("prettier/plugins/estree")'. ts(2339) +prettierPluginEstree.printers.estree; //=> any + +// Prettier 3.8.1 +prettierPluginEstree.printers.estree; //=> Printer +prettierPluginEstree.printers["estree-json"]; //=> Printer +``` + # 3.8.0 [diff](https://github.com/prettier/prettier/compare/3.7.4...3.8.0) diff --git a/package.json b/package.json index d735b9fa9..332194311 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prettier", - "version": "3.9.0-dev", + "version": "3.8.1", "description": "Prettier is an opinionated code formatter", "bin": "./bin/prettier.cjs", "repository": "prettier/prettier", diff --git a/website/versioned_docs/version-stable/browser.md b/website/versioned_docs/version-stable/browser.md index c432d8af2..2c68aa954 100644 --- a/website/versioned_docs/version-stable/browser.md +++ b/website/versioned_docs/version-stable/browser.md @@ -21,7 +21,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@3.8.0/plugins](https://unpkg.com/browse/prettier@3.8.0/plugins). Note that `estree` plugin should be loaded when printing JavaScript, TypeScript, Flow, or JSON. +- **`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.8.1/plugins](https://unpkg.com/browse/prettier@3.8.1/plugins). Note that `estree` plugin should be loaded when printing JavaScript, TypeScript, Flow, or JSON. You need to load the ones that you’re going to use and pass them to `prettier.format` using the `plugins` option. @@ -32,8 +32,8 @@ See below for examples. ### Global ```html - - + +