Skip to content

Commit 78595ce

Browse files
committed
singleAttributePerLine
1 parent 8613c8f commit 78595ce

File tree

5 files changed

+116
-8
lines changed

5 files changed

+116
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Support for the `singleAttributePerLine` option.
12+
913
## [2.0.1] - 2022-03-22
1014

1115
### Added

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ The `prettier` executable is now installed and ready for use:
4646

4747
Below are the options (from [`src/plugin.ts`](src/plugin.ts)) that `@prettier/plugin-xml` currently supports:
4848

49-
| API Option | CLI Option | Default | Description |
50-
| -------------------------- | ------------------------------ | :--------: | ----------------------------------------------------------------------------------------------------- |
51-
| `bracketSameLine` | `--bracket-same-line` | `true` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#bracket-same-line)) |
52-
| `printWidth` | `--print-width` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
53-
| `tabWidth` | `--tab-width` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |
54-
| `xmlSelfClosingSpace` | `--xml-self-closing-space` | `true` | Adds a space before self-closing tags. |
55-
| `xmlWhitespaceSensitivity` | `--xml-whitespace-sensitivity` | `"strict"` | Options are `"strict"` and `"ignore"`. You may want `"ignore"`, [see below](#whitespace). |
49+
| API Option | CLI Option | Default | Description |
50+
| -------------------------- | ------------------------------ | :--------: | ------------------------------------------------------------------------------------------------------------- |
51+
| `bracketSameLine` | `--bracket-same-line` | `true` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#bracket-same-line)) |
52+
| `printWidth` | `--print-width` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
53+
| `singleAttributePerLine` | `--single-attribute-per-line` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#single-attribute-per-line)) |
54+
| `tabWidth` | `--tab-width` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |
55+
| `xmlSelfClosingSpace` | `--xml-self-closing-space` | `true` | Adds a space before self-closing tags. |
56+
| `xmlWhitespaceSensitivity` | `--xml-whitespace-sensitivity` | `"strict"` | Options are `"strict"` and `"ignore"`. You may want `"ignore"`, [see below](#whitespace). |
5657

5758
Any of these can be added to your existing [prettier configuration
5859
file](https://prettier.io/docs/en/configuration.html). For example:

src/printer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,12 @@ const printer: Printer = {
264264
const parts: Doc[] = [OPEN[0].image, Name[0].image];
265265

266266
if (attribute) {
267+
const separator = opts.singleAttributePerLine ? hardline : line;
267268
parts.push(
268-
indent([line, join(line, path.map(print, "children", "attribute"))])
269+
indent([
270+
line,
271+
join(separator, path.map(print, "children", "attribute"))
272+
])
269273
);
270274
}
271275

test/__snapshots__/format.test.ts.snap

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,96 @@ use {
227227
"
228228
`;
229229

230+
exports[`singleAttributePerLine => true 1`] = `
231+
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?>
232+
<!DOCTYPE module PUBLIC \\"-//Puppy Crawl//DTD Check Configuration 1.3//EN\\"
233+
\\"https://www.puppycrawl.com/dtds/configuration_1_3.dtd\\">
234+
<?xml-model href=\\"project.rnc\\" type=\\"application/relax-ng-compact-syntax\\"?>
235+
<!-- foo -->
236+
<svg
237+
xmlns=\\"http://www.w3.org/2000/svg\\"
238+
xmlns:xlink=\\"http://www.w3.org/1999/xlink\\"
239+
width=\\"200\\"
240+
height=\\"100\\"
241+
viewBox=\\"0 0 200 100\\"
242+
>
243+
<title>Style inheritance and the use element</title>
244+
<desc _attr=\\"attr\\">
245+
&anp; &#12345;
246+
<![CDATA[
247+
foo
248+
]]>
249+
bar
250+
</desc>
251+
<?pagebreak?>
252+
253+
<style />
254+
<style />
255+
<style type=\\"text/css\\">
256+
circle {
257+
stroke-opacity: 0.7;
258+
}
259+
.special circle {
260+
stroke: green;
261+
}
262+
use {
263+
stroke: purple;
264+
fill: orange;
265+
}
266+
</style>
267+
268+
<yaml
269+
myveryveryveryverylongattributename=\\"myveryveryveryverylongattributevalue\\"
270+
>
271+
- 1
272+
- 2
273+
- 3
274+
</style>
275+
276+
<!-- inner comment -->
277+
278+
<?pagebreak?>
279+
<g
280+
class=\\"special\\"
281+
style=\\"fill: blue\\"
282+
>
283+
<circle
284+
id=\\"c\\"
285+
cy=\\"50\\"
286+
cx=\\"50\\"
287+
r=\\"40\\"
288+
stroke-width=\\"20\\"
289+
/>
290+
</g>
291+
<use
292+
xlink:href=\\"#c\\"
293+
x=\\"100\\"
294+
/>
295+
<ignored>
296+
<!-- prettier-ignore-start -->
297+
< ignored />
298+
<!-- prettier-ignore-end -->
299+
</ignored>
300+
<p>
301+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at est eget
302+
enim consectetur accumsan. Aliquam pretium sodales ipsum quis dignissim. Sed
303+
id sem vel diam luctus fringilla. Aliquam quis egestas magna. Curabitur
304+
molestie lorem et odio porta, et molestie libero laoreet. Morbi rhoncus
305+
sagittis cursus. Nullam vehicula pretium consequat. Praesent porta ante at
306+
posuere sollicitudin. Nullam commodo tempor arcu, at condimentum neque
307+
elementum ut.
308+
</p>
309+
<span>content</span>
310+
311+
<div>
312+
even more
313+
<content />
314+
</div>
315+
</svg>
316+
<!-- bar -->
317+
"
318+
`;
319+
230320
exports[`xmlSelfClosingSpace => false 1`] = `
231321
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
232322
<!DOCTYPE module PUBLIC \\"-//Puppy Crawl//DTD Check Configuration 1.3//EN\\"

test/format.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,12 @@ test("bracketSameLine => true, xmlSelfClosingSpace => false", () => {
5252

5353
expect(formatted).toMatchSnapshot();
5454
});
55+
56+
test("singleAttributePerLine => true", () => {
57+
const formatted = format(fixture, {
58+
singleAttributePerLine: true,
59+
xmlWhitespaceSensitivity: "ignore"
60+
});
61+
62+
expect(formatted).toMatchSnapshot();
63+
});

0 commit comments

Comments
 (0)