Skip to content

Commit 31609bb

Browse files
feat: Ability to control newline placement around comma when seperating multiple columns/tables
Add option that can control if the newline is present after the comma or before the comma when listing multiple columns/tables
1 parent 653f62c commit 31609bb

6 files changed

Lines changed: 49 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ All fields are optional and all fields that are not specified will be filled wit
183183
- [**`identifierCase`**](docs/identifierCase.md) uppercases or lowercases identifiers. (**experimental!**)
184184
- [**`indentStyle`**](docs/indentStyle.md) defines overall indentation style. (**deprecated!**)
185185
- [**`logicalOperatorNewline`**](docs/logicalOperatorNewline.md) newline before or after boolean operator (AND, OR, XOR).
186+
- [**`commaNewline`**](docs/commaNewline.md) newline before or after comma seperating multiple columns/tables.
186187
- [**`expressionWidth`**](docs/expressionWidth.md) maximum number of characters in parenthesized expressions to be kept on single line.
187188
- [**`linesBetweenQueries`**](docs/linesBetweenQueries.md) how many newlines to insert between queries.
188189
- [**`denseOperators`**](docs/denseOperators.md) packs operators densely without spaces.

docs/commaNewline.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# commaNewline
2+
3+
Decides newline placement before or after commas seperating columns/tables.
4+
5+
## Options
6+
7+
- `"after"` (default) adds newline after the comma.
8+
- `"before"` adds newline before the comma with a following space.
9+
10+
### after
11+
12+
```sql
13+
SELECT
14+
name,
15+
age,
16+
height
17+
FROM
18+
persons
19+
WHERE
20+
age > 10
21+
AND height < 150
22+
OR occupation IS NULL
23+
```
24+
25+
### before
26+
27+
```sql
28+
SELECT
29+
name
30+
, age
31+
, height
32+
FROM
33+
persons
34+
WHERE
35+
age > 10 AND
36+
height < 150 OR
37+
occupation IS NULL
38+
```

src/FormatOptions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type DataTypeCase = KeywordCase;
1010
export type FunctionCase = KeywordCase;
1111

1212
export type LogicalOperatorNewline = 'before' | 'after';
13+
export type CommaNewline = LogicalOperatorNewline;
1314

1415
export interface FormatOptions {
1516
tabWidth: number;
@@ -20,6 +21,7 @@ export interface FormatOptions {
2021
functionCase: FunctionCase;
2122
indentStyle: IndentStyle;
2223
logicalOperatorNewline: LogicalOperatorNewline;
24+
commaNewline: CommaNewline;
2325
expressionWidth: number;
2426
linesBetweenQueries: number;
2527
denseOperators: boolean;

src/formatter/ExpressionFormatter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,11 @@ export default class ExpressionFormatter {
341341

342342
private formatComma(_node: CommaNode) {
343343
if (!this.inline) {
344-
this.layout.add(WS.NO_SPACE, ',', WS.NEWLINE, WS.INDENT);
344+
if (this.cfg.commaNewline === 'before') {
345+
this.layout.add(WS.NEWLINE, WS.INDENT, ',', WS.SPACE);
346+
} else {
347+
this.layout.add(WS.NO_SPACE, ',', WS.NEWLINE, WS.INDENT);
348+
}
345349
} else {
346350
this.layout.add(WS.NO_SPACE, ',', WS.SPACE);
347351
}

src/sqlFormatter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const defaultOptions: FormatOptions = {
4848
functionCase: 'preserve',
4949
indentStyle: 'standard',
5050
logicalOperatorNewline: 'before',
51+
commaNewline: 'after',
5152
expressionWidth: 50,
5253
linesBetweenQueries: 1,
5354
denseOperators: false,

test/behavesLikeSqlFormatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import supportsIndentStyle from './options/indentStyle.js';
1313
import supportsLinesBetweenQueries from './options/linesBetweenQueries.js';
1414
import supportsNewlineBeforeSemicolon from './options/newlineBeforeSemicolon.js';
1515
import supportsLogicalOperatorNewline from './options/logicalOperatorNewline.js';
16+
import supportsCommaNewline from './options/commaNewline.js';
1617
import supportsParamTypes from './options/paramTypes.js';
1718
import supportsWindowFunctions from './features/windowFunctions.js';
1819
import supportsFunctionCase from './options/functionCase.js';
@@ -36,6 +37,7 @@ export default function behavesLikeSqlFormatter(format: FormatFn) {
3637
supportsExpressionWidth(format);
3738
supportsNewlineBeforeSemicolon(format);
3839
supportsLogicalOperatorNewline(format);
40+
supportsCommaNewline(format);
3941
supportsParamTypes(format);
4042
supportsWindowFunctions(format);
4143

0 commit comments

Comments
 (0)