From ae48d997e18098b182f680862ec6959a12d71bd5 Mon Sep 17 00:00:00 2001 From: Connor Date: Mon, 15 Jul 2024 08:09:38 -0700 Subject: [PATCH] feat: add GraphQL format support (#319) (#322) Prettier supports GraphQL natively. The file extensions are registered with the formatter and a basic format test is added. --- README.md | 3 ++- docs/format.md | 3 ++- example/src/hello.graphql | 7 +++++++ example/tools/format/BUILD.bazel | 1 + format/private/filter.jq | 1 + format/private/format.sh | 1 + format/private/formatter_binary.bzl | 1 + format/test/BUILD.bazel | 1 + format/test/format_test.bats | 7 +++++++ 9 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 example/src/hello.graphql diff --git a/README.md b/README.md index bd090872..cb6c1c4e 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,12 @@ Features: New tools are being added frequently, so check this page again! | Language | Formatter | Linter(s) | -| ---------------------- | --------------------- | ---------------- | +|------------------------| --------------------- |------------------| | C / C++ | [clang-format] | ([#112]) | | Cuda | [clang-format] | | | CSS, Less, Sass | [Prettier] | | | Go | [gofmt] or [gofumpt] | | +| GraphQL | [Prettier] | | | HCL (Hashicorp Config) | [terraform] fmt | | | HTML | [Prettier] | | | JSON | [Prettier] | | diff --git a/docs/format.md b/docs/format.md index ccee4fa1..0ab2dba0 100644 --- a/docs/format.md +++ b/docs/format.md @@ -47,7 +47,7 @@ format_multirun( ## languages
-languages(name, cc, css, cuda, go, html, java, javascript, jsonnet, kotlin, markdown,
+languages(name, cc, css, cuda, go, graphql, html, java, javascript, jsonnet, kotlin, markdown,
           protocol_buffer, python, rust, scala, shell, sql, starlark, swift, terraform, yaml)
 
@@ -74,6 +74,7 @@ Some languages have dialects: | css | a prettier binary, or any other tool that has a matching command-line interface. | Label | optional | None | | cuda | a clang-format binary, or any other tool that has a matching command-line interface. | Label | optional | None | | go | a gofmt binary, or any other tool that has a matching command-line interface. Use @aspect_rules_lint//format:gofumpt to choose the built-in tool. | Label | optional | None | +| graphql | a prettier binary, or any other tool that has a matching command-line interface. | Label | optional | None | | html | a prettier binary, or any other tool that has a matching command-line interface. | Label | optional | None | | java | a java-format binary, or any other tool that has a matching command-line interface. | Label | optional | None | | javascript | a prettier binary, or any other tool that has a matching command-line interface. | Label | optional | None | diff --git a/example/src/hello.graphql b/example/src/hello.graphql new file mode 100644 index 00000000..c075f056 --- /dev/null +++ b/example/src/hello.graphql @@ -0,0 +1,7 @@ +query +{ + world(greeting: + "hello") { + ... on Hello { salutation } + } +} diff --git a/example/tools/format/BUILD.bazel b/example/tools/format/BUILD.bazel index 43d4a87a..f7f26561 100644 --- a/example/tools/format/BUILD.bazel +++ b/example/tools/format/BUILD.bazel @@ -59,6 +59,7 @@ format_multirun( css = ":prettier", cuda = "@llvm_toolchain_llvm//:bin/clang-format", go = "@aspect_rules_lint//format:gofumpt", + graphql = ":prettier", html = ":prettier", # You can use standard gofmt instead of stricter gofumpt: # go = "@go_sdk//:bin/gofmt", diff --git a/format/private/filter.jq b/format/private/filter.jq index 44e7b705..cac07b8e 100644 --- a/format/private/filter.jq +++ b/format/private/filter.jq @@ -5,6 +5,7 @@ with_entries(select(.key | IN( "Cuda", "Markdown", "Go", + "GraphQL", "HCL", "HTML", "Java", diff --git a/format/private/format.sh b/format/private/format.sh index bf24d899..bb712af1 100755 --- a/format/private/format.sh +++ b/format/private/format.sh @@ -67,6 +67,7 @@ function ls-files { 'Cuda') patterns=('*.cu' '*.cuh') ;; 'CSS') patterns=('*.css') ;; 'Go') patterns=('*.go') ;; + 'GraphQL') patterns=('*.graphql' '*.gql' '*.graphqls') ;; 'HTML') patterns=('*.html' '*.hta' '*.htm' '*.html.hl' '*.inc' '*.xht' '*.xhtml') ;; 'JSON') patterns=('.all-contributorsrc' '.arcconfig' '.auto-changelog' '.c8rc' '.htmlhintrc' '.imgbotconfig' '.nycrc' '.tern-config' '.tern-project' '.watchmanconfig' 'Pipfile.lock' 'composer.lock' 'deno.lock' 'flake.lock' 'mcmod.info' '*.json' '*.4DForm' '*.4DProject' '*.avsc' '*.geojson' '*.gltf' '*.har' '*.ice' '*.JSON-tmLanguage' '*.jsonl' '*.mcmeta' '*.tfstate' '*.tfstate.backup' '*.topojson' '*.webapp' '*.webmanifest' '*.yy' '*.yyp') ;; 'Java') patterns=('*.java' '*.jav' '*.jsh') ;; diff --git a/format/private/formatter_binary.bzl b/format/private/formatter_binary.bzl index 688f4cbd..33ef7f6e 100644 --- a/format/private/formatter_binary.bzl +++ b/format/private/formatter_binary.bzl @@ -7,6 +7,7 @@ TOOLS = { "JavaScript": "prettier", "Markdown": "prettier", "CSS": "prettier", + "GraphQL": "prettier", "HTML": "prettier", "Python": "ruff", "Starlark": "buildifier", diff --git a/format/test/BUILD.bazel b/format/test/BUILD.bazel index 381ba601..a508b554 100644 --- a/format/test/BUILD.bazel +++ b/format/test/BUILD.bazel @@ -40,6 +40,7 @@ format_multirun( css = ":mock_prettier.sh", cuda = ":mock_clang-format.sh", go = ":mock_gofmt.sh", + graphql = ":mock_prettier.sh", html = ":mock_prettier.sh", java = ":mock_java-format.sh", javascript = ":mock_prettier.sh", diff --git a/format/test/format_test.bats b/format/test/format_test.bats index 7598e43e..6977f2d0 100644 --- a/format/test/format_test.bats +++ b/format/test/format_test.bats @@ -56,6 +56,13 @@ bats_load_library "bats-assert" assert_output --partial "+ prettier --write example/src/index.html" } +@test "should run prettier on GraphQL" { + run bazel run //format/test:format_GraphQL_with_prettier + assert_success + + assert_output --partial "+ prettier --write example/src/hello.graphql" +} + @test "should run prettier on SQL" { run bazel run //format/test:format_SQL_with_prettier assert_success