mcky / prettier-plugin-groq

Prettier GROQ plugin
4 stars 0 forks source link

Prettier for GROQ

prettier-plugin-groq is a prettier plugin for formatting GROQ queries.

Work in progress

The outputted queries are still considered unstable, it's still not smart enough to know when keep some queries as one-liners or to when to break. That said apart from stripping out comments it shouldn't destructively modify any queries. It's been tested against sanity's Query cheat sheet to verify stripWhitespace(format(query)) === stripWhitespace(query).

Examples

👍 handled

- *[_id=='foo'] | order(date asc) { _id, slug, 'title': name, foo { bar}  }
+ *[_id == "foo"] | order(date asc) {
+   _id,
+   slug,
+   "title": name,
+   foo { bar }
+ }
- *[] {
-     id,
-     title
- }
+ *[] { id, title }
- *[ _type match "foo*" && !( _id in path("drafts.**" ) ) ] |
- order(createdAt asc) {
-     "same": same,
-     "foo": {
-         bar,
-         "baz": { bing
-         }
-     },
-     "title": coalesce(
-       title.fi,
-       title.en.gb
-     ),
-     ...*[other != false],
-   member-> {
-     "profilePicture": profilePicture.asset-> { url }
-   },
-   body[]{
-     ..., markDefs[]{
-       ...,
-       _type == "person" => {"person": @.reference-> {name}}
-     }
-   },
- } [0..99]
+ *[_type match "foo*" && !(_id in path("drafts.**"))] | order(createdAt asc) {
+   same,
+   "foo": {
+     bar,
+     "baz": { bing }
+   },
+   "title": coalesce(title.fi, title.en.gb),
+   ...*[other != false],
+   member-> {
+     profilePicture.asset-> {
+       url
+     }
+   },
+   body[] {
+     ...,
+     markDefs[] {
+       ...,
+       _type == "person" => {
+         "person": @["reference"]-> {
+           name
+         }
+       }
+     }
+   }
+ }[0..99]

👎 in progress

Not splitting arrays/objects when it isn't needed

- *[_type in ["movie", "person"]]
+ *[_type in [
+   "movie",
+   "person"
+ ]]
- *[_type == "movie"] { _id, _type, title }
+ *[_type == "movie"] {
+   _id,
+   _type,
+   title
+ }
- *{ "title": coalesce(title.fi, title.en) }
+ * {
+   "title": coalesce(title.fi, title.en)
+ }
- [1, 2] + [3, 4]
+ [
+   1,
+   2
+ ] + [
+   3,
+   4
+ ]
- { "a": 1, "b": 2 } + { "c": 3 }
+ {
+   "a": 1,
+   "b": 2
+ } + { "c": 3 }

Useful GROQ links: