prettier-plugin-sql-cst 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -103,7 +103,10 @@ There are also some SQL-specific options:
103
103
  Currently this plugin supports two SQL dialects:
104
104
 
105
105
  - **SQLite** - full support.
106
- - **BigQuery** - most of the common SQL syntax is supported (see [#2][] for implementation progress).
106
+ - **BigQuery** - full support.
107
+
108
+ The specifics of the [SQL formatting style][STYLE_GUIDE] are still very much subject to change.
109
+ Though the general principles should be mostly in place by now.
107
110
 
108
111
  [prettier]: https://prettier.io/
109
112
  [printWidth]: https://prettier.io/docs/en/options.html#print-width
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.printers = exports.parsers = exports.languages = exports.options = void 0;
4
4
  var sql_parser_cst_1 = require("sql-parser-cst");
5
5
  var printSql_1 = require("./printSql");
6
+ var printComment_1 = require("./printComment");
6
7
  var embed_1 = require("./embed");
7
8
  var utils_1 = require("./utils");
8
9
  var transformCst_1 = require("./transform/transformCst");
@@ -42,9 +43,7 @@ exports.printers = {
42
43
  "sql-cst": {
43
44
  print: printSql_1.printSql,
44
45
  embed: embed_1.embed,
45
- printComment: function (path) {
46
- return path.getValue().text;
47
- },
46
+ printComment: printComment_1.printComment,
48
47
  canAttachComment: utils_1.isNode,
49
48
  isBlockComment: function (node) { return node.type === "block_comment"; },
50
49
  },
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isLanguageClause = exports.isCreateFunctionStmt = exports.isListExpr = exports.isParenExpr = exports.isAsClause = exports.isArraySubscript = exports.isStringLiteral = exports.isJsonLiteral = exports.isEmpty = exports.isKeyword = exports.isCreateTableStmt = exports.isFuncArgs = exports.isFuncCall = exports.isValuesClause = void 0;
3
+ exports.isLanguageClause = exports.isCreateFunctionStmt = exports.isListExpr = exports.isParenExpr = exports.isAsClause = exports.isArraySubscript = exports.isStringLiteral = exports.isJsonLiteral = exports.isEmpty = exports.isKeyword = exports.isCreateTableStmt = exports.isFuncArgs = exports.isFuncCall = exports.isValuesClause = exports.isProgram = void 0;
4
4
  var utils_1 = require("./utils");
5
5
  var is = function (type) {
6
6
  return function (node) {
7
7
  return (0, utils_1.isObject)(node) && node.type === type;
8
8
  };
9
9
  };
10
+ exports.isProgram = is("program");
10
11
  exports.isValuesClause = is("values_clause");
11
12
  exports.isFuncCall = is("func_call");
12
13
  exports.isFuncArgs = is("func_args");
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.printComment = void 0;
4
+ var printComment = function (path) {
5
+ var node = path.getValue();
6
+ if (node.type === "line_comment") {
7
+ if (/^--[^ \t]/.test(node.text)) {
8
+ return node.text.replace(/^--/, "-- ");
9
+ }
10
+ if (/^#[^ \t!]/.test(node.text)) {
11
+ return node.text.replace(/^#/, "# ");
12
+ }
13
+ }
14
+ return node.text;
15
+ };
16
+ exports.printComment = printComment;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.containsNewline = exports.join = exports.stripTrailingHardline = exports.breakParent = exports.ifBreak = exports.lineSuffix = exports.group = exports.indent = exports.softline = exports.hardline = exports.line = void 0;
4
+ exports.hasEmptyLineBetweenNodes = exports.containsNewline = exports.join = exports.stripTrailingHardline = exports.breakParent = exports.ifBreak = exports.lineSuffix = exports.group = exports.indent = exports.softline = exports.hardline = exports.line = void 0;
5
5
  var prettier_1 = require("prettier");
6
6
  var utils_1 = require("./utils");
7
7
  exports.line = (_a = prettier_1.doc.builders, _a.line), exports.hardline = _a.hardline, exports.softline = _a.softline, exports.indent = _a.indent, exports.group = _a.group, exports.lineSuffix = _a.lineSuffix, exports.ifBreak = _a.ifBreak, exports.breakParent = _a.breakParent;
@@ -20,3 +20,10 @@ var containsNewline = function (node, opts) {
20
20
  return prettier_1.util.hasNewlineInRange(opts.originalText, node.range[0], node.range[1]);
21
21
  };
22
22
  exports.containsNewline = containsNewline;
23
+ var hasEmptyLineBetweenNodes = function (node1, node2, opts) {
24
+ if (!node1.range || !node2.range) {
25
+ throw new Error("emptyLineBetweenNodes() expects Nodes with range info");
26
+ }
27
+ return /\n[ \t]*\r?\n/.test(opts.originalText.slice(node1.range[1], node2.range[0]));
28
+ };
29
+ exports.hasEmptyLineBetweenNodes = hasEmptyLineBetweenNodes;
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.bigqueryMap = void 0;
13
13
  var print_utils_1 = require("../print_utils");
14
14
  exports.bigqueryMap = {
15
- bigquery_options: function (print) { return print(["optionsKw", "options"]); },
15
+ bigquery_options: function (print) { return print.spaced(["optionsKw", "options"]); },
16
16
  bigquery_option_default_collate: function (print) {
17
17
  return print.spaced(["defaultCollateKw", "collation"]);
18
18
  },
@@ -91,4 +91,34 @@ exports.bigqueryMap = {
91
91
  print("actions"),
92
92
  ]);
93
93
  },
94
+ alter_capacity_stmt: function (print) {
95
+ return (0, print_utils_1.join)(print_utils_1.hardline, [
96
+ print.spaced(["alterCapacityKw", "name"]),
97
+ print("actions"),
98
+ ]);
99
+ },
100
+ alter_reservation_stmt: function (print) {
101
+ return (0, print_utils_1.join)(print_utils_1.hardline, [
102
+ print.spaced(["alterReservationKw", "name"]),
103
+ print("actions"),
104
+ ]);
105
+ },
106
+ // ASSERT
107
+ assert_stmt: function (print) { return print.spaced(["assertKw", "expr", "as"]); },
108
+ // EXPORT
109
+ export_data_stmt: function (print) {
110
+ return (0, print_utils_1.join)(print_utils_1.hardline, __spreadArray(__spreadArray([
111
+ print.spaced("exportDataKw")
112
+ ], print.spaced(["withConnection"]), true), [
113
+ print.spaced("options"),
114
+ print.spaced("as"),
115
+ ], false));
116
+ },
117
+ // LOAD
118
+ load_data_stmt: function (print) {
119
+ return (0, print_utils_1.join)(print_utils_1.hardline, __spreadArray([
120
+ print.spaced(["loadDataKw", "intoKw", "table", "columns"])
121
+ ], print("clauses"), true));
122
+ },
123
+ from_files_options: function (print) { return print.spaced(["fromFilesKw", "options"]); },
94
124
  };
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dclMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.dclMap = {
6
+ grant_stmt: function (print) {
7
+ return (0, print_utils_1.join)(print_utils_1.hardline, [
8
+ (0, print_utils_1.group)([print("grantKw"), (0, print_utils_1.indent)([print_utils_1.line, print("roles")])]),
9
+ (0, print_utils_1.group)(print.spaced(["onKw", "resourceType", "resourceName"])),
10
+ (0, print_utils_1.group)([print("toKw"), (0, print_utils_1.indent)([print_utils_1.line, print("users")])]),
11
+ ]);
12
+ },
13
+ revoke_stmt: function (print) {
14
+ return (0, print_utils_1.join)(print_utils_1.hardline, [
15
+ (0, print_utils_1.group)([print("revokeKw"), (0, print_utils_1.indent)([print_utils_1.line, print("roles")])]),
16
+ (0, print_utils_1.group)(print.spaced(["onKw", "resourceType", "resourceName"])),
17
+ (0, print_utils_1.group)([print("fromKw"), (0, print_utils_1.indent)([print_utils_1.line, print("users")])]),
18
+ ]);
19
+ },
20
+ };
@@ -65,10 +65,24 @@ exports.exprMap = {
65
65
  print_utils_1.hardline,
66
66
  print("endKw"),
67
67
  ]; },
68
- case_when: function (print) {
68
+ case_when: function (print, node) {
69
+ if ((0, node_utils_1.isProgram)(node.result)) {
70
+ return [
71
+ print.spaced(["whenKw", "condition", "thenKw"]),
72
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("result"))]),
73
+ ];
74
+ }
69
75
  return print.spaced(["whenKw", "condition", "thenKw", "result"]);
