Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions 15 changelog_unreleased/api/18706.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#### Include available `printers` in plugin type declarations (#18706 by @porada)

<!-- prettier-ignore -->
```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
```
45 changes: 38 additions & 7 deletions 45 scripts/build/builders/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,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(
Expand All @@ -51,6 +55,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(path.join(packageConfig.distDirectory, output), `${code}\n`);
Expand Down
4 changes: 4 additions & 0 deletions 4 tests/dts/unit/cases/parsers.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -104,3 +105,6 @@ prettier.format("hello world", {
prettierPluginYaml,
],
});

expectType<prettier.Printer>(prettierPluginEstree.printers.estree);
expectType<prettier.Printer>(prettierPluginEstree.printers["estree-json"]);
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.