New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sql-formatter

Package Overview
Dependencies
Maintainers
2
Versions
150
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sql-formatter - npm Package Compare versions

Comparing version 7.0.1 to 7.0.2

94

lib/core/AliasAs.js

@@ -8,4 +8,8 @@ "use strict";

var _AsTokenFactory = _interopRequireDefault(require("./AsTokenFactory"));
var _token = require("./token");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

@@ -17,14 +21,68 @@

/** Decides addition and removal of AS tokens */
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/** Adds and removes AS tokens as configured by aliasAs option */
var AliasAs = /*#__PURE__*/function () {
function AliasAs(aliasAs, formatter) {
function AliasAs(cfg, tokens) {
_classCallCheck(this, AliasAs);
this.aliasAs = aliasAs;
this.formatter = formatter;
_defineProperty(this, "index", 0);
_defineProperty(this, "tokens", []);
_defineProperty(this, "previousReservedToken", _token.EOF_TOKEN);
_defineProperty(this, "previousCommandToken", _token.EOF_TOKEN);
_defineProperty(this, "asTokenFactory", void 0);
_defineProperty(this, "aliasAs", void 0);
this.aliasAs = cfg.aliasAs;
this.asTokenFactory = new _AsTokenFactory["default"](cfg.keywordCase, tokens);
this.tokens = tokens;
}
/** True when AS keyword should be added *before* current token */
/** Returns tokens with AS tokens added/removed as needed */
_createClass(AliasAs, [{
key: "process",
value: function process() {
var processedTokens = [];
for (this.index = 0; this.index < this.tokens.length; this.index++) {
var token = this.tokens[this.index];
if ((0, _token.isReserved)(token)) {
this.previousReservedToken = token;
if (token.type === _token.TokenType.RESERVED_COMMAND) {
this.previousCommandToken = token;
}
}
if (_token.isToken.AS(token)) {
if (!this.shouldRemove()) {
processedTokens.push(token);
}
} else if (token.type === _token.TokenType.IDENT || token.type === _token.TokenType.NUMBER || token.type === _token.TokenType.STRING || token.type === _token.TokenType.VARIABLE) {
if (this.shouldAddBefore(token)) {
processedTokens.push(this.asTokenFactory.token());
}
processedTokens.push(token);
if (this.shouldAddAfter()) {
processedTokens.push(this.asTokenFactory.token());
}
} else {
processedTokens.push(token);
}
}
return processedTokens;
}
/** True when AS keyword should be added *before* current token */
}, {
key: "shouldAddBefore",

@@ -46,3 +104,3 @@ value: function shouldAddBefore(token) {

var nextToken = this.lookAhead();
return (this.aliasAs === 'always' || this.aliasAs === 'select') && this.formatter.isWithinSelect() && token.type === _token.TokenType.IDENT && (_token.isToken.END(prevToken) || (prevToken.type === _token.TokenType.IDENT || prevToken.type === _token.TokenType.NUMBER) && (nextToken.value === ',' || (0, _token.isCommand)(nextToken)));
return (this.aliasAs === 'always' || this.aliasAs === 'select') && this.isWithinSelect() && token.type === _token.TokenType.IDENT && (_token.isToken.END(prevToken) || (prevToken.type === _token.TokenType.IDENT || prevToken.type === _token.TokenType.NUMBER) && (nextToken.value === ',' || (0, _token.isCommand)(nextToken)));
}

@@ -60,3 +118,3 @@ /** True when AS keyword should be added *after* current token */

value: function isMissingTypeCastAs() {
return this.aliasAs === 'never' && this.formatter.isWithinSelect() && _token.isToken.CAST(this.formatter.getPreviousReservedToken()) && _token.isToken.AS(this.lookAhead()) && (this.lookAhead(2).type === _token.TokenType.IDENT || this.lookAhead(2).type === _token.TokenType.RESERVED_KEYWORD) && this.lookAhead(3).value === ')';
return this.aliasAs === 'never' && this.isWithinSelect() && _token.isToken.CAST(this.getPreviousReservedToken()) && _token.isToken.AS(this.lookAhead()) && (this.lookAhead(2).type === _token.TokenType.IDENT || this.lookAhead(2).type === _token.TokenType.RESERVED_KEYWORD) && this.lookAhead(3).value === ')';
} // checks for WITH `table` [AS] (

@@ -89,14 +147,26 @@

return this.lookBehind().value === ')' && // ) [AS] alias but not SELECT (a) [AS] alpha
!this.formatter.isWithinSelect() && this.lookAhead().value !== '(' // skip WITH foo [AS] ( ...
!this.isWithinSelect() && this.lookAhead().value !== '(' // skip WITH foo [AS] ( ...
;
}
}, {
key: "getPreviousReservedToken",
value: function getPreviousReservedToken() {
return this.previousReservedToken;
}
}, {
key: "isWithinSelect",
value: function isWithinSelect() {
return _token.isToken.SELECT(this.previousCommandToken);
}
}, {
key: "lookBehind",
value: function lookBehind(n) {
return this.formatter.tokenLookBehind(n);
value: function lookBehind() {
var n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return this.lookAhead(-n);
}
}, {
key: "lookAhead",
value: function lookAhead(n) {
return this.formatter.tokenLookAhead(n);
value: function lookAhead() {
var n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return this.tokens[this.index + n] || _token.EOF_TOKEN;
}

@@ -103,0 +173,0 @@ }]);

14

lib/core/Formatter.js

@@ -14,4 +14,2 @@ "use strict";

var _AsTokenFactory = _interopRequireDefault(require("./AsTokenFactory"));
var _Parser = _interopRequireDefault(require("./Parser"));

@@ -23,2 +21,4 @@

var _AliasAs = _interopRequireDefault(require("./AliasAs"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

@@ -80,4 +80,5 @@

var tokens = this.cachedTokenizer().tokenize(query);
var ast = new _Parser["default"](tokens).parse();
var formattedQuery = this.formatAst(ast, tokens);
var processedTokens = new _AliasAs["default"](this.cfg, tokens).process();
var ast = new _Parser["default"](processedTokens).parse();
var formattedQuery = this.formatAst(ast);
var finalQuery = this.postFormat(formattedQuery);

@@ -88,8 +89,7 @@ return finalQuery.trimEnd();

key: "formatAst",
value: function formatAst(statements, tokens) {
value: function formatAst(statements) {
var _this = this;
var asTokenFactory = new _AsTokenFactory["default"](this.cfg.keywordCase, tokens);
return statements.map(function (stat) {
return new _StatementFormatter["default"](_this.cfg, _this.params, asTokenFactory).format(stat);
return new _StatementFormatter["default"](_this.cfg, _this.params).format(stat);
}).join('\n'.repeat(this.cfg.linesBetweenQueries + 1));

@@ -96,0 +96,0 @@ }

@@ -98,5 +98,5 @@ "use strict";

if (token.type === _token.TokenType.BLOCK_START) {
if (token.type === _token.TokenType.OPEN_PAREN) {
level++;
} else if (token.type === _token.TokenType.BLOCK_END) {
} else if (token.type === _token.TokenType.CLOSE_PAREN) {
level--;

@@ -103,0 +103,0 @@

@@ -20,4 +20,2 @@ "use strict";

var _AliasAs = _interopRequireDefault(require("./AliasAs"));
var _config = require("./config");

@@ -49,3 +47,3 @@

var StatementFormatter = /*#__PURE__*/function () {
function StatementFormatter(cfg, params, asTokenFactory) {
function StatementFormatter(cfg, params) {
_classCallCheck(this, StatementFormatter);

@@ -59,8 +57,4 @@

_defineProperty(this, "aliasAs", void 0);
_defineProperty(this, "params", void 0);
_defineProperty(this, "asTokenFactory", void 0);
_defineProperty(this, "query", void 0);

@@ -81,5 +75,3 @@

this.inlineBlock = new _InlineBlock["default"](this.cfg.expressionWidth);
this.aliasAs = new _AliasAs["default"](this.cfg.aliasAs, this);
this.params = params;
this.asTokenFactory = asTokenFactory;
this.query = new _WhitespaceBuilder["default"](this.indentation);

@@ -138,7 +130,7 @@ }

case _token.TokenType.BLOCK_START:
return this.formatBlockStart(token);
case _token.TokenType.OPEN_PAREN:
return this.formatOpenParen(token);
case _token.TokenType.BLOCK_END:
return this.formatBlockEnd(token);
case _token.TokenType.CLOSE_PAREN:
return this.formatCloseParen(token);

@@ -168,3 +160,3 @@ case _token.TokenType.RESERVED_CASE_START:

/**
* Formats word tokens + any potential AS tokens for aliases
* Formats ident/string/number/variable tokens
*/

@@ -175,11 +167,3 @@

value: function formatWord(token) {
if (this.aliasAs.shouldAddBefore(token)) {
this.query.add(this.show(this.asTokenFactory.token()), _WhitespaceBuilder.WS.SPACE);
}
this.query.add(this.show(token), _WhitespaceBuilder.WS.SPACE);
if (this.aliasAs.shouldAddAfter()) {
this.query.add(this.show(this.asTokenFactory.token()), _WhitespaceBuilder.WS.SPACE);
}
}

@@ -247,7 +231,7 @@ /**

if (type === _token.TokenType.BLOCK_START) {
if (type === _token.TokenType.OPEN_PAREN) {
openBlocks++;
}
if (type === _token.TokenType.BLOCK_END) {
if (type === _token.TokenType.CLOSE_PAREN) {
openBlocks--;

@@ -340,3 +324,3 @@ }

/**
* Formats a Reserved Keyword onto query, skipping AS if disabled
* Formats a Reserved Keyword onto query
*/

@@ -347,6 +331,2 @@

value: function formatKeyword(token) {
if (_token.isToken.AS(token) && this.aliasAs.shouldRemove()) {
return;
}
this.query.add(this.show(token), _WhitespaceBuilder.WS.SPACE);

@@ -435,4 +415,4 @@ }

}, {
key: "formatBlockStart",
value: function formatBlockStart(token) {
key: "formatOpenParen",
value: function formatOpenParen(token) {
var _token$whitespaceBefo;

@@ -442,3 +422,3 @@

// or another opening parens or line comment
var preserveWhitespaceFor = [_token.TokenType.BLOCK_START, _token.TokenType.LINE_COMMENT, _token.TokenType.OPERATOR];
var preserveWhitespaceFor = [_token.TokenType.OPEN_PAREN, _token.TokenType.LINE_COMMENT, _token.TokenType.OPERATOR];

@@ -461,4 +441,4 @@ if (((_token$whitespaceBefo = token.whitespaceBefore) === null || _token$whitespaceBefo === void 0 ? void 0 : _token$whitespaceBefo.length) === 0 && !preserveWhitespaceFor.includes(this.tokenLookBehind().type)) {

}, {
key: "formatBlockEnd",
value: function formatBlockEnd(token) {
key: "formatCloseParen",
value: function formatCloseParen(token) {
if (this.inlineBlock.isActive()) {

@@ -465,0 +445,0 @@ this.inlineBlock.end();

@@ -27,4 +27,4 @@ "use strict";

TokenType["OPERATOR"] = "OPERATOR";
TokenType["BLOCK_START"] = "BLOCK_START";
TokenType["BLOCK_END"] = "BLOCK_END";
TokenType["OPEN_PAREN"] = "OPEN_PAREN";
TokenType["CLOSE_PAREN"] = "CLOSE_PAREN";
TokenType["LINE_COMMENT"] = "LINE_COMMENT";

@@ -31,0 +31,0 @@ TokenType["BLOCK_COMMENT"] = "BLOCK_COMMENT";

@@ -75,4 +75,4 @@ "use strict";

_cfg$operators,
_cfg$blockStart,
_cfg$blockEnd,
_cfg$openParens,
_cfg$closeParens,
_cfg$lineCommentTypes,

@@ -106,3 +106,3 @@ _this$REGEX_MAP,

this.quotedIdentRegex = regexFactory.createQuoteRegex(cfg.identTypes);
this.REGEX_MAP = (_this$REGEX_MAP = {}, _defineProperty(_this$REGEX_MAP, _token.TokenType.IDENT, regexFactory.createIdentRegex(cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.STRING, regexFactory.createQuoteRegex(cfg.stringTypes)), _defineProperty(_this$REGEX_MAP, _token.TokenType.VARIABLE, cfg.variableTypes ? regexFactory.createVariableRegex(cfg.variableTypes) : NULL_REGEX), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_KEYWORD, regexFactory.createReservedWordRegex(cfg.reservedKeywords, cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_DEPENDENT_CLAUSE, regexFactory.createReservedWordRegex((_cfg$reservedDependen = cfg.reservedDependentClauses) !== null && _cfg$reservedDependen !== void 0 ? _cfg$reservedDependen : [], cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_LOGICAL_OPERATOR, regexFactory.createReservedWordRegex((_cfg$reservedLogicalO = cfg.reservedLogicalOperators) !== null && _cfg$reservedLogicalO !== void 0 ? _cfg$reservedLogicalO : ['AND', 'OR'], cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_COMMAND, regexFactory.createReservedWordRegex(cfg.reservedCommands, cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_BINARY_COMMAND, regexFactory.createReservedWordRegex(cfg.reservedBinaryCommands, cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_JOIN_CONDITION, regexFactory.createReservedWordRegex((_cfg$reservedJoinCond = cfg.reservedJoinConditions) !== null && _cfg$reservedJoinCond !== void 0 ? _cfg$reservedJoinCond : ['ON', 'USING'], cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.OPERATOR, regexFactory.createOperatorRegex('+-/*%&|^><=.,;[]{}`:$@', ['<>', '<=', '>=', '!='].concat(_toConsumableArray((_cfg$operators = cfg.operators) !== null && _cfg$operators !== void 0 ? _cfg$operators : [])))), _defineProperty(_this$REGEX_MAP, _token.TokenType.BLOCK_START, regexFactory.createParenRegex((_cfg$blockStart = cfg.blockStart) !== null && _cfg$blockStart !== void 0 ? _cfg$blockStart : ['('])), _defineProperty(_this$REGEX_MAP, _token.TokenType.BLOCK_END, regexFactory.createParenRegex((_cfg$blockEnd = cfg.blockEnd) !== null && _cfg$blockEnd !== void 0 ? _cfg$blockEnd : [')'])), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_CASE_START, new RegExp("(CA[S\\u017F]E)\\b", "iy")), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_CASE_END, new RegExp("(END)\\b", "iy")), _defineProperty(_this$REGEX_MAP, _token.TokenType.LINE_COMMENT, regexFactory.createLineCommentRegex((_cfg$lineCommentTypes = cfg.lineCommentTypes) !== null && _cfg$lineCommentTypes !== void 0 ? _cfg$lineCommentTypes : ['--'])), _defineProperty(_this$REGEX_MAP, _token.TokenType.BLOCK_COMMENT, new RegExp("(\\/\\*(?:(?![])[\\s\\S])*?(?:\\*\\/|$))", "y")), _defineProperty(_this$REGEX_MAP, _token.TokenType.NUMBER, new RegExp("(0x[0-9A-Fa-f]+|0b[01]+|(\\x2D[\\t-\\r \\xA0\\u1680\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000\\uFEFF]*)?[0-9]+(\\.[0-9]*)?([Ee][\\+\\x2D]?[0-9]+(\\.[0-9]+)?)?)", "y")), _defineProperty(_this$REGEX_MAP, _token.TokenType.PARAMETER, NULL_REGEX), _defineProperty(_this$REGEX_MAP, _token.TokenType.EOF, NULL_REGEX), _this$REGEX_MAP);
this.REGEX_MAP = (_this$REGEX_MAP = {}, _defineProperty(_this$REGEX_MAP, _token.TokenType.IDENT, regexFactory.createIdentRegex(cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.STRING, regexFactory.createQuoteRegex(cfg.stringTypes)), _defineProperty(_this$REGEX_MAP, _token.TokenType.VARIABLE, cfg.variableTypes ? regexFactory.createVariableRegex(cfg.variableTypes) : NULL_REGEX), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_KEYWORD, regexFactory.createReservedWordRegex(cfg.reservedKeywords, cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_DEPENDENT_CLAUSE, regexFactory.createReservedWordRegex((_cfg$reservedDependen = cfg.reservedDependentClauses) !== null && _cfg$reservedDependen !== void 0 ? _cfg$reservedDependen : [], cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_LOGICAL_OPERATOR, regexFactory.createReservedWordRegex((_cfg$reservedLogicalO = cfg.reservedLogicalOperators) !== null && _cfg$reservedLogicalO !== void 0 ? _cfg$reservedLogicalO : ['AND', 'OR'], cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_COMMAND, regexFactory.createReservedWordRegex(cfg.reservedCommands, cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_BINARY_COMMAND, regexFactory.createReservedWordRegex(cfg.reservedBinaryCommands, cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_JOIN_CONDITION, regexFactory.createReservedWordRegex((_cfg$reservedJoinCond = cfg.reservedJoinConditions) !== null && _cfg$reservedJoinCond !== void 0 ? _cfg$reservedJoinCond : ['ON', 'USING'], cfg.identChars)), _defineProperty(_this$REGEX_MAP, _token.TokenType.OPERATOR, regexFactory.createOperatorRegex('+-/*%&|^><=.,;[]{}`:$@', ['<>', '<=', '>=', '!='].concat(_toConsumableArray((_cfg$operators = cfg.operators) !== null && _cfg$operators !== void 0 ? _cfg$operators : [])))), _defineProperty(_this$REGEX_MAP, _token.TokenType.OPEN_PAREN, regexFactory.createParenRegex((_cfg$openParens = cfg.openParens) !== null && _cfg$openParens !== void 0 ? _cfg$openParens : ['('])), _defineProperty(_this$REGEX_MAP, _token.TokenType.CLOSE_PAREN, regexFactory.createParenRegex((_cfg$closeParens = cfg.closeParens) !== null && _cfg$closeParens !== void 0 ? _cfg$closeParens : [')'])), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_CASE_START, new RegExp("(CA[S\\u017F]E)\\b", "iy")), _defineProperty(_this$REGEX_MAP, _token.TokenType.RESERVED_CASE_END, new RegExp("(END)\\b", "iy")), _defineProperty(_this$REGEX_MAP, _token.TokenType.LINE_COMMENT, regexFactory.createLineCommentRegex((_cfg$lineCommentTypes = cfg.lineCommentTypes) !== null && _cfg$lineCommentTypes !== void 0 ? _cfg$lineCommentTypes : ['--'])), _defineProperty(_this$REGEX_MAP, _token.TokenType.BLOCK_COMMENT, new RegExp("(\\/\\*(?:(?![])[\\s\\S])*?(?:\\*\\/|$))", "y")), _defineProperty(_this$REGEX_MAP, _token.TokenType.NUMBER, new RegExp("(0x[0-9A-Fa-f]+|0b[01]+|(\\x2D[\\t-\\r \\xA0\\u1680\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000\\uFEFF]*)?[0-9]+(\\.[0-9]*)?([Ee][\\+\\x2D]?[0-9]+(\\.[0-9]+)?)?)", "y")), _defineProperty(_this$REGEX_MAP, _token.TokenType.PARAMETER, NULL_REGEX), _defineProperty(_this$REGEX_MAP, _token.TokenType.EOF, NULL_REGEX), _this$REGEX_MAP);
this.paramPatterns = this.excludePatternsWithoutRegexes([{

@@ -198,3 +198,3 @@ // :name placeholders

value: function getNextToken(previousToken) {
return this.matchToken(_token.TokenType.LINE_COMMENT) || this.matchToken(_token.TokenType.BLOCK_COMMENT) || this.matchToken(_token.TokenType.STRING) || this.matchQuotedIdentToken() || this.matchToken(_token.TokenType.VARIABLE) || this.matchToken(_token.TokenType.BLOCK_START) || this.matchToken(_token.TokenType.BLOCK_END) || this.matchPlaceholderToken() || this.matchToken(_token.TokenType.NUMBER) || this.matchReservedWordToken(previousToken) || this.matchToken(_token.TokenType.IDENT) || this.matchToken(_token.TokenType.OPERATOR);
return this.matchToken(_token.TokenType.LINE_COMMENT) || this.matchToken(_token.TokenType.BLOCK_COMMENT) || this.matchToken(_token.TokenType.STRING) || this.matchQuotedIdentToken() || this.matchToken(_token.TokenType.VARIABLE) || this.matchToken(_token.TokenType.OPEN_PAREN) || this.matchToken(_token.TokenType.CLOSE_PAREN) || this.matchPlaceholderToken() || this.matchToken(_token.TokenType.NUMBER) || this.matchReservedWordToken(previousToken) || this.matchToken(_token.TokenType.IDENT) || this.matchToken(_token.TokenType.OPERATOR);
}

@@ -201,0 +201,0 @@ }, {

@@ -251,4 +251,4 @@ "use strict";

reservedKeywords: (0, _utils.dedupe)([].concat(_toConsumableArray(Object.values(reservedFunctions).flat()), _toConsumableArray(Object.values(reservedKeywords).flat()))),
blockStart: ['(', '['],
blockEnd: [')', ']'],
openParens: ['(', '['],
closeParens: [')', ']'],
stringTypes: [// The triple-quoted strings are listed first, so they get matched first.

@@ -291,2 +291,24 @@ // Otherwise the first two quotes of """ will get matched as an empty "" string.

function preprocess(tokens) {
return detectArraySubscripts(combineParameterizedTypes(tokens));
} // Converts OFFSET token inside array from RESERVED_COMMAND to RESERVED_KEYWORD
// See: https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#array_subscript_operator
function detectArraySubscripts(tokens) {
var prevToken = _token.EOF_TOKEN;
return tokens.map(function (token) {
if (token.value === 'OFFSET' && prevToken.value === '[') {
prevToken = token;
return _objectSpread(_objectSpread({}, token), {}, {
type: _token.TokenType.RESERVED_KEYWORD
});
} else {
prevToken = token;
return token;
}
});
} // Combines multiple tokens forming a parameterized type like STRUCT<ARRAY<INT64>> into a single token
function combineParameterizedTypes(tokens) {
var processed = [];

@@ -293,0 +315,0 @@

@@ -162,4 +162,4 @@ "use strict";

reservedKeywords: (0, _utils.dedupe)([].concat(_toConsumableArray(Object.values(reservedFunctions).flat()), _toConsumableArray(Object.values(reservedKeywords).flat()))),
blockStart: ['(', '['],
blockEnd: [')', ']'],
openParens: ['(', '['],
closeParens: [')', ']'],
stringTypes: ['""', "''"],

@@ -166,0 +166,0 @@ identTypes: ['``'],

@@ -113,4 +113,4 @@ "use strict";

identTypes: ['``'],
blockStart: ['(', '[', '{'],
blockEnd: [')', ']', '}'],
openParens: ['(', '[', '{'],
closeParens: [')', ']', '}'],
positionalParams: true,

@@ -117,0 +117,0 @@ numberedParamTypes: ['$'],

@@ -145,4 +145,4 @@ "use strict";

reservedKeywords: (0, _utils.dedupe)([].concat(_toConsumableArray(Object.values(reservedFunctions).flat()), reservedKeywords)),
blockStart: ['(', '['],
blockEnd: [')', ']'],
openParens: ['(', '['],
closeParens: [')', ']'],
stringTypes: [{

@@ -176,3 +176,3 @@ quote: "''",

if (_token.isToken.WINDOW(token) && nextToken.type === _token.TokenType.BLOCK_START) {
if (_token.isToken.WINDOW(token) && nextToken.type === _token.TokenType.OPEN_PAREN) {
// This is a function call, treat it as a reserved word

@@ -179,0 +179,0 @@ return _objectSpread(_objectSpread({}, token), {}, {

@@ -1,27 +0,29 @@

import type { AliasMode } from "../types";
import type { FormatOptions } from "../types";
import { type Token } from './token';
export interface TokenStream {
isWithinSelect(): boolean;
getPreviousReservedToken(): Token;
tokenLookBehind(n?: number): Token;
tokenLookAhead(n?: number): Token;
}
/** Decides addition and removal of AS tokens */
/** Adds and removes AS tokens as configured by aliasAs option */
export default class AliasAs {
private index;
private tokens;
private previousReservedToken;
private previousCommandToken;
private asTokenFactory;
private aliasAs;
private formatter;
constructor(aliasAs: AliasMode, formatter: TokenStream);
constructor(cfg: FormatOptions, tokens: Token[]);
/** Returns tokens with AS tokens added/removed as needed */
process(): Token[];
/** True when AS keyword should be added *before* current token */
shouldAddBefore(token: Token): boolean;
private shouldAddBefore;
private isMissingTableAlias;
private isMissingSelectColumnAlias;
/** True when AS keyword should be added *after* current token */
shouldAddAfter(): boolean;
private shouldAddAfter;
private isMissingTypeCastAs;
private isEdgeCaseCTE;
private isEdgeCaseCreateTable;
shouldRemove(): boolean;
private shouldRemove;
private isRemovableNonSelectAs;
getPreviousReservedToken(): Token;
isWithinSelect(): boolean;
private lookBehind;
private lookAhead;
}
import type { FormatOptions } from "../types";
import Params from './Params';
import { type Token } from './token';
import AsTokenFactory from './AsTokenFactory';
import { type Statement } from './Parser';

@@ -11,5 +9,3 @@ /** Formats single SQL statement */

private inlineBlock;
private aliasAs;
private params;
private asTokenFactory;
private query;

@@ -21,7 +17,7 @@ private currentNewline;

private index;
constructor(cfg: FormatOptions, params: Params, asTokenFactory: AsTokenFactory);
constructor(cfg: FormatOptions, params: Params);
format(statement: Statement): string;
private formatToken;
/**
* Formats word tokens + any potential AS tokens for aliases
* Formats ident/string/number/variable tokens
*/

@@ -56,3 +52,3 @@ private formatWord;

/**
* Formats a Reserved Keyword onto query, skipping AS if disabled
* Formats a Reserved Keyword onto query
*/

@@ -73,4 +69,4 @@ private formatKeyword;

private formatLogicalOperator;
private formatBlockStart;
private formatBlockEnd;
private formatOpenParen;
private formatCloseParen;
private formatCaseStart;

@@ -92,9 +88,9 @@ private formatCaseEnd;

/** Returns the latest encountered reserved keyword token */
getPreviousReservedToken(): Token;
private getPreviousReservedToken;
/** True when currently within SELECT command */
isWithinSelect(): boolean;
private isWithinSelect;
/** Fetches nth previous token from the token stream */
tokenLookBehind(n?: number): Token;
private tokenLookBehind;
/** Fetches nth next token from the token stream */
tokenLookAhead(n?: number): Token;
private tokenLookAhead;
}

@@ -15,4 +15,4 @@ /** Token type enum for all possible Token categories */

OPERATOR = "OPERATOR",
BLOCK_START = "BLOCK_START",
BLOCK_END = "BLOCK_END",
OPEN_PAREN = "OPEN_PAREN",
CLOSE_PAREN = "CLOSE_PAREN",
LINE_COMMENT = "LINE_COMMENT",

@@ -19,0 +19,0 @@ BLOCK_COMMENT = "BLOCK_COMMENT",

@@ -14,4 +14,4 @@ import * as regexFactory from './regexFactory';

variableTypes?: regexFactory.VariableType[];
blockStart?: string[];
blockEnd?: string[];
openParens?: ('(' | '[' | '{')[];
closeParens?: (')' | ']' | '}')[];
positionalParams?: boolean;

@@ -18,0 +18,0 @@ numberedParamTypes?: ('?' | ':' | '$')[];

{
"name": "sql-formatter",
"version": "7.0.1",
"version": "7.0.2",
"description": "Format whitespace in a SQL query to make it more readable",

@@ -5,0 +5,0 @@ "license": "MIT",

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc