Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gritql as a formatter/linter for embedded languages #525

Open
musjj opened this issue Sep 27, 2024 · 1 comment
Open

gritql as a formatter/linter for embedded languages #525

musjj opened this issue Sep 27, 2024 · 1 comment

Comments

@musjj
Copy link

musjj commented Sep 27, 2024

One of my pet peeves is formatting languages embedded in strings.

For example, with sqlx in rust:

sqlx::query("SELECT * FROM users WHERE email = ?")

Or jq in the shell:

jq '.[0] | {message: .commit.message, name: .commit.committer.name}'

There's currently no generalized tools for formatting this kind of strings, so you usually end up doing it manually.

But I've been thinking that gritql seems like the perfect tool for dealing with this problem. You could write patterns to extract these strings, define an external command to pipe the string to, then use output of the command for your needs.

It could, for example, look like this:

`sqlx::query("$query")` where {
  $formatted = $query |> $(sqlfluff format), // this pipes $query to a shell command
  $query => $formatted
}
@morgante
Copy link
Contributor

I think formatting itself is out of scope for GritQL, but this is a scenario we could likely support in our upcoming workflow engine + SDK. The syntax would look something like this:

grit.rust`sqlx::query("$query")`.each((binding, context) => {
  const query = context.findVar("$query");
  const formatted = $`sqlformat < ${query.text()}`.text();
  query.replace(formatted);
}).run();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants