Skip to content

Commit

Permalink
feat: support shorthand for CallExpression as tag
Browse files Browse the repository at this point in the history
close #137
  • Loading branch information
JounQin authored and Sec-ant committed Feb 23, 2025
1 parent d8b1fae commit 15dd288
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-ghosts-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prettier-plugin-embed": minor
---

feat: support shorthand for CallExpression as tag
29 changes: 20 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Expression, Node, TemplateLiteral } from "estree";
import type { Expression, Node, Super, TemplateLiteral } from "estree";
import memoize from "micro-memoize";
import {
type AstPath,
Expand Down Expand Up @@ -220,6 +220,15 @@ export async function resolveEmbeddedOverrideOptions(
}
}

function* createExpressionGenerator(
expression: Expression | Super,
): Generator<Expression | Super> {
yield expression;
if (expression.type === "CallExpression") {
yield* createExpressionGenerator(expression.callee);
}
}

export const compareTagExpressionToTagString = (() => {
const ignoreSet = new Set([
"start",
Expand Down Expand Up @@ -266,14 +275,16 @@ export const compareTagExpressionToTagString = (() => {
return false;
}

if (
compareObjects(
tagExpression as unknown as Record<string, unknown>,
tagStringNode.tag as unknown as Record<string, unknown>,
ignoreSet,
)
) {
return true;
for (const tagExp of createExpressionGenerator(tagExpression)) {
if (
compareObjects(
tagExp as unknown as Record<string, unknown>,
tagStringNode.tag as unknown as Record<string, unknown>,
ignoreSet,
)
) {
return true;
}
}

return false;
Expand Down
18 changes: 18 additions & 0 deletions test/__snapshots__/fixtures.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`fixtures > should work 1`] = `
"sql.type(SESSION)\`
INSERT INTO
sessions (account_id, expires_at)
VALUES
(
\${accountId},
\${expiresAt.toJSON()}
) RETURNING id,
created_at AS "createdAt",
updated_at AS "updatedAt",
expires_at AS "expiresAt",
account_id AS "accountId"
\`;
"
`;
23 changes: 23 additions & 0 deletions test/fixtures.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fs from "node:fs/promises";
import path from "node:path";

import prettier from "prettier";
import sql from "prettier-plugin-sql";
import * as embed from "../src";

import { describe, expect, it } from "vitest";

describe("fixtures", () => {
it("should work", async () => {
for (const file of await fs.readdir("test/fixtures")) {
const filepath = path.resolve("test/fixtures", file);
const code = await fs.readFile(filepath, "utf-8");
const formatted = await prettier.format(code, {
filepath,
plugins: [embed, sql],
embeddedSqlTags: ["sql.type"],
});
expect(formatted).toMatchSnapshot();
}
});
});
10 changes: 10 additions & 0 deletions test/fixtures/137.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sql.type(SESSION)`
INSERT INTO sessions (account_id, expires_at)
VALUES (${accountId}, ${expiresAt.toJSON()})
RETURNING
id,
created_at AS "createdAt",
updated_at AS "updatedAt",
expires_at AS "expiresAt",
account_id AS "accountId"
`;

0 comments on commit 15dd288

Please sign in to comment.