Skip to content

Commit f6b83ce

Browse files
committed
Don't add spaces around -> and ->> operators in PostgreSQL
Fixes #68
1 parent 5f57415 commit f6b83ce

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

src/syntax/expr.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export const exprMap: CstToDocMap<AllExprNodes> = {
9393
// String concatenation with whitespace (MySQL / MariaDB)
9494
return print.spaced(["left", "right"]);
9595
}
96+
if (isString(node.operator) && isCompactOp(node.operator)) {
97+
// Some operators are better formatted without spaces around them
98+
return print(["left", "operator", "right"]);
99+
}
96100
return print.spaced(["left", "operator", "right"]);
97101
},
98102
prefix_op_expr: (print, node) =>
@@ -307,3 +311,5 @@ const isFunctionContext = (
307311
};
308312

309313
const isBooleanOp = ({ name }: Keyword) => name === "AND" || name === "OR";
314+
315+
const isCompactOp = (op: string) => op === "->" || op === "->>";

test/expr/expr.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ describe("expr", () => {
233233
await testPostgresql(`SELECT 5 OPERATOR(+) 6`);
234234
await testPostgresql(`SELECT x OPERATOR(my_schema.>>) y FROM tbl`);
235235
});
236+
237+
it(`formats -> and ->> operators without spaces`, async () => {
238+
await testPostgresql(`SELECT user->'name'`);
239+
await testPostgresql(`SELECT col->>'field'->>'field' FROM tbl`);
240+
});
236241
});
237242

238243
describe("MySQL", () => {

0 commit comments

Comments
 (0)