From 5296012645a9fe07077e8e9515d490e98f96bfc1 Mon Sep 17 00:00:00 2001 From: boinsoft Date: Sun, 2 Feb 2025 00:28:48 -0800 Subject: [PATCH 1/2] feat: add XML format via prettier --- README.md | 1 + example/package.json | 1 + example/src/hello.xml | 4 ++++ example/tools/format/BUILD.bazel | 1 + format/private/format.sh | 1 + format/private/formatter_binary.bzl | 1 + format/test/BUILD.bazel | 1 + format/test/format_test.bats | 7 +++++++ 8 files changed, 17 insertions(+) create mode 100644 example/src/hello.xml diff --git a/README.md b/README.md index 461ea52f..4fcd0303 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ Linters which are not language-specific: | TSX | [Prettier] | [ESLint] | | TypeScript | [Prettier] | [ESLint] | | YAML | [yamlfmt] | | +| XML | [@prettier/plugin-xml]| | [prettier]: https://prettier.io [google-java-format]: https://github.com/google/google-java-format diff --git a/example/package.json b/example/package.json index ce81cc21..0522d5de 100644 --- a/example/package.json +++ b/example/package.json @@ -9,6 +9,7 @@ "eslint": "^9.16.0", "prettier": "^2.8.7", "prettier-plugin-sql": "^0.14.0", + "@prettier/plugin-xml": "^0.10.0", "stylelint": "^16", "stylelint-config-standard": "^36.0.1", "typescript": "4.9.5", diff --git a/example/src/hello.xml b/example/src/hello.xml new file mode 100644 index 00000000..7ab400f6 --- /dev/null +++ b/example/src/hello.xml @@ -0,0 +1,4 @@ + + + todo + diff --git a/example/tools/format/BUILD.bazel b/example/tools/format/BUILD.bazel index ea03d4ab..fad3f062 100644 --- a/example/tools/format/BUILD.bazel +++ b/example/tools/format/BUILD.bazel @@ -93,6 +93,7 @@ format_multirun( swift = ":swiftformat", terraform = "@aspect_rules_lint//format:terraform", visibility = ["//:__subpackages__"], + xml = ":prettier", yaml = "@aspect_rules_lint//format:yamlfmt", ) diff --git a/format/private/format.sh b/format/private/format.sh index 7008b0f7..c763bd85 100755 --- a/format/private/format.sh +++ b/format/private/format.sh @@ -142,6 +142,7 @@ function ls-files { 'TypeScript') patterns=('*.ts' '*.cts' '*.mts') ;; 'Vue') patterns=('*.vue') ;; 'YAML') patterns=('*.yml' '*.yaml' '.clang-format' '.clang-tidy' '.gemrc') ;; + 'XML') patterns=('*.xml') ;; # Note: terraform fmt cannot handle all HCL files such as .terraform.lock # "Only .tf and .tfvars files can be processed with terraform fmt" diff --git a/format/private/formatter_binary.bzl b/format/private/formatter_binary.bzl index cbb20fad..baa3c081 100644 --- a/format/private/formatter_binary.bzl +++ b/format/private/formatter_binary.bzl @@ -26,6 +26,7 @@ TOOLS = { "Cuda": "clang-format", "YAML": "yamlfmt", "Rust": "rustfmt", + "XML": "prettier", } # Provided to make install more convenient diff --git a/format/test/BUILD.bazel b/format/test/BUILD.bazel index a508b554..9a08339f 100644 --- a/format/test/BUILD.bazel +++ b/format/test/BUILD.bazel @@ -57,5 +57,6 @@ format_multirun( swift = ":mock_swiftformat.sh", # TODO: this attribute should be renamed to hcl terraform = ":mock_terraform-fmt.sh", + xml = ":mock_prettier.sh", yaml = ":mock_yamlfmt.sh", ) diff --git a/format/test/format_test.bats b/format/test/format_test.bats index f4416185..58b64a1f 100644 --- a/format/test/format_test.bats +++ b/format/test/format_test.bats @@ -40,6 +40,13 @@ bats_load_library "bats-assert" assert_output --partial "+ prettier --write .bcr/README.md CONTRIBUTING.md README.md" } +@test "should run prettier on XML" { + run bazel run //format/test:format_XML_with_prettier + assert_success + + assert_output --partial "+ prettier --write example/src/hello.xml" +} + @test "should run prettier on CSS" { run bazel run //format/test:format_CSS_with_prettier assert_success From 46bf1dde4cca44e04dc2b2c5e5cd32a11dd5cfb5 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Fri, 14 Feb 2025 10:39:57 -0800 Subject: [PATCH 2/2] fixes --- README.md | 3 ++- docs/format.md | 3 ++- format/private/format.sh | 4 +++- format/test/format_test.bats | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4fcd0303..92d730b8 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Linters which are not language-specific: | TSX | [Prettier] | [ESLint] | | TypeScript | [Prettier] | [ESLint] | | YAML | [yamlfmt] | | -| XML | [@prettier/plugin-xml]| | +| XML | [prettier/plugin-xml] | | [prettier]: https://prettier.io [google-java-format]: https://github.com/google/google-java-format @@ -69,6 +69,7 @@ Linters which are not language-specific: [ktlint]: https://github.com/pinterest/ktlint [buildifier]: https://github.com/keith/buildifier-prebuilt [prettier-plugin-sql]: https://github.com/un-ts/prettier +[prettier/plugin-xml]: https://github.com/prettier/plugin-xml [gofmt]: https://pkg.go.dev/cmd/gofmt [gofumpt]: https://github.com/mvdan/gofumpt [jsonnetfmt]: https://github.com/google/go-jsonnet diff --git a/docs/format.md b/docs/format.md index 7fa8a759..beb3dc87 100644 --- a/docs/format.md +++ b/docs/format.md @@ -23,7 +23,7 @@ See the example/tools/format/BUILD file in this repo for full examples of declar load("@aspect_rules_lint//format:defs.bzl", "languages") languages(name, c, cc, css, cuda, go, graphql, html, java, javascript, jsonnet, kotlin, markdown, - protocol_buffer, python, rust, scala, shell, sql, starlark, swift, terraform, yaml) + protocol_buffer, python, rust, scala, shell, sql, starlark, swift, terraform, xml, yaml) Language attributes that may be passed to [format_multirun](#format_multirun) or [format_test](#format_test). @@ -65,6 +65,7 @@ Some languages have dialects: | starlark | a `buildifier` binary, or any other tool that has a matching command-line interface. | Label | optional | `None` | | swift | a `swiftformat` binary, or any other tool that has a matching command-line interface. | Label | optional | `None` | | terraform | a `terraform-fmt` binary, or any other tool that has a matching command-line interface. Use `@aspect_rules_lint//format:terraform` to choose the built-in tool. | Label | optional | `None` | +| xml | a `prettier` binary, or any other tool that has a matching command-line interface. | Label | optional | `None` | | yaml | a `yamlfmt` binary, or any other tool that has a matching command-line interface. Use `@aspect_rules_lint//format:yamlfmt` to choose the built-in tool. | Label | optional | `None` | diff --git a/format/private/format.sh b/format/private/format.sh index c763bd85..7264e010 100755 --- a/format/private/format.sh +++ b/format/private/format.sh @@ -142,7 +142,9 @@ function ls-files { 'TypeScript') patterns=('*.ts' '*.cts' '*.mts') ;; 'Vue') patterns=('*.vue') ;; 'YAML') patterns=('*.yml' '*.yaml' '.clang-format' '.clang-tidy' '.gemrc') ;; - 'XML') patterns=('*.xml') ;; + # Note: https://github.com/github-linguist/linguist/blob/559a6426942abcae16b6d6b328147476432bf6cb/lib/linguist/languages.yml#L7767-L7882 + # has a giant list of patterns. We arbitrarily choose some "common" ones. + 'XML') patterns=('*.xml' '*.xsd') ;; # Note: terraform fmt cannot handle all HCL files such as .terraform.lock # "Only .tf and .tfvars files can be processed with terraform fmt" diff --git a/format/test/format_test.bats b/format/test/format_test.bats index 58b64a1f..0c979a53 100644 --- a/format/test/format_test.bats +++ b/format/test/format_test.bats @@ -44,7 +44,7 @@ bats_load_library "bats-assert" run bazel run //format/test:format_XML_with_prettier assert_success - assert_output --partial "+ prettier --write example/src/hello.xml" + assert_output --partial "+ prettier --write example/checkstyle-suppressions.xml" } @test "should run prettier on CSS" {