Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ Wylie Conlon <wylie@superblockshq.com>
Xin Hu <hoosin.git@gmail.com>
Zhongxian Liang <zhongxian.liang@shopee.com>
0xflotus <0xflotus@gmail.com>
Swayam Shah(dlh.io) <swayam@aicg.com>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ All fields are optional and all fields that are not specified will be filled wit
- [**`identifierCase`**](docs/identifierCase.md) uppercases or lowercases identifiers. (**experimental!**)
- [**`indentStyle`**](docs/indentStyle.md) defines overall indentation style. (**deprecated!**)
- [**`logicalOperatorNewline`**](docs/logicalOperatorNewline.md) newline before or after boolean operator (AND, OR, XOR).
- [**`commaNewline`**](docs/commaNewline.md) newline before or after comma seperating multiple columns/tables.
- [**`expressionWidth`**](docs/expressionWidth.md) maximum number of characters in parenthesized expressions to be kept on single line.
- [**`linesBetweenQueries`**](docs/linesBetweenQueries.md) how many newlines to insert between queries.
- [**`denseOperators`**](docs/denseOperators.md) packs operators densely without spaces.
Expand Down
38 changes: 38 additions & 0 deletions docs/commaNewline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# commaNewline

Decides newline placement before or after commas seperating columns/tables.

## Options

- `"after"` (default) adds newline after the comma.
- `"before"` adds newline before the comma with a following space.
Comment thread
swayam-aicg marked this conversation as resolved.
Outdated

### after

```sql
SELECT
name,
age,
height
FROM
persons
WHERE
age > 10
AND height < 150
OR occupation IS NULL
```

### before

```sql
SELECT
name
, age
, height
Comment thread
swayam-aicg marked this conversation as resolved.
Outdated
FROM
persons
WHERE
age > 10 AND
height < 150 OR
occupation IS NULL
```
2 changes: 2 additions & 0 deletions src/FormatOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type DataTypeCase = KeywordCase;
export type FunctionCase = KeywordCase;

export type LogicalOperatorNewline = 'before' | 'after';
export type CommaNewline = LogicalOperatorNewline;

export interface FormatOptions {
tabWidth: number;
Expand All @@ -20,6 +21,7 @@ export interface FormatOptions {
functionCase: FunctionCase;
indentStyle: IndentStyle;
logicalOperatorNewline: LogicalOperatorNewline;
commaNewline: CommaNewline;
expressionWidth: number;
linesBetweenQueries: number;
denseOperators: boolean;
Expand Down
6 changes: 5 additions & 1 deletion src/formatter/ExpressionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,11 @@ export default class ExpressionFormatter {

private formatComma(_node: CommaNode) {
if (!this.inline) {
this.layout.add(WS.NO_SPACE, ',', WS.NEWLINE, WS.INDENT);
if (this.cfg.commaNewline === 'before') {
this.layout.add(WS.NEWLINE, WS.INDENT, ',', WS.SPACE);
} else {
this.layout.add(WS.NO_SPACE, ',', WS.NEWLINE, WS.INDENT);
}
} else {
this.layout.add(WS.NO_SPACE, ',', WS.SPACE);
}
Expand Down
1 change: 1 addition & 0 deletions src/sqlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const defaultOptions: FormatOptions = {
functionCase: 'preserve',
indentStyle: 'standard',
logicalOperatorNewline: 'before',
commaNewline: 'after',
expressionWidth: 50,
linesBetweenQueries: 1,
denseOperators: false,
Expand Down
2 changes: 2 additions & 0 deletions test/behavesLikeSqlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import supportsIndentStyle from './options/indentStyle.js';
import supportsLinesBetweenQueries from './options/linesBetweenQueries.js';
import supportsNewlineBeforeSemicolon from './options/newlineBeforeSemicolon.js';
import supportsLogicalOperatorNewline from './options/logicalOperatorNewline.js';
import supportsCommaNewline from './options/commaNewline.js';
import supportsParamTypes from './options/paramTypes.js';
import supportsWindowFunctions from './features/windowFunctions.js';
import supportsFunctionCase from './options/functionCase.js';
Expand All @@ -36,6 +37,7 @@ export default function behavesLikeSqlFormatter(format: FormatFn) {
supportsExpressionWidth(format);
supportsNewlineBeforeSemicolon(format);
supportsLogicalOperatorNewline(format);
supportsCommaNewline(format);
supportsParamTypes(format);
supportsWindowFunctions(format);

Expand Down
37 changes: 37 additions & 0 deletions test/options/commaNewline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import dedent from 'dedent-js';

import { FormatFn } from '../../src/sqlFormatter.js';

export default function supportsCommaNewline(format: FormatFn) {
it('by default adds newline after comma', () => {
const result = format(`SELECT id, first_name, last_name, email FROM users;`);
expect(result).toBe(
dedent`
SELECT
id,
first_name,
last_name,
email
FROM
users;
`
);
});

it('supports newline before comma', () => {
const result = format(`SELECT id, first_name, last_name, email FROM users;`, {
commaNewline: 'before',
});
expect(result).toBe(
dedent`
SELECT
id
, first_name
, last_name
, email
FROM
users;
Comment thread
swayam-aicg marked this conversation as resolved.
Outdated
`
);
});
}