Skip to content
Draft
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ file](https://prettier.io/docs/en/configuration.html). For example:
}
```

For a TypeScript configuration, import the plugin to add its XML options to
Prettier's `Options` type:

```ts
import xml from "@prettier/plugin-xml";
import type { Options } from "prettier";

export default {
plugins: [xml],
xmlWhitespaceSensitivity: "ignore"
} satisfies Options;
```

Or, they can be passed to `prettier` as arguments:

```bash
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"jest": "^30.0.0",
"linguist-languages": "^9.0.0",
"lint-staged": "^17.0.2",
"prettier": "^3.6.2"
"prettier": "^3.6.2",
"typescript": "^5.9.3"
},
"jest": {
"testRegex": ".test.js$",
Expand Down
24 changes: 24 additions & 0 deletions test/types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";

test("XML options augment Prettier's Options", () => {
const result = spawnSync(
process.execPath,
[
fileURLToPath(
new URL("../node_modules/typescript/lib/tsc.js", import.meta.url)
),
"--project",
fileURLToPath(new URL("./types/tsconfig.json", import.meta.url))
],
{ encoding: "utf8" }
);

expect({
status: result.status,
output: result.stdout + result.stderr
}).toEqual({
status: 0,
output: ""
});
});
73 changes: 73 additions & 0 deletions test/types/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import plugin from "@prettier/plugin-xml";
import { format, type Options, type Plugin } from "prettier";

const xmlPlugin: Plugin = plugin;

const options: Options = {
plugins: [xmlPlugin],
printWidth: 80,
xmlWhitespaceSensitivity: "strict",
xmlSelfClosingSpace: true,
xmlSortAttributesByKey: false,
xmlQuoteAttributes: "preserve"
};

const whitespace: "strict" | "preserve" | "ignore" | undefined =
options.xmlWhitespaceSensitivity;
const selfClosingSpace: boolean | undefined = options.xmlSelfClosingSpace;
const sortAttributes: boolean | undefined = options.xmlSortAttributesByKey;
const quotes: "preserve" | "single" | "double" | undefined =
options.xmlQuoteAttributes;

format("<foo/>", options);
format("<foo/>", {} satisfies Options);
format("<foo/>", {
xmlWhitespaceSensitivity: undefined,
xmlSelfClosingSpace: undefined,
xmlSortAttributesByKey: undefined,
xmlQuoteAttributes: undefined
} satisfies Options);

for (const xmlWhitespaceSensitivity of [
"strict",
"preserve",
"ignore"
] as const) {
format("<foo/>", { xmlWhitespaceSensitivity } satisfies Options);
}

for (const xmlQuoteAttributes of ["preserve", "single", "double"] as const) {
format("<foo/>", { xmlQuoteAttributes } satisfies Options);
}

for (const enabled of [true, false]) {
format("<foo/>", {
xmlSelfClosingSpace: enabled,
xmlSortAttributesByKey: enabled
} satisfies Options);
}

format("<foo/>", {
// @ts-expect-error Only the plugin's whitespace modes are valid.
xmlWhitespaceSensitivity: "collapse"
} satisfies Options);

format("<foo/>", {
// @ts-expect-error Only the plugin's quote modes are valid.
xmlQuoteAttributes: "auto"
} satisfies Options);

format("<foo/>", {
// @ts-expect-error This option accepts booleans, not strings.
xmlSelfClosingSpace: "true"
} satisfies Options);

format("<foo/>", {
// @ts-expect-error This option accepts booleans, not numbers.
xmlSortAttributesByKey: 1
} satisfies Options);

format("<foo/>", {
// @ts-expect-error The augmentation must preserve Prettier's built-in options.
printWidth: "wide"
} satisfies Options);
11 changes: 11 additions & 0 deletions test/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"exactOptionalPropertyTypes": true,
"module": "NodeNext",
"noEmit": true,
"strict": true,
"target": "ES2022",
"types": []
},
"files": ["plugin.ts"]
}
9 changes: 9 additions & 0 deletions types/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import type { Plugin } from "prettier";

declare module "prettier" {
interface Options {
xmlWhitespaceSensitivity?: "strict" | "preserve" | "ignore" | undefined;
xmlSelfClosingSpace?: boolean | undefined;
xmlSortAttributesByKey?: boolean | undefined;
xmlQuoteAttributes?: "preserve" | "single" | "double" | undefined;
}
}

declare const plugin: Plugin;
export default plugin;
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2946,6 +2946,11 @@ type-fest@^0.21.3:
resolved "https://registry.npmjs.org/type-fest/-/type-fest-0.21.3.tgz"
integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==

typescript@^5.9.3:
version "5.9.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.9.3.tgz#5b4f59e15310ab17a216f5d6cf53ee476ede670f"
integrity sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==

unrs-resolver@^1.12.1:
version "1.12.2"
resolved "https://registry.yarnpkg.com/unrs-resolver/-/unrs-resolver-1.12.2.tgz#a6c6888396abba5adaac4cab6587df866f1d7afd"
Expand Down