Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sql-formatter

Package Overview
Dependencies
Maintainers
2
Versions
146
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 10.8.0-beta.1 to 10.8.0-beta.2

bin/sql-formatter-cli.cjs

76

lib/formatter/ExpressionFormatter.js

@@ -89,2 +89,11 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

case NodeType.case_expression:
return this.formatCaseExpression(node);
case NodeType.case_when:
return this.formatCaseWhen(node);
case NodeType.case_else:
return this.formatCaseElse(node);
case NodeType.clause:

@@ -193,2 +202,29 @@ return this.formatClause(node);

}, {
key: "formatCaseExpression",
value: function formatCaseExpression(node) {
this.formatNode(node["case"]);
this.layout = this.formatSubExpression(node.expr);
this.layout.indentation.increaseBlockLevel();
this.layout = this.formatSubExpression(node.clauses);
this.layout.indentation.decreaseBlockLevel();
this.layout.add(WS.NEWLINE, WS.INDENT);
this.formatNode(node.end);
}
}, {
key: "formatCaseWhen",
value: function formatCaseWhen(node) {
this.layout.add(WS.NEWLINE, WS.INDENT);
this.formatNode(node.when);
this.layout = this.formatSubExpression(node.condition);
this.formatNode(node.then);
this.layout = this.formatSubExpression(node.result);
}
}, {
key: "formatCaseElse",
value: function formatCaseElse(node) {
this.layout.add(WS.NEWLINE, WS.INDENT);
this.formatNode(node["else"]);
this.layout = this.formatSubExpression(node.result);
}
}, {
key: "formatClause",

@@ -415,20 +451,4 @@ value: function formatClause(node) {

case TokenType.RESERVED_KEYWORD:
case TokenType.RESERVED_FUNCTION_NAME:
case TokenType.RESERVED_PHRASE:
case TokenType.THEN:
default:
return this.formatKeyword(node);
case TokenType.CASE:
return this.formatCaseStart(node);
case TokenType.END:
return this.formatCaseEnd(node);
case TokenType.WHEN:
case TokenType.ELSE:
return this.formatCaseWhenOrElse(node);
default:
throw new Error("Unexpected token type: ".concat(node.tokenType));
}

@@ -454,7 +474,2 @@ }

}, {
key: "formatCaseWhenOrElse",
value: function formatCaseWhenOrElse(node) {
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node), WS.SPACE);
}
}, {
key: "formatLogicalOperator",

@@ -476,19 +491,2 @@ value: function formatLogicalOperator(node) {

}, {
key: "formatCaseStart",
value: function formatCaseStart(node) {
this.layout.indentation.increaseBlockLevel();
this.layout.add(this.showKw(node), WS.NEWLINE, WS.INDENT);
}
}, {
key: "formatCaseEnd",
value: function formatCaseEnd(node) {
this.formatMultilineBlockEnd(node);
}
}, {
key: "formatMultilineBlockEnd",
value: function formatMultilineBlockEnd(node) {
this.layout.indentation.decreaseBlockLevel();
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node), WS.SPACE);
}
}, {
key: "showKw",

@@ -495,0 +493,0 @@ value: function showKw(node) {

@@ -12,2 +12,5 @@ export var NodeType;

NodeType["between_predicate"] = "between_predicate";
NodeType["case_expression"] = "case_expression";
NodeType["case_when"] = "case_when";
NodeType["case_else"] = "case_else";
NodeType["limit_clause"] = "limit_clause";

@@ -14,0 +17,0 @@ NodeType["all_columns_asterisk"] = "all_columns_asterisk";

@@ -373,13 +373,7 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

"name": "expression$subexpression$1",
"symbols": ["simple_expression"]
"symbols": ["asteriskless_expression"]
}, {
"name": "expression$subexpression$1",
"symbols": ["between_predicate"]
"symbols": ["asterisk"]
}, {
"name": "expression$subexpression$1",
"symbols": ["comma"]
}, {
"name": "expression$subexpression$1",
"symbols": ["comment"]
}, {
"name": "expression",

@@ -396,2 +390,5 @@ "symbols": ["expression$subexpression$1"],

"name": "asteriskless_expression$subexpression$1",
"symbols": ["case_expression"]
}, {
"name": "asteriskless_expression$subexpression$1",
"symbols": ["comma"]

@@ -402,2 +399,5 @@ }, {

}, {
"name": "asteriskless_expression$subexpression$1",
"symbols": ["other_keyword"]
}, {
"name": "asteriskless_expression",

@@ -644,2 +644,115 @@ "symbols": ["asteriskless_expression$subexpression$1"],

}, {
"name": "case_expression$ebnf$1",
"symbols": []
}, {
"name": "case_expression$ebnf$1",
"symbols": ["case_expression$ebnf$1", "simple_expression"],
"postprocess": function postprocess(d) {
return d[0].concat([d[1]]);
}
}, {
"name": "case_expression$ebnf$2",
"symbols": []
}, {
"name": "case_expression$ebnf$2",
"symbols": ["case_expression$ebnf$2", "case_clause"],
"postprocess": function postprocess(d) {
return d[0].concat([d[1]]);
}
}, {
"name": "case_expression",
"symbols": [lexer.has("CASE") ? {
type: "CASE"
} : CASE, "_", "case_expression$ebnf$1", "case_expression$ebnf$2", "_", lexer.has("END") ? {
type: "END"
} : END],
"postprocess": function postprocess(_ref37) {
var _ref38 = _slicedToArray(_ref37, 6),
caseToken = _ref38[0],
_1 = _ref38[1],
expr = _ref38[2],
clauses = _ref38[3],
_2 = _ref38[4],
endToken = _ref38[5];
return {
type: NodeType.case_expression,
"case": addTrailingComments(toKeywordNode(caseToken), _1),
end: addLeadingComments(toKeywordNode(endToken), _2),
expr: expr,
clauses: clauses
};
}
}, {
"name": "case_clause$ebnf$1",
"symbols": ["simple_expression"]
}, {
"name": "case_clause$ebnf$1",
"symbols": ["case_clause$ebnf$1", "simple_expression"],
"postprocess": function postprocess(d) {
return d[0].concat([d[1]]);
}
}, {
"name": "case_clause$ebnf$2",
"symbols": ["simple_expression"]
}, {
"name": "case_clause$ebnf$2",
"symbols": ["case_clause$ebnf$2", "simple_expression"],
"postprocess": function postprocess(d) {
return d[0].concat([d[1]]);
}
}, {
"name": "case_clause",
"symbols": ["_", lexer.has("WHEN") ? {
type: "WHEN"
} : WHEN, "_", "case_clause$ebnf$1", "_", lexer.has("THEN") ? {
type: "THEN"
} : THEN, "_", "case_clause$ebnf$2"],
"postprocess": function postprocess(_ref39) {
var _ref40 = _slicedToArray(_ref39, 8),
_1 = _ref40[0],
whenToken = _ref40[1],
_2 = _ref40[2],
cond = _ref40[3],
_3 = _ref40[4],
thenToken = _ref40[5],
_4 = _ref40[6],
expr = _ref40[7];
return {
type: NodeType.case_when,
when: addTrailingComments(addLeadingComments(toKeywordNode(whenToken), _1), _2),
then: addTrailingComments(addLeadingComments(toKeywordNode(thenToken), _3), _4),
condition: cond,
result: expr
};
}
}, {
"name": "case_clause$ebnf$3",
"symbols": ["simple_expression"]
}, {
"name": "case_clause$ebnf$3",
"symbols": ["case_clause$ebnf$3", "simple_expression"],
"postprocess": function postprocess(d) {
return d[0].concat([d[1]]);
}
}, {
"name": "case_clause",
"symbols": ["_", lexer.has("ELSE") ? {
type: "ELSE"
} : ELSE, "_", "case_clause$ebnf$3"],
"postprocess": function postprocess(_ref41) {
var _ref42 = _slicedToArray(_ref41, 4),
_1 = _ref42[0],
elseToken = _ref42[1],
_2 = _ref42[2],
expr = _ref42[3];
return {
type: NodeType.case_else,
"else": addTrailingComments(addLeadingComments(toKeywordNode(elseToken), _1), _2),
result: expr
};
}
}, {
"name": "comma$subexpression$1",

@@ -652,6 +765,6 @@ "symbols": [lexer.has("COMMA") ? {

"symbols": ["comma$subexpression$1"],
"postprocess": function postprocess(_ref37) {
var _ref38 = _slicedToArray(_ref37, 1),
_ref38$ = _slicedToArray(_ref38[0], 1),
token = _ref38$[0];
"postprocess": function postprocess(_ref43) {
var _ref44 = _slicedToArray(_ref43, 1),
_ref44$ = _slicedToArray(_ref44[0], 1),
token = _ref44$[0];

@@ -670,6 +783,6 @@ return {

"symbols": ["asterisk$subexpression$1"],
"postprocess": function postprocess(_ref39) {
var _ref40 = _slicedToArray(_ref39, 1),
_ref40$ = _slicedToArray(_ref40[0], 1),
token = _ref40$[0];
"postprocess": function postprocess(_ref45) {
var _ref46 = _slicedToArray(_ref45, 1),
_ref46$ = _slicedToArray(_ref46[0], 1),
token = _ref46$[0];

@@ -689,6 +802,6 @@ return {

"symbols": ["operator$subexpression$1"],
"postprocess": function postprocess(_ref41) {
var _ref42 = _slicedToArray(_ref41, 1),
_ref42$ = _slicedToArray(_ref42[0], 1),
token = _ref42$[0];
"postprocess": function postprocess(_ref47) {
var _ref48 = _slicedToArray(_ref47, 1),
_ref48$ = _slicedToArray(_ref48[0], 1),
token = _ref48$[0];

@@ -718,6 +831,6 @@ return {

"symbols": ["identifier$subexpression$1"],
"postprocess": function postprocess(_ref43) {
var _ref44 = _slicedToArray(_ref43, 1),
_ref44$ = _slicedToArray(_ref44[0], 1),
token = _ref44$[0];
"postprocess": function postprocess(_ref49) {
var _ref50 = _slicedToArray(_ref49, 1),
_ref50$ = _slicedToArray(_ref50[0], 1),
token = _ref50$[0];

@@ -752,6 +865,6 @@ return {

"symbols": ["parameter$subexpression$1"],
"postprocess": function postprocess(_ref45) {
var _ref46 = _slicedToArray(_ref45, 1),
_ref46$ = _slicedToArray(_ref46[0], 1),
token = _ref46$[0];
"postprocess": function postprocess(_ref51) {
var _ref52 = _slicedToArray(_ref51, 1),
_ref52$ = _slicedToArray(_ref52[0], 1),
token = _ref52$[0];

@@ -777,6 +890,6 @@ return {

"symbols": ["literal$subexpression$1"],
"postprocess": function postprocess(_ref47) {
var _ref48 = _slicedToArray(_ref47, 1),
_ref48$ = _slicedToArray(_ref48[0], 1),
token = _ref48$[0];
"postprocess": function postprocess(_ref53) {
var _ref54 = _slicedToArray(_ref53, 1),
_ref54$ = _slicedToArray(_ref54[0], 1),
token = _ref54$[0];

@@ -805,27 +918,2 @@ return {

"name": "keyword$subexpression$1",
"symbols": [lexer.has("CASE") ? {
type: "CASE"
} : CASE]
}, {
"name": "keyword$subexpression$1",
"symbols": [lexer.has("END") ? {
type: "END"
} : END]
}, {
"name": "keyword$subexpression$1",
"symbols": [lexer.has("WHEN") ? {
type: "WHEN"
} : WHEN]
}, {
"name": "keyword$subexpression$1",
"symbols": [lexer.has("ELSE") ? {
type: "ELSE"
} : ELSE]
}, {
"name": "keyword$subexpression$1",
"symbols": [lexer.has("THEN") ? {
type: "THEN"
} : THEN]
}, {
"name": "keyword$subexpression$1",
"symbols": [lexer.has("AND") ? {

@@ -847,6 +935,6 @@ type: "AND"

"symbols": ["keyword$subexpression$1"],
"postprocess": function postprocess(_ref49) {
var _ref50 = _slicedToArray(_ref49, 1),
_ref50$ = _slicedToArray(_ref50[0], 1),
token = _ref50$[0];
"postprocess": function postprocess(_ref55) {
var _ref56 = _slicedToArray(_ref55, 1),
_ref56$ = _slicedToArray(_ref56[0], 1),
token = _ref56$[0];

@@ -856,2 +944,32 @@ return toKeywordNode(token);

}, {
"name": "other_keyword$subexpression$1",
"symbols": [lexer.has("WHEN") ? {
type: "WHEN"
} : WHEN]
}, {
"name": "other_keyword$subexpression$1",
"symbols": [lexer.has("THEN") ? {
type: "THEN"
} : THEN]
}, {
"name": "other_keyword$subexpression$1",
"symbols": [lexer.has("ELSE") ? {
type: "ELSE"
} : ELSE]
}, {
"name": "other_keyword$subexpression$1",
"symbols": [lexer.has("END") ? {
type: "END"
} : END]
}, {
"name": "other_keyword",
"symbols": ["other_keyword$subexpression$1"],
"postprocess": function postprocess(_ref57) {
var _ref58 = _slicedToArray(_ref57, 1),
_ref58$ = _slicedToArray(_ref58[0], 1),
token = _ref58$[0];
return toKeywordNode(token);
}
}, {
"name": "_$ebnf$1",

@@ -868,5 +986,5 @@ "symbols": []

"symbols": ["_$ebnf$1"],
"postprocess": function postprocess(_ref51) {
var _ref52 = _slicedToArray(_ref51, 1),
comments = _ref52[0];
"postprocess": function postprocess(_ref59) {
var _ref60 = _slicedToArray(_ref59, 1),
comments = _ref60[0];

@@ -880,5 +998,5 @@ return comments;

} : LINE_COMMENT],
"postprocess": function postprocess(_ref53) {
var _ref54 = _slicedToArray(_ref53, 1),
token = _ref54[0];
"postprocess": function postprocess(_ref61) {
var _ref62 = _slicedToArray(_ref61, 1),
token = _ref62[0];

@@ -896,5 +1014,5 @@ return {

} : BLOCK_COMMENT],
"postprocess": function postprocess(_ref55) {
var _ref56 = _slicedToArray(_ref55, 1),
token = _ref56[0];
"postprocess": function postprocess(_ref63) {
var _ref64 = _slicedToArray(_ref63, 1),
token = _ref64[0];

@@ -901,0 +1019,0 @@ return {

@@ -28,2 +28,5 @@ import { FormatOptions } from "../FormatOptions";

private formatBetweenPredicate;
private formatCaseExpression;
private formatCaseWhen;
private formatCaseElse;
private formatClause;

@@ -48,7 +51,3 @@ private formatSetOperation;

private formatKeyword;
private formatCaseWhenOrElse;
private formatLogicalOperator;
private formatCaseStart;
private formatCaseEnd;
private formatMultilineBlockEnd;
private showKw;

@@ -55,0 +54,0 @@ private showNonTabularKw;

@@ -11,2 +11,5 @@ import { TokenType } from "../lexer/token";

between_predicate = "between_predicate",
case_expression = "case_expression",
case_when = "case_when",
case_else = "case_else",
limit_clause = "limit_clause",

@@ -65,2 +68,21 @@ all_columns_asterisk = "all_columns_asterisk",

}
export interface CaseExpressionNode extends BaseNode {
type: NodeType.case_expression;
case: KeywordNode;
end: KeywordNode;
expr: AstNode[];
clauses: (CaseWhenNode | CaseElseNode)[];
}
export interface CaseWhenNode extends BaseNode {
type: NodeType.case_when;
when: KeywordNode;
then: KeywordNode;
condition: AstNode[];
result: AstNode[];
}
export interface CaseElseNode extends BaseNode {
type: NodeType.case_else;
else: KeywordNode;
result: AstNode[];
}
export interface LimitClauseNode extends BaseNode {

@@ -116,3 +138,3 @@ type: NodeType.limit_clause;

export declare type CommentNode = LineCommentNode | BlockCommentNode;
export declare type AstNode = ClauseNode | SetOperationNode | FunctionCallNode | ArraySubscriptNode | PropertyAccessNode | ParenthesisNode | BetweenPredicateNode | LimitClauseNode | AllColumnsAsteriskNode | LiteralNode | IdentifierNode | KeywordNode | ParameterNode | OperatorNode | CommaNode | LineCommentNode | BlockCommentNode;
export declare type AstNode = ClauseNode | SetOperationNode | FunctionCallNode | ArraySubscriptNode | PropertyAccessNode | ParenthesisNode | BetweenPredicateNode | CaseExpressionNode | CaseWhenNode | CaseElseNode | LimitClauseNode | AllColumnsAsteriskNode | LiteralNode | IdentifierNode | KeywordNode | ParameterNode | OperatorNode | CommaNode | LineCommentNode | BlockCommentNode;
export {};
{
"name": "sql-formatter",
"version": "10.8.0-beta.1",
"version": "10.8.0-beta.2",
"description": "Format whitespace in a SQL query to make it more readable",
"license": "MIT",
"main": "dist/sql-formatter.min.js",
"main": "dist/sql-formatter.min.cjs",
"module": "lib/index.js",
"types": "lib/src/index.d.ts",
"exports": {
".": "./lib/index.js"
".": {
"types": "./lib/src/index.d.ts",
"import": "./lib/index.js",
"require": "./dist/sql-formatter.min.cjs"
}
},
"bin": {
"sql-formatter": "./bin/sql-formatter-cli.js"
"sql-formatter": "./bin/sql-formatter-cli.cjs"
},

@@ -15,0 +19,0 @@ "type": "module",

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