Skip to content

Commit 15dd288

Browse files
JounQinSec-ant
authored andcommitted
feat: support shorthand for CallExpression as tag
close #137
1 parent d8b1fae commit 15dd288

File tree

5 files changed

+76
-9
lines changed

5 files changed

+76
-9
lines changed

.changeset/dirty-ghosts-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"prettier-plugin-embed": minor
3+
---
4+
5+
feat: support shorthand for CallExpression as tag

src/utils.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Expression, Node, TemplateLiteral } from "estree";
1+
import type { Expression, Node, Super, TemplateLiteral } from "estree";
22
import memoize from "micro-memoize";
33
import {
44
type AstPath,
@@ -220,6 +220,15 @@ export async function resolveEmbeddedOverrideOptions(
220220
}
221221
}
222222

223+
function* createExpressionGenerator(
224+
expression: Expression | Super,
225+
): Generator<Expression | Super> {
226+
yield expression;
227+
if (expression.type === "CallExpression") {
228+
yield* createExpressionGenerator(expression.callee);
229+
}
230+
}
231+
223232
export const compareTagExpressionToTagString = (() => {
224233
const ignoreSet = new Set([
225234
"start",
@@ -266,14 +275,16 @@ export const compareTagExpressionToTagString = (() => {
266275
return false;
267276
}
268277

269-
if (
270-
compareObjects(
271-
tagExpression as unknown as Record<string, unknown>,
272-
tagStringNode.tag as unknown as Record<string, unknown>,
273-
ignoreSet,
274-
)
275-
) {
276-
return true;
278+
for (const tagExp of createExpressionGenerator(tagExpression)) {
279+
if (
280+
compareObjects(
281+
tagExp as unknown as Record<string, unknown>,
282+
tagStringNode.tag as unknown as Record<string, unknown>,
283+
ignoreSet,
284+
)
285+
) {
286+
return true;
287+
}
277288
}
278289

279290
return false;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`fixtures > should work 1`] = `
4+
"sql.type(SESSION)\`
5+
INSERT INTO
6+
sessions (account_id, expires_at)
7+
VALUES
8+
(
9+
\${accountId},
10+
\${expiresAt.toJSON()}
11+
) RETURNING id,
12+
created_at AS "createdAt",
13+
updated_at AS "updatedAt",
14+
expires_at AS "expiresAt",
15+
account_id AS "accountId"
16+
\`;
17+
"
18+
`;

test/fixtures.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import fs from "node:fs/promises";
2+
import path from "node:path";
3+
4+
import prettier from "prettier";
5+
import sql from "prettier-plugin-sql";
6+
import * as embed from "../src";
7+
8+
import { describe, expect, it } from "vitest";
9+
10+
describe("fixtures", () => {
11+
it("should work", async () => {
12+
for (const file of await fs.readdir("test/fixtures")) {
13+
const filepath = path.resolve("test/fixtures", file);
14+
const code = await fs.readFile(filepath, "utf-8");
15+
const formatted = await prettier.format(code, {
16+
filepath,
17+
plugins: [embed, sql],
18+
embeddedSqlTags: ["sql.type"],
19+
});
20+
expect(formatted).toMatchSnapshot();
21+
}
22+
});
23+
});

test/fixtures/137.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sql.type(SESSION)`
2+
INSERT INTO sessions (account_id, expires_at)
3+
VALUES (${accountId}, ${expiresAt.toJSON()})
4+
RETURNING
5+
id,
6+
created_at AS "createdAt",
7+
updated_at AS "updatedAt",
8+
expires_at AS "expiresAt",
9+
account_id AS "accountId"
10+
`;

0 commit comments

Comments
 (0)