Skip to content

Commit

Permalink
feat: support xml (#472)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
zanfee and antfu committed May 13, 2024
1 parent 76443cc commit 19b4e39
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"json",
"jsonc",
"yaml",
"toml"
"toml",
"xml"
],

"pair-diff.patterns": [
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- Opinionated, but [very customizable](#customization)
- [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), compose easily!
- Optional [React](#react), [Svelte](#svelte), [UnoCSS](#unocss), [Astro](#astro), [Solid](#solid) support
- Optional [formatters](#formatters) support for formatting CSS, HTML, etc.
- Optional [formatters](#formatters) support for formatting CSS, HTML, XML, etc.
- **Style principle**: Minimal for reading, stable for diff, consistent
- Sorted imports, dangling commas
- Single quotes, no semi
Expand Down Expand Up @@ -143,6 +143,7 @@ Add the following settings to your `.vscode/settings.json`:
"jsonc",
"yaml",
"toml",
"xml",
"gql",
"graphql",
"astro"
Expand Down
9 changes: 9 additions & 0 deletions fixtures/input/xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<store>
<product id= "01" category="books" stock="available"><name>Effective Java</name><price currency='USD'>45.00</price></product>
<product id="02" category="electronics" stock="unavailable" ><name>Bluetooth Speaker</name><price currency='USD'>120.00</price></product>
<product id="03" category="books" stock="available" ><name>Clean Code</name>
<price currency='USD'>33.50</price>
</product>
<membership level="gold" id="001"> <user Name="John Doe"/> </membership>
<membership level="silver" id="002"> <user Name="Jane Smith"></user> </membership>
</store>
20 changes: 20 additions & 0 deletions fixtures/output/with-formatters/xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<store>
<product id="01" category="books" stock="available">
<name>Effective Java</name>
<price currency="USD">45.00</price>
</product>
<product id="02" category="electronics" stock="unavailable">
<name>Bluetooth Speaker</name>
<price currency="USD">120.00</price>
</product>
<product id="03" category="books" stock="available">
<name>Clean Code</name>
<price currency="USD">33.50</price>
</product>
<membership level="gold" id="001">
<user Name="John Doe" />
</membership>
<membership level="silver" id="002">
<user Name="Jane Smith" />
</membership>
</store>
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"peerDependencies": {
"@eslint-react/eslint-plugin": "^1.5.8",
"@prettier/plugin-xml": "^3.4.1",
"@unocss/eslint-plugin": ">=0.50.0",
"astro-eslint-parser": "^0.16.3",
"eslint": ">=8.40.0",
Expand All @@ -57,6 +58,9 @@
"@eslint-react/eslint-plugin": {
"optional": true
},
"@prettier/plugin-xml": {
"optional": true
},
"@unocss/eslint-plugin": {
"optional": true
},
Expand Down Expand Up @@ -133,6 +137,7 @@
"@antfu/ni": "^0.21.12",
"@eslint-react/eslint-plugin": "^1.5.11",
"@eslint/config-inspector": "^0.4.8",
"@prettier/plugin-xml": "^3.4.1",
"@stylistic/eslint-plugin-migrate": "^2.1.0",
"@types/eslint": "^8.56.10",
"@types/fs-extra": "^11.0.4",
Expand Down
40 changes: 36 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/cli/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const vscodeSettingsString = `
"jsonc",
"yaml",
"toml",
"xml",
"gql",
"graphql",
"astro"
Expand Down
34 changes: 33 additions & 1 deletion src/configs/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isPackageExists } from 'local-pkg'
import { GLOB_ASTRO, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS } from '../globs'
import { GLOB_ASTRO, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_XML } from '../globs'
import type { VendoredPrettierOptions } from '../vender/prettier-types'
import { ensurePackages, interopDefault, parserPlain } from '../utils'
import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem } from '../types'
Expand All @@ -17,13 +17,15 @@ export async function formatters(
html: true,
markdown: true,
slidev: isPackageExists('@slidev/cli'),
xml: isPackageExists('@prettier/plugin-xml'),
}
}

await ensurePackages([
'eslint-plugin-format',
options.markdown && options.slidev ? 'prettier-plugin-slidev' : undefined,
options.astro ? 'prettier-plugin-astro' : undefined,
options.xml ? '@prettier/plugin-xml' : undefined,
])

if (options.slidev && options.markdown !== true && options.markdown !== 'prettier')
Expand All @@ -50,6 +52,13 @@ export async function formatters(
options.prettierOptions || {},
)

const prettierXmlOptions = {
xmlQuoteAttributes: 'double',
xmlSelfClosingSpace: true,
xmlSortAttributesByKey: false,
xmlWhitespaceSensitivity: 'ignore',
}

const dprintOptions = Object.assign(
{
indentWidth: typeof indent === 'number' ? indent : 2,
Expand Down Expand Up @@ -142,6 +151,29 @@ export async function formatters(
})
}

if (options.xml) {
configs.push({
files: [GLOB_XML],
languageOptions: {
parser: parserPlain,
},
name: 'antfu/formatter/xml',
rules: {
'format/prettier': [
'error',
{
...prettierXmlOptions,
...prettierOptions,
parser: 'xml',
plugins: [
'@prettier/plugin-xml',
],
},
],
},
})
}

if (options.markdown) {
const formater = options.markdown === true
? 'prettier'
Expand Down
2 changes: 2 additions & 0 deletions src/globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const GLOB_SVELTE = '**/*.svelte'
export const GLOB_VUE = '**/*.vue'
export const GLOB_YAML = '**/*.y?(a)ml'
export const GLOB_TOML = '**/*.toml'
export const GLOB_XML = '**/*.xml'
export const GLOB_HTML = '**/*.htm?(l)'
export const GLOB_ASTRO = '**/*.astro'
export const GLOB_GRAPHQL = '**/*.{g,graph}ql'
Expand All @@ -46,6 +47,7 @@ export const GLOB_ALL_SRC = [
GLOB_SVELTE,
GLOB_VUE,
GLOB_YAML,
GLOB_XML,
GLOB_HTML,
]

Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export interface OptionsFormatters {
*/
html?: 'prettier' | boolean

/**
* Enable formatting support for XML.
*
* Currently only support Prettier.
*/
xml?: 'prettier' | boolean

/**
* Enable formatting support for Markdown.
*
Expand Down
26 changes: 24 additions & 2 deletions src/vender/prettier-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface VendoredPrettierOptionsRequired {
*/
bracketSpacing: boolean
/**
* Put the `>` of a multi-line HTML (HTML, JSX, Vue, Angular) element at the end of the last line instead of being
* Put the `>` of a multi-line HTML (HTML, XML, JSX, Vue, Angular) element at the end of the last line instead of being
* alone on the next line (does not apply to self closing elements).
*/
bracketSameLine: boolean
Expand Down Expand Up @@ -93,10 +93,31 @@ export interface VendoredPrettierOptionsRequired {
*/
vueIndentScriptAndStyle: boolean
/**
* Enforce single attribute per line in HTML, Vue and JSX.
* Enforce single attribute per line in HTML, XML, Vue and JSX.
* @default false
*/
singleAttributePerLine: boolean

/**
* How to handle whitespaces in XML.
* @default "preserve"
*/
xmlQuoteAttributes: 'single' | 'double' | 'preserve'
/**
* Whether to put a space inside the brackets of self-closing XML elements.
* @default true
*/
xmlSelfClosingSpace: boolean
/**
* Whether to sort attributes by key in XML elements.
* @default false
*/
xmlSortAttributesByKey: boolean
/**
* How to handle whitespaces in XML.
* @default "ignore"
*/
xmlWhitespaceSensitivity: 'ignore' | 'strict' | 'preserve'
}

export type BuiltInParserName =
Expand All @@ -122,6 +143,7 @@ export type BuiltInParserName =
| 'scss'
| 'typescript'
| 'vue'
| 'xml'
| 'yaml'

// This utility is here to handle the case where you have an explicit union
Expand Down

0 comments on commit 19b4e39

Please sign in to comment.