70
76
  },
71
- case_else: function (print) { return print.spaced(["elseKw", "result"]); },
77
+ case_else: function (print, node) {
78
+ if ((0, node_utils_1.isProgram)(node.result)) {
79
+ return [
80
+ print("elseKw"),
81
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("result"))]),
82
+ ];
83
+ }
84
+ return print.spaced(["elseKw", "result"]);
85
+ },
72
86
  member_expr: function (print, node) {
73
87
  return (0, node_utils_1.isArraySubscript)(node.property)
74
88
  ? print(["object", "property"])
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.preparedStatementsMap = void 0;
13
+ var print_utils_1 = require("../print_utils");
14
+ exports.preparedStatementsMap = {
15
+ execute_stmt: function (print) {
16
+ return (0, print_utils_1.join)(print_utils_1.hardline, __spreadArray([
17
+ (0, print_utils_1.group)([
18
+ print.spaced(["executeKw", "immediateKw"]),
19
+ (0, print_utils_1.indent)([print_utils_1.line, print("expr")]),
20
+ ])
21
+ ], print(["into", "using"]).map(function (clause) { return (0, print_utils_1.group)(clause); }), true));
22
+ },
23
+ execute_into_clause: function (print) { return print.spaced(["intoKw", "variables"]); },
24
+ execute_using_clause: function (print) { return print.spaced(["usingKw", "values"]); },
25
+ };
@@ -1,12 +1,119 @@
1
1
  "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.proceduralLanguageMap = void 0;
4
13
  var print_utils_1 = require("../print_utils");
5
14
  exports.proceduralLanguageMap = {
6
- block_stmt: function (print) { return [
15
+ // BEGIN .. END
16
+ block_stmt: function (print, node) { return [
7
17
  print("beginKw"),
8
18
  (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("program"))]),
19
+ node.exception ? [print_utils_1.hardline, print("exception")] : [],
9
20
  print_utils_1.hardline,
10
21
  print("endKw"),
11
22
  ]; },
23
+ exception_clause: function (print) { return [
24
+ print.spaced(["exceptionKw", "whenKw", "condition", "thenKw"]),
25
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("program"))]),
26
+ ]; },
27
+ error_category: function (print) { return print("errorKw"); },
28
+ // DECLARE
29
+ declare_stmt: function (print) {
30
+ return (0, print_utils_1.join)(" ", __spreadArray([
31
+ print("declareKw"),
32
+ (0, print_utils_1.group)(print("names"))
33
+ ], print.spaced(["dataType", "default"]), true));
34
+ },
35
+ declare_default: function (print) { return print.spaced(["defaultKw", "expr"]); },
36
+ // SET
37
+ set_stmt: function (print) { return print.spaced(["setKw", "assignments"]); },
38
+ // IF
39
+ if_stmt: function (print) {
40
+ return (0, print_utils_1.join)(print_utils_1.hardline, __spreadArray(__spreadArray([], print("clauses"), true), [print.spaced("endIfKw")], false));
41
+ },
42
+ if_clause: function (print) { return [
43
+ (0, print_utils_1.group)([
44
+ print("ifKw"),
45
+ (0, print_utils_1.indent)([print_utils_1.line, print("condition")]),
46
+ print_utils_1.line,
47
+ print("thenKw"),
48
+ ]),
49
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("consequent"))]),
50
+ ]; },
51
+ elseif_clause: function (print) { return [
52
+ (0, print_utils_1.group)([
53
+ print("elseifKw"),
54
+ (0, print_utils_1.indent)([print_utils_1.line, print("condition")]),
55
+ print_utils_1.line,
56
+ print("thenKw"),
57
+ ]),
58
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("consequent"))]),
59
+ ]; },
60
+ else_clause: function (print) { return [
61
+ print("elseKw"),
62
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("consequent"))]),
63
+ ]; },
64
+ // CASE
65
+ case_stmt: function (print) { return [
66
+ print.spaced(["caseKw", "expr"]),
67
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.join)(print_utils_1.hardline, print("clauses"))]),
68
+ print_utils_1.hardline,
69
+ print.spaced("endCaseKw"),
70
+ ]; },
71
+ // LOOP
72
+ loop_stmt: function (print) { return [
73
+ print("loopKw"),
74
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("body"))]),
75
+ print_utils_1.hardline,
76
+ print.spaced("endLoopKw"),
77
+ ]; },
78
+ // REPEAT
79
+ repeat_stmt: function (print) { return [
80
+ print("repeatKw"),
81
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("body"))]),
82
+ print_utils_1.hardline,
83
+ print.spaced(["untilKw", "condition", "endRepeatKw"]),
84
+ ]; },
85
+ // WHILE
86
+ while_stmt: function (print) { return [
87
+ print.spaced(["whileKw", "condition", "doKw"]),
88
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("body"))]),
89
+ print_utils_1.hardline,
90
+ print.spaced("endWhileKw"),
91
+ ]; },
92
+ // FOR .. IN
93
+ for_stmt: function (print) { return [
94
+ print.spaced(["forKw", "left", "inKw", "right", "doKw"]),
95
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("body"))]),
96
+ print_utils_1.hardline,
97
+ print.spaced("endForKw"),
98
+ ]; },
99
+ // BREAK/CONTINUE
100
+ break_stmt: function (print) { return print.spaced(["breakKw", "label"]); },
101
+ continue_stmt: function (print) { return print.spaced(["continueKw", "label"]); },
102
+ labeled_stmt: function (print, node) { return [
103
+ print("beginLabel"),
104
+ ": ",
105
+ print("statement"),
106
+ node.endLabel ? [" ", print(["endLabel"])] : [],
107
+ ]; },
108
+ // CALL
109
+ call_stmt: function (print) { return print.spaced(["callKw", "func"]); },
110
+ // RETURN
111
+ return_stmt: function (print) { return print("returnKw"); },
112
+ // RAISE
113
+ raise_stmt: function (print) { return print.spaced(["raiseKw", "message"]); },
114
+ raise_message: function (print) { return [
115
+ print.spaced("usingMessageKw"),
116
+ " = ",
117
+ print("string"),
118
+ ]; },
12
119
  };
