prettier-sql
Advanced tools
Comparing version 5.0.1 to 5.1.0
# CHANGELOG | ||
## NEXT [5.0.0] : 2021-11-22 | ||
## [5.1.0] : 2021-12-21 | ||
### Known Issues | ||
- `newline: number` does not work with `[foo]`, `[` and `]` both count as 1 token each so `[foo]` would be 3 items | ||
- `array[0]` or `map[key]` do still work as they are parsed as WORD | ||
- BigQuery formatter fails a few tests (ALTER TABLE, tricky line comments) due to those tests not including valid BigQuery SQL, those tests are currently skipped | ||
### Added | ||
@@ -9,2 +15,49 @@ | ||
- consumed VSCode Extension as subrepo | ||
- added support for Hive language | ||
- added support for BigQuery language | ||
- added keyword dedupe on Formatter classes via Set | ||
VSCode: | ||
- added command `prettier-sql-vscode.format-selection` | ||
- Formats SQL selections | ||
- added settings to override user/workspace `tabSize` and `insertSpaces` settings | ||
- added error message on format fail | ||
- added setting to override formatting language for `sql` files when SQL flavour does not have a VSCode language ID (Microsoft PostgreSQL and MSSQL Extensions) | ||
Other: | ||
- demo page now deployed as git repo subtree, served from root/ (subtree of static/) | ||
### Updated | ||
- fixed handling of `newline` options | ||
- simplified `NewlineMode` config | ||
- fixed ; indentation when used with `semicolonNewline` and `tenSpace` configs | ||
- Formatter now uses numeric for loop to allow for index manipulation | ||
- updated `linesBetweenQueries` to add an extra newline (0 lines = 1 line break, no space in between) | ||
- renamed Formatter class files to `<flavour>.formatter.ts` | ||
- renamed test files to `<flavour>.test.js` | ||
### Removed | ||
- removed `newline: hybrid` config | ||
- `newline: number` now acts like `hybrid` | ||
## [5.0.1] : 2021-11-24 | ||
### Updated | ||
- fixed bug when using SELECT \* and `denseOperators` | ||
- fixed aliasAs option on demo page | ||
- fixed handling of tokens with `aliasAs` flag | ||
- demo page now prints stack trace in textarea on error | ||
## [5.0.0] : 2021-11-22 | ||
### Added | ||
Source: | ||
- added support for ES6 module exports with TypeScript | ||
@@ -11,0 +64,0 @@ - updated webpack with ts-loader for module types |
@@ -26,5 +26,3 @@ import Indentation from './Indentation'; | ||
* @param {Boolean} cfg.uppercase | ||
* @param {NewlineOptions} cfg.newline | ||
* @param {NewlineMode} cfg.newline.mode | ||
* @param {Integer} cfg.newline.itemCount | ||
* @param {NewlineMode} cfg.newline | ||
* @param {Integer} cfg.lineWidth | ||
@@ -64,2 +62,3 @@ * @param {Integer} cfg.linesBetweenQueries | ||
formatBinaryCommand(token: Token, query: string): string; | ||
formatKeyword(token: Token, query: string): string; | ||
formatOperator(token: Token, query: string): string; | ||
@@ -66,0 +65,0 @@ formatDependentClause(token: Token, query: string): string; |
@@ -58,5 +58,3 @@ "use strict"; | ||
* @param {Boolean} cfg.uppercase | ||
* @param {NewlineOptions} cfg.newline | ||
* @param {NewlineMode} cfg.newline.mode | ||
* @param {Integer} cfg.newline.itemCount | ||
* @param {NewlineMode} cfg.newline | ||
* @param {Integer} cfg.lineWidth | ||
@@ -94,7 +92,16 @@ * @param {Integer} cfg.linesBetweenQueries | ||
_defineProperty(this, "checkNewline", function (index) { | ||
if (_this.newline.mode === _types.NewlineMode.always || _this.tokens.some(function (_ref) { | ||
var tail = _this.tokens.slice(index + 1); | ||
var nextTokens = tail.slice(0, tail.length ? tail.findIndex(function (_ref) { | ||
var type = _ref.type, | ||
value = _ref.value; | ||
return type === _token.TokenType.RESERVED_COMMAND || type === _token.TokenType.RESERVED_BINARY_COMMAND || value === ';'; | ||
}) : undefined // add undefined for EOF | ||
); | ||
if (_this.newline === _types.NewlineMode.always || _this.withinSelect && nextTokens.some(function (_ref2) { | ||
var type = _ref2.type, | ||
value = _ref2.value; | ||
return type === _token.TokenType.BLOCK_START && value.length > 1; | ||
}) // auto break on CASE statements | ||
}) // auto break if SELECT includes CASE statements | ||
) { | ||
@@ -104,12 +111,6 @@ return true; | ||
if (_this.newline.mode === _types.NewlineMode.never) { | ||
if (_this.newline === _types.NewlineMode.never) { | ||
return false; | ||
} | ||
var tail = _this.tokens.slice(index + 1); | ||
var nextTokens = tail.slice(0, tail.findIndex(function (_ref2) { | ||
var type = _ref2.type; | ||
return type === _token.TokenType.RESERVED_COMMAND || type === _token.TokenType.RESERVED_BINARY_COMMAND || type === _token.TokenType.RESERVED_LOGICAL_OPERATOR; | ||
})); | ||
var numItems = nextTokens.reduce(function (acc, _ref3) { | ||
@@ -144,9 +145,4 @@ var type = _ref3.type, | ||
} // start with 1 for first word | ||
).count; | ||
).count; // calculate length if it were all inline | ||
if (_this.newline.mode === _types.NewlineMode.itemCount) { | ||
return numItems > _this.newline.itemCount; | ||
} // calculate length if it were all inline | ||
var inlineWidth = "".concat(_this.tokens[index].whitespaceBefore).concat(_this.tokens[index].value, " ").concat(nextTokens.map(function (_ref4) { | ||
@@ -157,6 +153,6 @@ var value = _ref4.value; | ||
if (_this.newline.mode === _types.NewlineMode.lineWidth) { | ||
if (_this.newline === _types.NewlineMode.lineWidth) { | ||
return inlineWidth > _this.lineWidth; | ||
} else if (_this.newline.mode === _types.NewlineMode.hybrid) { | ||
return numItems > _this.newline.itemCount || inlineWidth > _this.lineWidth; | ||
} else if (!Number.isNaN(_this.newline)) { | ||
return numItems > _this.newline || inlineWidth > _this.lineWidth; | ||
} | ||
@@ -178,3 +174,3 @@ | ||
this.tokens = []; | ||
this.index = 0; | ||
this.index = -1; | ||
} | ||
@@ -354,18 +350,16 @@ /** | ||
value: function getFormattedQueryFromTokens() { | ||
var _this3 = this; | ||
var formattedQuery = ''; | ||
this.tokens.forEach(function (token, index) { | ||
_this3.index = index; | ||
token = _this3.tokenOverride(token); | ||
for (this.index = 0; this.index < this.tokens.length; this.index++) { | ||
var token = this.tokenOverride(this.tokens[this.index]); | ||
if ((0, _token.isReserved)(token)) { | ||
_this3.previousReservedToken = token; | ||
this.previousReservedToken = token; | ||
if (token.type !== _token.TokenType.RESERVED_KEYWORD) { | ||
token = _this3.tenSpacedToken(token); | ||
token = this.tenSpacedToken(token); | ||
} | ||
if (token.type === _token.TokenType.RESERVED_COMMAND) { | ||
_this3.withinSelect = _token.isToken.SELECT(token); | ||
this.withinSelect = _token.isToken.SELECT(token); | ||
} | ||
@@ -375,41 +369,34 @@ } | ||
if (token.type === _token.TokenType.LINE_COMMENT) { | ||
formattedQuery = _this3.formatLineComment(token, formattedQuery); | ||
formattedQuery = this.formatLineComment(token, formattedQuery); | ||
} else if (token.type === _token.TokenType.BLOCK_COMMENT) { | ||
formattedQuery = _this3.formatBlockComment(token, formattedQuery); | ||
formattedQuery = this.formatBlockComment(token, formattedQuery); | ||
} else if (token.type === _token.TokenType.RESERVED_COMMAND) { | ||
_this3.currentNewline = _this3.checkNewline(index); | ||
formattedQuery = _this3.formatCommand(token, formattedQuery); | ||
this.currentNewline = this.checkNewline(this.index); | ||
formattedQuery = this.formatCommand(token, formattedQuery); | ||
} else if (token.type === _token.TokenType.RESERVED_BINARY_COMMAND) { | ||
formattedQuery = _this3.formatBinaryCommand(token, formattedQuery); | ||
formattedQuery = this.formatBinaryCommand(token, formattedQuery); | ||
} else if (token.type === _token.TokenType.RESERVED_DEPENDENT_CLAUSE) { | ||
formattedQuery = _this3.formatDependentClause(token, formattedQuery); | ||
formattedQuery = this.formatDependentClause(token, formattedQuery); | ||
} else if (token.type === _token.TokenType.RESERVED_LOGICAL_OPERATOR) { | ||
formattedQuery = _this3.formatLogicalOperator(token, formattedQuery); | ||
formattedQuery = this.formatLogicalOperator(token, formattedQuery); | ||
} else if (token.type === _token.TokenType.RESERVED_KEYWORD) { | ||
var _this3$tokenLookBehin, _this3$tokenLookAhead; | ||
if (!(_token.isToken.AS(token) && (_this3.cfg.aliasAs === _types.AliasMode.never || // skip all AS if never | ||
_this3.cfg.aliasAs === _types.AliasMode.select && ((_this3$tokenLookBehin = _this3.tokenLookBehind()) === null || _this3$tokenLookBehin === void 0 ? void 0 : _this3$tokenLookBehin.value) === ')' && // ) [AS] alias but not SELECT (a) [AS] alpha | ||
!_this3.withinSelect && // skip WITH foo [AS] ( ... | ||
((_this3$tokenLookAhead = _this3.tokenLookAhead()) === null || _this3$tokenLookAhead === void 0 ? void 0 : _this3$tokenLookAhead.value) !== '('))) { | ||
// do not format if skipping AS | ||
formattedQuery = _this3.formatWithSpaces(token, formattedQuery); | ||
_this3.previousReservedToken = token; | ||
} | ||
formattedQuery = this.formatKeyword(token, formattedQuery); | ||
this.previousReservedToken = token; | ||
} else if (token.type === _token.TokenType.BLOCK_START) { | ||
formattedQuery = _this3.formatBlockStart(token, formattedQuery); | ||
formattedQuery = this.formatBlockStart(token, formattedQuery); | ||
} else if (token.type === _token.TokenType.BLOCK_END) { | ||
formattedQuery = _this3.formatBlockEnd(token, formattedQuery); | ||
formattedQuery = this.formatBlockEnd(token, formattedQuery); | ||
} else if (token.type === _token.TokenType.PLACEHOLDER) { | ||
formattedQuery = _this3.formatPlaceholder(token, formattedQuery); | ||
formattedQuery = this.formatPlaceholder(token, formattedQuery); | ||
} else if (token.type === _token.TokenType.OPERATOR) { | ||
formattedQuery = _this3.formatOperator(token, formattedQuery); | ||
formattedQuery = this.formatOperator(token, formattedQuery); | ||
} else { | ||
if (_this3.cfg.aliasAs !== _types.AliasMode.never) { | ||
formattedQuery = _this3.formatAliases(token, formattedQuery); | ||
if (this.cfg.aliasAs !== _types.AliasMode.never) { | ||
formattedQuery = this.formatAliases(token, formattedQuery); | ||
} | ||
formattedQuery = _this3.formatWithSpaces(token, formattedQuery); | ||
formattedQuery = this.formatWithSpaces(token, formattedQuery); | ||
} | ||
}); | ||
} | ||
return formattedQuery.replace(new RegExp(_token.ZWS, 'ugim'), ' '); | ||
@@ -429,4 +416,3 @@ } | ||
var missingSelectColumnAlias = // if select column alias is missing and alias is not never | ||
this.withinSelect && token.type === _token.TokenType.WORD && (_token.isToken.END(prevToken) || // isAs(prevToken) || | ||
(prevToken === null || prevToken === void 0 ? void 0 : prevToken.type) === _token.TokenType.WORD && ((nextToken === null || nextToken === void 0 ? void 0 : nextToken.value) === ',' || (0, _token.isCommand)(nextToken))); | ||
this.withinSelect && token.type === _token.TokenType.WORD && (_token.isToken.END(prevToken) || (prevToken === null || prevToken === void 0 ? void 0 : prevToken.type) === _token.TokenType.WORD && ((nextToken === null || nextToken === void 0 ? void 0 : nextToken.value) === ',' || (0, _token.isCommand)(nextToken))); | ||
@@ -496,5 +482,19 @@ if (missingTableAlias || missingSelectColumnAlias) { | ||
}, { | ||
key: "formatKeyword", | ||
value: function formatKeyword(token, query) { | ||
var _this$tokenLookBehind, _this$tokenLookAhead3; | ||
if (_token.isToken.AS(token) && (this.cfg.aliasAs === _types.AliasMode.never || this.cfg.aliasAs === _types.AliasMode.select && ((_this$tokenLookBehind = this.tokenLookBehind()) === null || _this$tokenLookBehind === void 0 ? void 0 : _this$tokenLookBehind.value) === ')' && // ) [AS] alias but not SELECT (a) [AS] alpha | ||
!this.withinSelect && // skip WITH foo [AS] ( ... | ||
((_this$tokenLookAhead3 = this.tokenLookAhead()) === null || _this$tokenLookAhead3 === void 0 ? void 0 : _this$tokenLookAhead3.value) !== '(')) { | ||
// do not format if skipping AS | ||
return query; | ||
} | ||
return this.formatWithSpaces(token, query); | ||
} | ||
}, { | ||
key: "formatOperator", | ||
value: function formatOperator(token, query) { | ||
var _this$tokenLookBehind; | ||
var _this$tokenLookBehind2; | ||
@@ -515,3 +515,3 @@ // special operator | ||
if (this.cfg.denseOperators && ((_this$tokenLookBehind = this.tokenLookBehind()) === null || _this$tokenLookBehind === void 0 ? void 0 : _this$tokenLookBehind.type) !== _token.TokenType.RESERVED_COMMAND) { | ||
if (this.cfg.denseOperators && ((_this$tokenLookBehind2 = this.tokenLookBehind()) === null || _this$tokenLookBehind2 === void 0 ? void 0 : _this$tokenLookBehind2.type) !== _token.TokenType.RESERVED_COMMAND) { | ||
// do not trim whitespace if SELECT * | ||
@@ -540,5 +540,6 @@ return this.formatWithoutSpaces(token, query); | ||
if (this.cfg.breakBeforeBooleanOperator) { | ||
return this.addNewline(query) + this.equalizeWhitespace(this.show(token)) + ' '; | ||
return (this.currentNewline ? this.addNewline(query) : query) + this.equalizeWhitespace(this.show(token)) + ' '; | ||
} else { | ||
return this.addNewline(query + this.show(token)); | ||
query += this.show(token); | ||
return this.currentNewline ? this.addNewline(query) : query; | ||
} | ||
@@ -559,3 +560,3 @@ } // Replace any sequence of whitespace characters with single space | ||
} else { | ||
var _token$whitespaceBefo, _this$tokenLookBehind2; | ||
var _token$whitespaceBefo, _this$tokenLookBehind3; | ||
@@ -566,3 +567,3 @@ // Take out the preceding space unless there was whitespace there in the original query | ||
if (((_token$whitespaceBefo = token.whitespaceBefore) === null || _token$whitespaceBefo === void 0 ? void 0 : _token$whitespaceBefo.length) === 0 && !preserveWhitespaceFor.includes((_this$tokenLookBehind2 = this.tokenLookBehind()) === null || _this$tokenLookBehind2 === void 0 ? void 0 : _this$tokenLookBehind2.type)) { | ||
if (((_token$whitespaceBefo = token.whitespaceBefore) === null || _token$whitespaceBefo === void 0 ? void 0 : _token$whitespaceBefo.length) === 0 && !preserveWhitespaceFor.includes((_this$tokenLookBehind3 = this.tokenLookBehind()) === null || _this$tokenLookBehind3 === void 0 ? void 0 : _this$tokenLookBehind3.type)) { | ||
query = (0, _utils.trimSpacesEnd)(query); | ||
@@ -580,3 +581,3 @@ } else if (!this.cfg.parenOptions.openParenNewline) { | ||
if (!_token.isToken.CASE(token) || this.newline.mode === _types.NewlineMode.always) { | ||
if (!_token.isToken.CASE(token) || this.newline === _types.NewlineMode.always) { | ||
query = this.addNewline(query); | ||
@@ -651,5 +652,9 @@ } | ||
query += '\n'; | ||
if (this.cfg.tenSpace) { | ||
query += this.cfg.indent; | ||
} | ||
} | ||
return query + this.show(token) + '\n'.repeat(this.cfg.linesBetweenQueries || 1); | ||
return query + this.show(token) + '\n'.repeat(this.cfg.linesBetweenQueries + 1); | ||
} // Converts token to string (uppercasing it if needed) | ||
@@ -656,0 +661,0 @@ |
@@ -6,10 +6,10 @@ "use strict"; | ||
}); | ||
exports.createOperatorRegex = createOperatorRegex; | ||
exports.createLineCommentRegex = createLineCommentRegex; | ||
exports.createOperatorRegex = createOperatorRegex; | ||
exports.createParenRegex = createParenRegex; | ||
exports.createPlaceholderRegex = createPlaceholderRegex; | ||
exports.createReservedWordRegex = createReservedWordRegex; | ||
exports.createWordRegex = createWordRegex; | ||
exports.createStringPattern = createStringPattern; | ||
exports.createStringRegex = createStringRegex; | ||
exports.createWordRegex = createWordRegex; | ||
exports.createParenRegex = createParenRegex; | ||
exports.createPlaceholderRegex = createPlaceholderRegex; | ||
@@ -16,0 +16,0 @@ var _utils = require("../utils"); |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.testToken = exports.isToken = exports.isReserved = exports.isCommand = exports.ZWS = exports.TokenType = void 0; | ||
exports.isReserved = exports.isCommand = exports.isToken = exports.testToken = exports.ZWS = exports.TokenType = void 0; | ||
var TokenType; | ||
@@ -9,0 +9,0 @@ exports.TokenType = TokenType; |
import type { ParamItems } from './core/Params'; | ||
import Db2Formatter from './languages/Db2Formatter'; | ||
import MariaDbFormatter from './languages/MariaDbFormatter'; | ||
import MySqlFormatter from './languages/MySqlFormatter'; | ||
import N1qlFormatter from './languages/N1qlFormatter'; | ||
import PlSqlFormatter from './languages/PlSqlFormatter'; | ||
import PostgreSqlFormatter from './languages/PostgreSqlFormatter'; | ||
import RedshiftFormatter from './languages/RedshiftFormatter'; | ||
import SparkSqlFormatter from './languages/SparkSqlFormatter'; | ||
import StandardSqlFormatter from './languages/StandardSqlFormatter'; | ||
import TSqlFormatter from './languages/TSqlFormatter'; | ||
import type { NewlineOptions } from './types'; | ||
import { AliasMode, CommaPosition, KeywordMode, ParenOptions } from './types'; | ||
import BigQueryFormatter from './languages/bigquery.formatter'; | ||
import Db2Formatter from './languages/db2.formatter'; | ||
import HiveFormatter from './languages/hive.formatter'; | ||
import MariaDbFormatter from './languages/mariadb.formatter'; | ||
import MySqlFormatter from './languages/mysql.formatter'; | ||
import N1qlFormatter from './languages/n1ql.formatter'; | ||
import PlSqlFormatter from './languages/plsql.formatter'; | ||
import PostgreSqlFormatter from './languages/postgresql.formatter'; | ||
import RedshiftFormatter from './languages/redshift.formatter'; | ||
import SparkSqlFormatter from './languages/sparksql.formatter'; | ||
import StandardSqlFormatter from './languages/standardsql.formatter'; | ||
import TSqlFormatter from './languages/tsql.formatter'; | ||
import { AliasMode, CommaPosition, KeywordMode, NewlineMode, ParenOptions } from './types'; | ||
export declare const formatters: { | ||
bigquery: typeof BigQueryFormatter; | ||
db2: typeof Db2Formatter; | ||
hive: typeof HiveFormatter; | ||
mariadb: typeof MariaDbFormatter; | ||
@@ -33,3 +36,3 @@ mysql: typeof MySqlFormatter; | ||
keywordPosition: KeywordMode | keyof typeof KeywordMode; | ||
newline: NewlineOptions; | ||
newline: NewlineMode | keyof typeof NewlineMode | number; | ||
breakBeforeBooleanOperator: boolean; | ||
@@ -55,5 +58,3 @@ aliasAs: AliasMode | keyof typeof AliasMode; | ||
* @param {KeywordMode} cfg.keywordPosition Sets main keyword position style, see keywordPosition.md for examples | ||
* @param {NewlineOptions} cfg.newline Determines when to break words onto a newline; | ||
* @param {NewlineMode} cfg.newline.mode always | never | lineWidth (break only when > line width) | itemCount (break when > itemCount) | hybrid (lineWidth OR itemCount) | ||
* @param {Integer} cfg.newline.itemCount Used when mode is itemCount or hybrid, must be >=0 | ||
* @param {NewlineMode} cfg.newline Determines when to break words onto a newline; always | never | lineWidth (break only when > line width) | number (break when > n) | ||
* @param {Boolean} cfg.breakBeforeBooleanOperator Break before boolean operator (AND, OR, XOR) ? | ||
@@ -60,0 +61,0 @@ * @param {AliasMode} cfg.aliasAs Whether to use AS in column aliases in only SELECT clause, both SELECT and table aliases, or never |
@@ -6,24 +6,28 @@ "use strict"; | ||
}); | ||
exports.supportedDialects = exports.formatters = exports.format = void 0; | ||
exports.format = exports.supportedDialects = exports.formatters = void 0; | ||
var _Db2Formatter = _interopRequireDefault(require("./languages/Db2Formatter")); | ||
var _bigquery = _interopRequireDefault(require("./languages/bigquery.formatter")); | ||
var _MariaDbFormatter = _interopRequireDefault(require("./languages/MariaDbFormatter")); | ||
var _db = _interopRequireDefault(require("./languages/db2.formatter")); | ||
var _MySqlFormatter = _interopRequireDefault(require("./languages/MySqlFormatter")); | ||
var _hive = _interopRequireDefault(require("./languages/hive.formatter")); | ||
var _N1qlFormatter = _interopRequireDefault(require("./languages/N1qlFormatter")); | ||
var _mariadb = _interopRequireDefault(require("./languages/mariadb.formatter")); | ||
var _PlSqlFormatter = _interopRequireDefault(require("./languages/PlSqlFormatter")); | ||
var _mysql = _interopRequireDefault(require("./languages/mysql.formatter")); | ||
var _PostgreSqlFormatter = _interopRequireDefault(require("./languages/PostgreSqlFormatter")); | ||
var _n1ql = _interopRequireDefault(require("./languages/n1ql.formatter")); | ||
var _RedshiftFormatter = _interopRequireDefault(require("./languages/RedshiftFormatter")); | ||
var _plsql = _interopRequireDefault(require("./languages/plsql.formatter")); | ||
var _SparkSqlFormatter = _interopRequireDefault(require("./languages/SparkSqlFormatter")); | ||
var _postgresql = _interopRequireDefault(require("./languages/postgresql.formatter")); | ||
var _StandardSqlFormatter = _interopRequireDefault(require("./languages/StandardSqlFormatter")); | ||
var _redshift = _interopRequireDefault(require("./languages/redshift.formatter")); | ||
var _TSqlFormatter = _interopRequireDefault(require("./languages/TSqlFormatter")); | ||
var _sparksql = _interopRequireDefault(require("./languages/sparksql.formatter")); | ||
var _standardsql = _interopRequireDefault(require("./languages/standardsql.formatter")); | ||
var _tsql = _interopRequireDefault(require("./languages/tsql.formatter")); | ||
var _types = require("./types"); | ||
@@ -42,12 +46,14 @@ | ||
var formatters = { | ||
db2: _Db2Formatter["default"], | ||
mariadb: _MariaDbFormatter["default"], | ||
mysql: _MySqlFormatter["default"], | ||
n1ql: _N1qlFormatter["default"], | ||
plsql: _PlSqlFormatter["default"], | ||
postgresql: _PostgreSqlFormatter["default"], | ||
redshift: _RedshiftFormatter["default"], | ||
spark: _SparkSqlFormatter["default"], | ||
sql: _StandardSqlFormatter["default"], | ||
tsql: _TSqlFormatter["default"] | ||
bigquery: _bigquery["default"], | ||
db2: _db["default"], | ||
hive: _hive["default"], | ||
mariadb: _mariadb["default"], | ||
mysql: _mysql["default"], | ||
n1ql: _n1ql["default"], | ||
plsql: _plsql["default"], | ||
postgresql: _postgresql["default"], | ||
redshift: _redshift["default"], | ||
spark: _sparksql["default"], | ||
sql: _standardsql["default"], | ||
tsql: _tsql["default"] | ||
}; | ||
@@ -67,5 +73,3 @@ exports.formatters = formatters; | ||
* @param {KeywordMode} cfg.keywordPosition Sets main keyword position style, see keywordPosition.md for examples | ||
* @param {NewlineOptions} cfg.newline Determines when to break words onto a newline; | ||
* @param {NewlineMode} cfg.newline.mode always | never | lineWidth (break only when > line width) | itemCount (break when > itemCount) | hybrid (lineWidth OR itemCount) | ||
* @param {Integer} cfg.newline.itemCount Used when mode is itemCount or hybrid, must be >=0 | ||
* @param {NewlineMode} cfg.newline Determines when to break words onto a newline; always | never | lineWidth (break only when > line width) | number (break when > n) | ||
* @param {Boolean} cfg.breakBeforeBooleanOperator Break before boolean operator (AND, OR, XOR) ? | ||
@@ -102,17 +106,11 @@ * @param {AliasMode} cfg.aliasAs Whether to use AS in column aliases in only SELECT clause, both SELECT and table aliases, or never | ||
if (cfg.newline && (cfg.newline.mode === _types.NewlineMode.itemCount || cfg.newline.mode === _types.NewlineMode.hybrid)) { | ||
var _cfg$newline$itemCoun; | ||
if (cfg.newline && !Number.isNaN(+cfg.newline)) { | ||
var _cfg$newline; | ||
if (((_cfg$newline$itemCoun = cfg.newline.itemCount) !== null && _cfg$newline$itemCoun !== void 0 ? _cfg$newline$itemCoun : 0) < 0) { | ||
throw new Error('Error: newline.itemCount must be a positive number.'); | ||
if (((_cfg$newline = cfg.newline) !== null && _cfg$newline !== void 0 ? _cfg$newline : 0) < 0) { | ||
throw new Error('Error: newline must be a positive number.'); | ||
} | ||
if (cfg.newline.itemCount === 0) { | ||
if (cfg.newline.mode === _types.NewlineMode.hybrid) { | ||
cfg.newline.mode = _types.NewlineMode.lineWidth; | ||
} else if (cfg.newline.mode === _types.NewlineMode.itemCount) { | ||
cfg.newline = { | ||
mode: _types.NewlineMode.always | ||
}; | ||
} | ||
if (cfg.newline === 0) { | ||
cfg.newline = _types.NewlineMode.always; | ||
} | ||
@@ -131,5 +129,3 @@ } | ||
keywordPosition: _types.KeywordMode.standard, | ||
newline: { | ||
mode: _types.NewlineMode.always | ||
}, | ||
newline: _types.NewlineMode.always, | ||
breakBeforeBooleanOperator: true, | ||
@@ -136,0 +132,0 @@ aliasAs: _types.AliasMode.select, |
@@ -9,10 +9,4 @@ export declare enum KeywordMode { | ||
never = "never", | ||
lineWidth = "lineWidth", | ||
itemCount = "itemCount", | ||
hybrid = "hybrid" | ||
lineWidth = "lineWidth" | ||
} | ||
export interface NewlineOptions { | ||
mode: NewlineMode | keyof typeof NewlineMode; | ||
itemCount?: number; | ||
} | ||
export declare enum AliasMode { | ||
@@ -19,0 +13,0 @@ always = "always", |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.NewlineMode = exports.KeywordMode = exports.CommaPosition = exports.AliasMode = void 0; | ||
exports.CommaPosition = exports.AliasMode = exports.NewlineMode = exports.KeywordMode = void 0; | ||
var KeywordMode; | ||
@@ -24,4 +24,2 @@ exports.KeywordMode = KeywordMode; | ||
NewlineMode["lineWidth"] = "lineWidth"; | ||
NewlineMode["itemCount"] = "itemCount"; | ||
NewlineMode["hybrid"] = "hybrid"; | ||
})(NewlineMode || (exports.NewlineMode = NewlineMode = {})); | ||
@@ -28,0 +26,0 @@ |
@@ -0,1 +1,2 @@ | ||
export declare const dedupe: (arr: string[]) => string[]; | ||
export declare const trimSpacesEnd: (str: string) => string; | ||
@@ -2,0 +3,0 @@ export declare const last: <T extends unknown>(arr: T[]) => T; |
@@ -6,5 +6,23 @@ "use strict"; | ||
}); | ||
exports.trimSpacesEnd = exports.tabulateLines = exports.sortByLengthDesc = exports.maxLength = exports.last = exports.isEmpty = exports.escapeRegExp = void 0; | ||
exports.tabulateLines = exports.maxLength = exports.sortByLengthDesc = exports.escapeRegExp = exports.isEmpty = exports.last = exports.trimSpacesEnd = exports.dedupe = void 0; | ||
// Only removes spaces, not newlines | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
var dedupe = function dedupe(arr) { | ||
return _toConsumableArray(new Set(arr)); | ||
}; // Only removes spaces, not newlines | ||
exports.dedupe = dedupe; | ||
var trimSpacesEnd = function trimSpacesEnd(str) { | ||
@@ -11,0 +29,0 @@ return str.replace(/[\t ]+$/, ''); |
{ | ||
"name": "prettier-sql", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"description": "Format whitespace in a SQL query to make it more readable", | ||
@@ -98,5 +98,4 @@ "license": "MIT", | ||
], | ||
"testRegex": ".*Test", | ||
"collectCoverage": true | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
<img src="static/prettier-sql-clean.svg" width="128"/> | ||
<a href='https://github.com/inferrinizzard/prettier-sql'><img src="static/prettier-sql-clean.svg" width="128"/></a> | ||
@@ -12,8 +12,10 @@ # 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) | ||
- **sql** - [Standard SQL][] | ||
- **bigquery** - [GCP BigQuery][] | ||
- **db2** - [IBM DB2][] | ||
- **hive** - [Apache Hive][] | ||
- **mariadb** - [MariaDB][] | ||
- **mysql** - [MySQL][] | ||
- **n1ql** - [Couchbase N1QL][] | ||
- **plsql** - [Oracle PL/SQL][] | ||
- **postgresql** - [PostgreSQL][] | ||
- **db2** - [IBM DB2][] | ||
- **plsql** - [Oracle PL/SQL][] | ||
- **n1ql** - [Couchbase N1QL][] | ||
- **redshift** - [Amazon Redshift][] | ||
@@ -28,3 +30,3 @@ - **spark** - [Spark][] | ||
→ [Try the demo.](https://inferrinizzard.github.io/prettier-sql/static) | ||
→ [Try the demo.](https://inferrinizzard.github.io/prettier-sql) | ||
@@ -121,3 +123,3 @@ # Table of contents | ||
usage: sqlfmt.js [-h] [-o OUTPUT] \ | ||
[-l {db2,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,tsql}] [-c CONFIG] [--version] [FILE] | ||
[-l {bigquery,db2,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,tsql}] [-c CONFIG] [--version] [FILE] | ||
@@ -133,3 +135,3 @@ Prettier SQL | ||
File to write SQL output (defaults to stdout) | ||
-l, --language {db2,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,tsql} | ||
-l, --language {bigquery,db2,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,tsql} | ||
SQL dialect (defaults to standard sql) | ||
@@ -165,6 +167,3 @@ -c, --config CONFIG | ||
"keywordPosition": "standard" | "tenSpaceLeft" | "tenSpaceRight", | ||
"newline": { | ||
"mode": "always" | "itemCount" | "lineWidth" | "hybrid" | "never", | ||
"itemCount"?: number // only used if newline.mode is itemCount or hybrid | ||
}, | ||
"newline": "always" | "lineWidth" | "never" | number, | ||
"breakBeforeBooleanOperator": boolean, | ||
@@ -205,10 +204,12 @@ "aliasAs": "always" | "select" | "never", | ||
[standard sql]: https://en.wikipedia.org/wiki/SQL:2011 | ||
[gcp bigquery]: https://cloud.google.com/bigquery | ||
[ibm db2]: https://www.ibm.com/analytics/us/en/technology/db2/ | ||
[apache hive]: https://hive.apache.org/ | ||
[mariadb]: https://mariadb.com/ | ||
[mysql]: https://www.mysql.com/ | ||
[couchbase n1ql]: http://www.couchbase.com/n1ql | ||
[ibm db2]: https://www.ibm.com/analytics/us/en/technology/db2/ | ||
[oracle pl/sql]: http://www.oracle.com/technetwork/database/features/plsql/index.html | ||
[postgresql]: https://www.postgresql.org/ | ||
[amazon redshift]: https://docs.aws.amazon.com/redshift/latest/dg/cm_chap_SQLCommandRef.html | ||
[spark]: https://spark.apache.org/docs/latest/api/sql/index.html | ||
[postgresql]: https://www.postgresql.org/ | ||
[mariadb]: https://mariadb.com/ | ||
[mysql]: https://www.mysql.com/ | ||
[tsql]: https://docs.microsoft.com/en-us/sql/sql-server/ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2287119
77
8826
210