Skip to content

Commit

Permalink
Support xmlSelfClosingSpace options
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jan 19, 2020
1 parent ab9128e commit b415dcf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

- A dependency on the `@xml-tools/parser` package to handle parsing.
- We now register as supporting `.svg` and `.xsd` files.
- The `xmlSelfClosingSpace` option for specifying whether or not to add spaces before self-closing element tags.

## [0.3.0] - 2019-11-14

Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ The `prettier` executable is now installed and ready for use:

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

| Name | Default | Description |
| ------------ | :-----: | ------------------------------------------------------------------------------------------------ |
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
| `tabWidth` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |
| Name | Default | Description |
| --------------------- | :-----: | ------------------------------------------------------------------------------------------------ |
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
| `tabWidth` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |
| `xmlSelfClosingSpace` | `true` | Adds a space before self-closing tags. |

Any of these can be added to your existing [prettier configuration
file](https://prettier.io/docs/en/configuration.html). For example:
Expand Down
9 changes: 8 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ const plugin = {
print
}
},
options: {},
options: {
xmlSelfClosingSpace: {
type: "boolean",
category: "Global",
default: true,
description: "Adds a space before self-closing tags."
}
},
defaultOptions: {
printWidth: 80,
tabWidth: 2
Expand Down
10 changes: 6 additions & 4 deletions src/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ const nodes = {
}

if (SLASH_CLOSE) {
return group(concat(parts.concat(line, SLASH_CLOSE[0].image)));
const space = opts.xmlSelfClosingSpace ? line : softline;
return group(concat(parts.concat(space, SLASH_CLOSE[0].image)));
}

if (Object.keys(content[0].children).length === 0) {
return group(concat(parts.concat(line, "/>")));
const space = opts.xmlSelfClosingSpace ? line : softline;
return group(concat(parts.concat(space, "/>")));
}

return group(
Expand All @@ -162,7 +164,6 @@ const nodes = {
},
prolog: (path, opts, print) => {
const { XMLDeclOpen, attribute, SPECIAL_CLOSE } = path.getValue().children;

const parts = [XMLDeclOpen[0].image];

if (attribute) {
Expand All @@ -176,7 +177,8 @@ const nodes = {
);
}

parts.push(softline, SPECIAL_CLOSE[0].image);
const space = opts.xmlSelfClosingSpace ? line : softline;
parts.push(space, SPECIAL_CLOSE[0].image);

return group(concat(parts));
}
Expand Down

0 comments on commit b415dcf

Please sign in to comment.