Skip to content

Commit

Permalink
feat: format yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
daflyinbed committed Sep 25, 2024
1 parent 5bf7332 commit 545a09b
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 13 deletions.
40 changes: 40 additions & 0 deletions fixtures/input/yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
invoice : 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
-

sku : BL394D
? quantity
: 4
description : Basketball
? price
: 450.00


-
sku : BL4438H
quantity : 1
description: Super Hoop
price : 2392.00


tax : 251.42
total : 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
"BAD": 'BAD'
33 changes: 33 additions & 0 deletions fixtures/output/all/yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
invoice: 34843
date: 2001-01-23
bill-to: &id001
given: Chris
family: Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city: Royal Oak
state: MI
postal: 48046
ship-to: *id001
product:
- sku: BL394D
quantity: 4
description: Basketball
price: 450.00

- sku: BL4438H
quantity: 1
description: Super Hoop
price: 2392.00

tax: 251.42
total: 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
BAD: BAD
40 changes: 40 additions & 0 deletions fixtures/output/no-style/yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
invoice : 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
-

sku : BL394D
? quantity
: 4
description : Basketball
? price
: 450.00


-
sku : BL4438H
quantity : 1
description: Super Hoop
price : 2392.00


tax : 251.42
total : 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
BAD: BAD
33 changes: 33 additions & 0 deletions fixtures/output/tab-single-quotes/yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
invoice: 34843
date: 2001-01-23
bill-to: &id001
given: Chris
family: Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city: Royal Oak
state: MI
postal: 48046
ship-to: *id001
product:
- sku: BL394D
quantity: 4
description: Basketball
price: 450.00

- sku: BL4438H
quantity: 1
description: Super Hoop
price: 2392.00

tax: 251.42
total: 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
BAD: BAD
27 changes: 22 additions & 5 deletions src/configs/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
GLOB_TOML,
GLOB_VUE,
GLOB_XML,
GLOB_YAML,
} from "../globs";
import { pluginFormat, pluginStylistic } from "../plugins";
import { ensurePackages, isPackageInScope, parserPlain } from "../utils";
Expand All @@ -40,6 +41,7 @@ export async function formatters(
graphql: true,
html: true,
markdown: true,
yaml: true,
slidev: isPackageInScope("@slidev/cli"),
svg: isPrettierPluginXmlInScope,
xml: isPrettierPluginXmlInScope,
Expand All @@ -65,11 +67,7 @@ export async function formatters(
needPluginXml ? "@prettier/plugin-xml" : undefined,
]);

if (
options.slidev &&
options.markdown !== true &&
options.markdown !== "prettier"
)
if (options.slidev && options.markdown !== true)
throw new Error(
"`slidev` option only works when `markdown` is enabled with `prettier`",
);
Expand Down Expand Up @@ -390,5 +388,24 @@ export async function formatters(
},
});
}

if (options.yaml) {
configs.push({
files: [GLOB_YAML],
languageOptions: {
parser: parserPlain,
},
name: "xwbx/formatter/yaml",
rules: {
"format/prettier": [
"error",
{
...prettierOptions,
parser: "yaml",
},
],
},
});
}
return configs;
}
2 changes: 0 additions & 2 deletions src/configs/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export async function yaml(
rules: {
"style/spaced-comment": "off",

"yaml/block-mapping": "error",
"yaml/block-sequence": "error",
"yaml/no-empty-key": "error",
"yaml/no-empty-sequence-entry": "error",
"yaml/no-irregular-whitespace": "error",
Expand Down
14 changes: 8 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,40 @@ export interface OptionsFormatters {
*
* Currently only support Prettier.
*/
css?: "prettier" | boolean;
css?: boolean;

/**
* Enable formatting support for HTML.
*
* Currently only support Prettier.
*/
html?: "prettier" | boolean;
html?: boolean;

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

/**
* Enable formatting support for SVG.
*
* Currently only support Prettier.
*/
svg?: "prettier" | boolean;
svg?: boolean;

/**
* Enable formatting support for Markdown.
*
* Currently only support Prettier.
*/
markdown?: "prettier" | boolean;
markdown?: boolean;

/**
* Enable formatting support for GraphQL.
*/
graphql?: "prettier" | boolean;
graphql?: boolean;

/**
* Custom options for Prettier.
Expand All @@ -117,6 +117,8 @@ export interface OptionsFormatters {
* Currently only support Prettier.
*/
astro?: "prettier" | boolean;

yaml?: boolean;
}

export interface OptionsComponentExts {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ runWithConfig("all", {
vue: true,
astro: true,
formatters: true,
yaml: true,
});
runWithConfig("no-style", {
typescript: true,
vue: true,
astro: true,
formatters: false,
yaml: true,
});
runWithConfig("tab-single-quotes", {
typescript: true,
vue: true,
astro: true,
yaml: true,
formatters: {
prettierOptions: {
useTabs: true,
Expand Down

0 comments on commit 545a09b

Please sign in to comment.