Skip to content

Commit

Permalink
v5.0.1 (#45)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
inferrinizzard authored Nov 25, 2021
1 parent 5f21dba commit 45d4dd2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/formatting-bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Please provide a code snippet of the SQL output produced by the tool.

**Usage**

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

Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/script-bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ If applicable, add screenshots to help explain your problem.

**Usage**

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

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<img src="static/prettier-sql-clean.svg" width="128"/>

# 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)
# 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)

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

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

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

# Table of contents

Expand Down
14 changes: 12 additions & 2 deletions src/core/Formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,16 @@ export default class Formatter {
} else if (token.type === TokenType.RESERVED_LOGICAL_OPERATOR) {
formattedQuery = this.formatLogicalOperator(token, formattedQuery);
} else if (token.type === TokenType.RESERVED_KEYWORD) {
if (!(isToken.AS(token) && this.cfg.aliasAs === AliasMode.never)) {
if (
!(
isToken.AS(token) &&
(this.cfg.aliasAs === AliasMode.never || // skip all AS if never
(this.cfg.aliasAs === AliasMode.select &&
this.tokenLookBehind()?.value === ')' && // ) [AS] alias but not SELECT (a) [AS] alpha
!this.withinSelect && // skip WITH foo [AS] ( ...
this.tokenLookAhead()?.value !== '('))
)
) {
// do not format if skipping AS
formattedQuery = this.formatWithSpaces(token, formattedQuery);
this.previousReservedToken = token;
Expand Down Expand Up @@ -378,7 +387,8 @@ export default class Formatter {
}

// regular operator
if (this.cfg.denseOperators) {
if (this.cfg.denseOperators && this.tokenLookBehind()?.type !== TokenType.RESERVED_COMMAND) {
// do not trim whitespace if SELECT *
return this.formatWithoutSpaces(token, query);
}
return this.formatWithSpaces(token, query);
Expand Down
57 changes: 33 additions & 24 deletions static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,39 @@ const attachFormat = () => {
const semicolonNewline = document.getElementById('semicolonNewline');

function format() {
console.time('formatting');
const config = {
language: language.options[language.selectedIndex].value,
uppercase: uppercase.checked,
keywordPosition: keywordPosition.options[keywordPosition.selectedIndex].value,
breakBeforeBooleanOperator: breakBeforeBooleanOperator.checked,
aliasAs: aliasAs.checked,
newline: {
mode: newline.options[newline.selectedIndex].value,
itemCount: itemCount.value,
},
tabulateAlias: tabulateAlias.checked,
commaPosition: commaPosition.options[commaPosition.selectedIndex].value,
parenOptions: {
openParenNewline: openParenNewline.checked,
closeParenNewline: closeParenNewline.checked,
},
lineWidth: lineWidth.value,
lineBetweenQueries: lineBetweenQueries.value,
denseOperators: denseOperators.checked,
semicolonNewline: semicolonNewline.checked,
};
output.value = prettierSql.format(input.value, config);
console.timeEnd('formatting');
try {
console.time('formatting');
const config = {
language: language.options[language.selectedIndex].value,
uppercase: uppercase.checked,
keywordPosition: keywordPosition.options[keywordPosition.selectedIndex].value,
breakBeforeBooleanOperator: breakBeforeBooleanOperator.checked,
aliasAs: aliasAs.options[aliasAs.selectedIndex].value,
newline: {
mode: newline.options[newline.selectedIndex].value,
itemCount: itemCount.value,
},
tabulateAlias: tabulateAlias.checked,
commaPosition: commaPosition.options[commaPosition.selectedIndex].value,
parenOptions: {
openParenNewline: openParenNewline.checked,
closeParenNewline: closeParenNewline.checked,
},
lineWidth: lineWidth.value,
lineBetweenQueries: lineBetweenQueries.value,
denseOperators: denseOperators.checked,
semicolonNewline: semicolonNewline.checked,
};
output.value = prettierSql.format(input.value, config);
console.timeEnd('formatting');
} catch (e) {
output.value = `
An Error Occurred, please report this at:
https://github.com/inferrinizzard/prettier-sql/issues\n
Stack Trace:
${e.stack.toString()}
`;
}
}

input.addEventListener('input', format);
Expand Down
3 changes: 2 additions & 1 deletion test/behavesLikeSqlFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ export default function behavesLikeSqlFormatter(format) {

it('formats UPDATE query with AS part', () => {
const result = format(
'UPDATE customers SET total_orders = order_summary.total FROM ( SELECT * FROM bank) AS order_summary'
'UPDATE customers SET total_orders = order_summary.total FROM ( SELECT * FROM bank) AS order_summary',
{ aliasAs: 'always' }
);
expect(result).toBe(dedent`
UPDATE
Expand Down

0 comments on commit 45d4dd2

Please sign in to comment.