Skip to content

Commit d7d96ab

Browse files
committed
Merge PR 32: Keep parentheses around function argument select statements
2 parents 626354c + 48f6150 commit d7d96ab

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"name": "prettier-plugin-sql-cst",
33
"version": "0.11.6",
44
"description": "Prettier plugin for SQL",
5-
"author": "Rene Saarsoo <nene@triin.net>",
5+
"contributors": [
6+
"Bowen Parnell <bparnell@4tel.com.au>",
7+
"Rene Saarsoo <nene@triin.net>"
8+
],
69
"license": "GPL-3.0-or-later",
710
"main": "dist/index.js",
811
"types": "dist/index.d.ts",

src/node_utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export const isStringLiteral = is("string_literal");
2020
export const isArraySubscript = is("array_subscript");
2121
export const isAsClause = is("as_clause");
2222
export const isParenExpr = is("paren_expr");
23+
export const isSelectStmt = is("select_stmt");
24+
export const isCompoundSelectStmt = is("compound_select_stmt");
2325
export const isListExpr = is("list_expr");
2426
export const isCreateFunctionStmt = is("create_function_stmt");
2527
export const isLanguageClause = is("language_clause");

src/syntax/expr.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
isFuncArgs,
2020
isListExpr,
2121
isProgram,
22+
isSelectStmt,
23+
isCompoundSelectStmt,
2224
} from "../node_utils";
2325
import { isString, last } from "../utils";
2426

@@ -48,7 +50,9 @@ export const exprMap: CstToDocMap<AllExprNodes> = {
4850
// Discard unnecessary parenthesis around function arguments
4951
if (
5052
isListExpr(path.getParentNode(0)) &&
51-
isFuncArgs(path.getParentNode(1))
53+
isFuncArgs(path.getParentNode(1)) &&
54+
!isSelectStmt(node.expr) &&
55+
!isCompoundSelectStmt(node.expr)
5256
) {
5357
return print("expr");
5458
}

test/expr/expr.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ describe("expr", () => {
110110
`);
111111
});
112112

113+
it(`preserves parenthesis around SELECT inside function arguments`, async () => {
114+
await test(dedent`
115+
SELECT coalesce((SELECT foo FROM bar), 'default') FROM tbl
116+
`);
117+
});
118+
119+
it(`preserves parenthesis around compound-SELECT inside function arguments`, async () => {
120+
await test(dedent`
121+
SELECT
122+
coalesce(
123+
'',
124+
(
125+
SELECT x FROM xs
126+
UNION
127+
SELECT y FROM ys
128+
)
129+
)
130+
FROM tbl
131+
`);
132+
});
133+
113134
describe("case", () => {
114135
it(`formats CASE expression always on multiple lines`, async () => {
115136
await test(dedent`

0 commit comments

Comments
 (0)