@@ -3,19 +3,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.programMap = void 0;
4
4
  var print_utils_1 = require("../print_utils");
5
5
  exports.programMap = {
6
- program: function (print, node) {
6
+ program: function (print, node, path, options) {
7
7
  return print("statements").map(function (doc, i) {
8
- return printStatement(doc, i, node.statements);
8
+ return printStatement(doc, i, node.statements, options);
9
9
  });
10
10
  },
11
11
  };
12
- var printStatement = function (doc, i, statements) {
12
+ var printStatement = function (doc, i, statements, options) {
13
+ var prevNode = statements[i - 1];
13
14
  var node = statements[i];
14
15
  if (i === 0) {
15
16
  return doc;
16
17
  }
17
18
  else if (node.type !== "empty") {
18
- return [";", print_utils_1.hardline, print_utils_1.hardline, doc];
19
+ if ((0, print_utils_1.hasEmptyLineBetweenNodes)(prevNode, node, options)) {
20
+ return [";", print_utils_1.hardline, print_utils_1.hardline, doc];
21
+ }
22
+ else {
23
+ return [";", print_utils_1.hardline, doc];
24
+ }
19
25
  }
20
26
  else {
21
27
  return [";", print_utils_1.hardline, doc];
@@ -20,6 +20,7 @@ var bigquery_1 = require("./bigquery");
20
20
  var constraint_1 = require("./constraint");
21
21
  var create_table_1 = require("./create_table");
22
22
  var data_type_1 = require("./data_type");
23
+ var dcl_1 = require("./dcl");
23
24
  var delete_1 = require("./delete");
24
25
  var drop_table_1 = require("./drop_table");
25
26
  var explain_1 = require("./explain");
@@ -29,6 +30,7 @@ var function_1 = require("./function");
29
30
  var index_1 = require("./index");
30
31
  var insert_1 = require("./insert");
31
32
  var merge_1 = require("./merge");
33
+ var prepared_statements_1 = require("./prepared_statements");
32
34
  var procedural_language_1 = require("./procedural_language");
33
35
  var procedure_1 = require("./procedure");
34
36
  var proc_clause_1 = require("./proc_clause");
@@ -41,4 +43,4 @@ var trigger_1 = require("./trigger");
41
43
  var truncate_1 = require("./truncate");
42
44
  var update_1 = require("./update");
43
45
  var view_1 = require("./view");
44
- exports.transformMap = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, alias_1.aliasMap), alter_table_1.alterTableMap), analyze_1.analyzeMap), base_1.baseMap), bigquery_1.bigqueryMap), constraint_1.constraintMap), create_table_1.createTableMap), data_type_1.dataTypeMap), delete_1.deleteMap), drop_table_1.dropTableMap), explain_1.explainMap), expr_1.exprMap), frame_1.frameMap), function_1.functionMap), index_1.indexMap), insert_1.insertMap), merge_1.mergeMap), proc_clause_1.procClauseMap), procedural_language_1.proceduralLanguageMap), procedure_1.procedureMap), program_1.programMap), schema_1.schemaMap), select_1.selectMap), sqlite_1.sqliteMap), transaction_1.transactionMap), trigger_1.triggerMap), truncate_1.truncateMap), update_1.updateMap), view_1.viewMap);
46
+ exports.transformMap = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, alias_1.aliasMap), alter_table_1.alterTableMap), analyze_1.analyzeMap), base_1.baseMap), bigquery_1.bigqueryMap), constraint_1.constraintMap), create_table_1.createTableMap), data_type_1.dataTypeMap), dcl_1.dclMap), delete_1.deleteMap), drop_table_1.dropTableMap), explain_1.explainMap), expr_1.exprMap), frame_1.frameMap), function_1.functionMap), index_1.indexMap), insert_1.insertMap), merge_1.mergeMap), prepared_statements_1.preparedStatementsMap), proc_clause_1.procClauseMap), procedural_language_1.proceduralLanguageMap), procedure_1.procedureMap), program_1.programMap), schema_1.schemaMap), select_1.selectMap), sqlite_1.sqliteMap), transaction_1.transactionMap), trigger_1.triggerMap), truncate_1.truncateMap), update_1.updateMap), view_1.viewMap);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-sql-cst",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Prettier plugin for SQL",
5
5
  "author": "Rene Saarsoo <nene@triin.net>",
6
6
  "license": "GPL-3.0-or-later",
@@ -29,7 +29,7 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "prettier": "^2.8.2",
32
- "sql-parser-cst": "^0.14.0"
32
+ "sql-parser-cst": "^0.14.1"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/jest": "^29.2.5",