Skip to content

Commit 45d4dd2

Browse files
v5.0.1 (#45)
* fix SELECT * with denseOperators * fix aliasAs in test page * fix AliasAs.select mode * update README * update issue templates * update demo page on error * add comments
1 parent 5f21dba commit 45d4dd2

File tree

6 files changed

+51
-31
lines changed

6 files changed

+51
-31
lines changed

.github/ISSUE_TEMPLATE/formatting-bug-report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Please provide a code snippet of the SQL output produced by the tool.
1717

1818
**Usage**
1919

20-
- How are you calling / using the script? (Please provide a code snippet)
20+
- How are you calling / using the script? (Please provide a code snippet, if applicable)
2121
- What SQL language(s) does this apply to?
2222
- What Node version? (If applicable)
2323

.github/ISSUE_TEMPLATE/script-bug-report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ If applicable, add screenshots to help explain your problem.
2525

2626
**Usage**
2727

28-
- How are you calling / using the script? (Please provide a code snippet)
28+
- How are you calling / using the script? (Please provide a code snippet, if applicable)
2929
- What SQL language(s) does this apply to?
3030
- What Node version? (If applicable)
3131

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<img src="static/prettier-sql-clean.svg" width="128"/>
22

3-
# Prettier SQL [![NPM version](https://img.shields.io/npm/v/prettier-sql.svg)](https://npmjs.com/package/prettier-sql) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/inferrinizzard/prettier-sql/coveralls/develop?label=Dev%20Build&logo=Github) ![GitHub Workflow Status (event)](https://img.shields.io/github/workflow/status/inferrinizzard/prettier-sql/webpack/master?event=push&label=Prod%20Build&logo=Github) ![Coveralls](https://img.shields.io/coveralls/github/inferrinizzard/prettier-sql?branch=master&label=Coverage&logo=coveralls&style=plastic)
3+
# Prettier SQL [![NPM version](https://img.shields.io/npm/v/prettier-sql.svg)](https://npmjs.com/package/prettier-sql) ![GitHub Workflow Status (event)](https://img.shields.io/github/workflow/status/inferrinizzard/prettier-sql/coveralls/master?label=Build&logo=Github) ![Coveralls](https://img.shields.io/coveralls/github/inferrinizzard/prettier-sql?branch=master&label=Coverage&logo=coveralls&style=plastic)
44

55
## **Prettier SQL** is a JavaScript library for pretty-printing SQL queries.
66

@@ -24,7 +24,7 @@ It does not support:
2424
- Stored procedures.
2525
- Changing of the delimiter type to something else than `;`.
2626

27-
[Try the demo.](https://inferrinizzard.github.io/prettier-sql/)
27+
[Try the demo.](https://inferrinizzard.github.io/prettier-sql/static)
2828

2929
# Table of contents
3030

src/core/Formatter.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,16 @@ export default class Formatter {
219219
} else if (token.type === TokenType.RESERVED_LOGICAL_OPERATOR) {
220220
formattedQuery = this.formatLogicalOperator(token, formattedQuery);
221221
} else if (token.type === TokenType.RESERVED_KEYWORD) {
222-
if (!(isToken.AS(token) && this.cfg.aliasAs === AliasMode.never)) {
222+
if (
223+
!(
224+
isToken.AS(token) &&
225+
(this.cfg.aliasAs === AliasMode.never || // skip all AS if never
226+
(this.cfg.aliasAs === AliasMode.select &&
227+
this.tokenLookBehind()?.value === ')' && // ) [AS] alias but not SELECT (a) [AS] alpha
228+
!this.withinSelect && // skip WITH foo [AS] ( ...
229+
this.tokenLookAhead()?.value !== '('))
230+
)
231+
) {
223232
// do not format if skipping AS
224233
formattedQuery = this.formatWithSpaces(token, formattedQuery);
225234
this.previousReservedToken = token;
@@ -378,7 +387,8 @@ export default class Formatter {
378387
}
379388

380389
// regular operator
381-
if (this.cfg.denseOperators) {
390+
if (this.cfg.denseOperators && this.tokenLookBehind()?.type !== TokenType.RESERVED_COMMAND) {
391+
// do not trim whitespace if SELECT *
382392
return this.formatWithoutSpaces(token, query);
383393
}
384394
return this.formatWithSpaces(token, query);

static/index.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,39 @@ const attachFormat = () => {
1818
const semicolonNewline = document.getElementById('semicolonNewline');
1919

2020
function format() {
21-
console.time('formatting');
22-
const config = {
23-
language: language.options[language.selectedIndex].value,
24-
uppercase: uppercase.checked,
25-
keywordPosition: keywordPosition.options[keywordPosition.selectedIndex].value,
26-
breakBeforeBooleanOperator: breakBeforeBooleanOperator.checked,
27-
aliasAs: aliasAs.checked,
28-
newline: {
29-
mode: newline.options[newline.selectedIndex].value,
30-
itemCount: itemCount.value,
31-
},
32-
tabulateAlias: tabulateAlias.checked,
33-
commaPosition: commaPosition.options[commaPosition.selectedIndex].value,
34-
parenOptions: {
35-
openParenNewline: openParenNewline.checked,
36-
closeParenNewline: closeParenNewline.checked,
37-
},
38-
lineWidth: lineWidth.value,
39-
lineBetweenQueries: lineBetweenQueries.value,
40-
denseOperators: denseOperators.checked,
41-
semicolonNewline: semicolonNewline.checked,
42-
};
43-
output.value = prettierSql.format(input.value, config);
44-
console.timeEnd('formatting');
21+
try {
22+
console.time('formatting');
23+
const config = {
24+
language: language.options[language.selectedIndex].value,
25+
uppercase: uppercase.checked,
26+
keywordPosition: keywordPosition.options[keywordPosition.selectedIndex].value,
27+
breakBeforeBooleanOperator: breakBeforeBooleanOperator.checked,
28+
aliasAs: aliasAs.options[aliasAs.selectedIndex].value,
29+
newline: {
30+
mode: newline.options[newline.selectedIndex].value,
31+
itemCount: itemCount.value,
32+
},
33+
tabulateAlias: tabulateAlias.checked,
34+
commaPosition: commaPosition.options[commaPosition.selectedIndex].value,
35+
parenOptions: {
36+
openParenNewline: openParenNewline.checked,
37+
closeParenNewline: closeParenNewline.checked,
38+
},
39+
lineWidth: lineWidth.value,
40+
lineBetweenQueries: lineBetweenQueries.value,
41+
denseOperators: denseOperators.checked,
42+
semicolonNewline: semicolonNewline.checked,
43+
};
44+
output.value = prettierSql.format(input.value, config);
45+
console.timeEnd('formatting');
46+
} catch (e) {
47+
output.value = `
48+
An Error Occurred, please report this at:
49+
https://github.com/inferrinizzard/prettier-sql/issues\n
50+
Stack Trace:
51+
${e.stack.toString()}
52+
`;
53+
}
4554
}
4655

4756
input.addEventListener('input', format);

test/behavesLikeSqlFormatter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ export default function behavesLikeSqlFormatter(format) {
300300

301301
it('formats UPDATE query with AS part', () => {
302302
const result = format(
303-
'UPDATE customers SET total_orders = order_summary.total FROM ( SELECT * FROM bank) AS order_summary'
303+
'UPDATE customers SET total_orders = order_summary.total FROM ( SELECT * FROM bank) AS order_summary',
304+
{ aliasAs: 'always' }
304305
);
305306
expect(result).toBe(dedent`
306307
UPDATE

0 commit comments

Comments
 (0)