Skip to content

Commit 4dfecaa

Browse files
Merge pull request #9 from inferrinizzard/feature/operator-spacing
Add flag for dense operator spacing
2 parents c140dae + 073dd6b commit 4dfecaa

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/core/Formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ export default class Formatter {
143143
(token.value === '`' && this.tokenLookBehind(2)?.value === '`')
144144
) {
145145
formattedQuery = this.formatWithSpaces(token, formattedQuery, 'after');
146+
} else if (token.type === tokenTypes.OPERATOR && this.cfg.denseOperators) {
147+
formattedQuery = this.formatWithoutSpaces(token, formattedQuery);
146148
} else {
147149
if (this.cfg.aliasAs !== 'never') {
148150
formattedQuery = this.formatAliases(token, formattedQuery);

src/sqlFormatter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface FormatOptions {
3939
aliasAs: 'always' | 'never' | 'select';
4040
lineWidth: number;
4141
linesBetweenQueries: number;
42+
denseOperators: boolean;
4243
}
4344
/**
4445
* Format whitespace in a query to make it easier to read.
@@ -54,6 +55,7 @@ export interface FormatOptions {
5455
* @param {String} cfg.aliasAs Whether to use AS in column aliases in only SELECT clause, both SELECT and table aliases, or never
5556
* @param {Integer} cfg.lineWidth Number of characters in each line before breaking, default: 50
5657
* @param {Integer} cfg.linesBetweenQueries How many line breaks between queries
58+
* @param {Boolean} cfg.denseOperators whether to format operators with spaces
5759
* @param {ParamItems} cfg.params Collection of params for placeholder replacement
5860
* @return {String}
5961
*/
@@ -92,6 +94,7 @@ export const format = (query: string, cfg: Partial<FormatOptions> = {}): string
9294
newline: { mode: 'always' },
9395
aliasAs: 'select',
9496
lineWidth: 50,
97+
denseOperators: false,
9598
};
9699
cfg = { ...defaultOptions, ...cfg };
97100

test/features/operators.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ export default function supportsOperators(format, operators = []) {
99
expect(format(`foo${op}bar`)).toBe(`foo ${op} bar`);
1010
});
1111
});
12+
13+
operators.forEach(op => {
14+
it(`supports ${op} operator in dense mode`, () => {
15+
expect(format(`foo ${op} bar`, { denseOperators: true })).toBe(`foo${op}bar`);
16+
});
17+
});
1218
}

0 commit comments

Comments
 (0)