prettier-plugin-sql-cst 0.3.0 → 0.5.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
@@ -8,7 +8,7 @@ this plugin formats SQL expressions differently depending on their length.
8
8
  A short SQL query will be formatted on a single line:
9
9
 
10
10
  ```sql
11
- SELECT a, b, c FROM tbl WHERE x > 10
11
+ SELECT a, b, c FROM tbl WHERE x > 10;
12
12
  ```
13
13
 
14
14
  A longer query, will get each clause printed on a separate line:
@@ -16,7 +16,7 @@ A longer query, will get each clause printed on a separate line:
16
16
  ```sql
17
17
  SELECT id, client.name, client.priority
18
18
  FROM client
19
- WHERE client.id IN (12, 18, 121)
19
+ WHERE client.id IN (12, 18, 121);
20
20
  ```
21
21
 
22
22
  An even longer one gets the contents of each clause indented:
@@ -37,7 +37,7 @@ WHERE
37
37
  AND order.status IN ('active', 'pending', 'processing')
38
38
  GROUP BY client.id
39
39
  ORDER BY client.name
40
- LIMIT 100
40
+ LIMIT 100;
41
41
  ```
42
42
 
43
43
  ## Formatting philosophy
@@ -47,16 +47,11 @@ LIMIT 100
47
47
  - Format embedded languages (like JSON data and JavaScript programs).
48
48
  - When unsure, preserve existing syntax.
49
49
 
50
- ## Non-whitespace formatting
51
-
52
50
  Currently this plugin preserves most of the syntax elements
53
51
  and concentrates mainly on the layout of whitespace.
54
52
  For example it does not add/remove paratheses.
55
53
 
56
- There are some opinionated non-whitespace changes though:
57
-
58
- - UPPERCASE all keywords.
59
- - Add `AS` keyword to all alias definitions.
54
+ See [STYLE_GUIDE][] for overview of the SQL formatting style used.
60
55
 
61
56
  ## Getting started
62
57
 
@@ -66,7 +61,7 @@ Install it as any other Prettier plugin:
66
61
  npm install --save-dev prettier prettier-plugin-sql-cst
67
62
  ```
68
63
 
69
- Then use it on SQL files though Prettier command line tool or Prettier extension
64
+ Then use it on SQL files through Prettier command line tool or Prettier extension
70
65
  for your editor of choice.
71
66
 
72
67
  ## Choosing an SQL dialect
@@ -105,17 +100,18 @@ There are also some SQL-specific options:
105
100
 
106
101
  ## Limitations and development status
107
102
 
108
- Currently this plugin supports most of **SQLite** syntax.
109
- (It also supports some **BigQuery**, but only the syntax that's subset of SQLite.)
110
- Formatting of the following SQL statements is fully implemented:
103
+ Currently this plugin supports full **SQLite** syntax.
104
+ It also supports most of the **BigQuery** syntax.
105
+ Specifically the formatting of the following BigQuery SQL statements is fully implemented:
111
106
 
112
107
  - SELECT
113
108
  - UPDATE
114
109
  - INSERT
115
110
  - DELETE
111
+ - TRUNCATE
116
112
  - CREATE / DROP / ALTER TABLE
117
113
  - CREATE / DROP VIEW
118
- - CREATE / DROP INDEX
114
+ - transactions (BEGIN, COMMIT, ROLLBACK)
119
115
 
120
116
  [prettier]: https://prettier.io/
121
117
  [printWidth]: https://prettier.io/docs/en/options.html#print-width
@@ -123,3 +119,4 @@ Formatting of the following SQL statements is fully implemented:
123
119
  [useTabs]: https://prettier.io/docs/en/options.html#tabs
124
120
  [sql-parser-cst]: https://github.com/nene/sql-parser-cst
125
121
  [wadler-prettier]: http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf
122
+ [STYLE_GUIDE]: ./STYLE_GUIDE.md
package/dist/index.js CHANGED
@@ -27,7 +27,7 @@ var createParser = function (dialect) { return ({
27
27
  includeRange: true,
28
28
  includeComments: true,
29
29
  filename: options.filepath,
30
- }), options);
30
+ }), text);
31
31
  },
32
32
  astFormat: "sql-cst",
33
33
  locStart: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[0]; },
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isStringLiteral = exports.isJsonLiteral = exports.isEmpty = exports.isKeyword = exports.isCreateTableStmt = exports.isFuncCall = exports.isValuesClause = void 0;
3
+ exports.isAsClause = exports.isArraySubscript = exports.isStringLiteral = exports.isJsonLiteral = exports.isEmpty = exports.isKeyword = exports.isCreateTableStmt = exports.isFuncCall = exports.isValuesClause = void 0;
4
4
  var utils_1 = require("./utils");
5
5
  var is = function (type) {
6
6
  return function (node) {
@@ -14,3 +14,5 @@ exports.isKeyword = is("keyword");
14
14
  exports.isEmpty = is("empty");
15
15
  exports.isJsonLiteral = is("json_literal");
16
16
  exports.isStringLiteral = is("string_literal");
17
+ exports.isArraySubscript = is("array_subscript");
18
+ exports.isAsClause = is("as_clause");
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.containsNewline = exports.join = exports.stripTrailingHardline = exports.ifBreak = exports.lineSuffix = exports.group = exports.indent = exports.softline = exports.hardline = exports.line = void 0;
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;
5
5
  var prettier_1 = require("prettier");
6
6
  var utils_1 = require("./utils");
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;
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;
8
8
  var origJoin = prettier_1.doc.builders.join;
9
9
  exports.stripTrailingHardline = prettier_1.doc.utils.stripTrailingHardline;
10
10
  /** Improved join() that also accepts a non-array docs parameter */
@@ -4,14 +4,33 @@ exports.alterTableMap = void 0;
4
4
  var print_utils_1 = require("../print_utils");
5
5
  exports.alterTableMap = {
6
6
  alter_table_stmt: function (print) { return [
7
- print.spaced(["alterTableKw", "table"]),
7
+ print.spaced(["alterTableKw", "ifExistsKw", "table"]),
8
8
  print_utils_1.hardline,
9
9
  print("actions"),
10
10
  ]; },
11
11
  alter_action_rename_table: function (print) { return print.spaced(["renameKw", "newName"]); },
12
12
  alter_action_rename_column: function (print) {
13
- return print.spaced(["renameKw", "oldName", "toKw", "newName"]);
13
+ return print.spaced(["renameKw", "ifExistsKw", "oldName", "toKw", "newName"]);
14
+ },
15
+ alter_action_add_column: function (print) {
16
+ return print.spaced(["addKw", "ifNotExistsKw", "column"]);
17
+ },
18
+ alter_action_drop_column: function (print) {
19
+ return print.spaced(["dropKw", "ifExistsKw", "column"]);
20
+ },
21
+ alter_action_set_options: function (print) { return print.spaced(["setKw", "options"]); },
22
+ alter_action_set_default_collate: function (print) {
23
+ return print.spaced(["setDefaultCollateKw", "collation"]);
24
+ },
25
+ alter_action_alter_column: function (print) { return [
26
+ print.spaced(["alterKw", "ifExistsKw", "column"]),
27
+ print_utils_1.hardline,
28
+ print("action"),
29
+ ]; },
30
+ alter_action_set_default: function (print) { return print.spaced(["setDefaultKw", "expr"]); },
31
+ alter_action_drop_default: function (print) { return print.spaced("dropDefaultKw"); },
32
+ alter_action_drop_not_null: function (print) { return print.spaced("dropNotNullKw"); },
33
+ alter_action_set_data_type: function (print) {
34
+ return print.spaced(["setDataTypeKw", "dataType"]);
14
35
  },
15
- alter_action_add_column: function (print) { return print.spaced(["addKw", "column"]); },
16
- alter_action_drop_column: function (print) { return print.spaced(["dropKw", "column"]); },
17
36
  };
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.analyzeMap = void 0;
4
+ exports.analyzeMap = {
5
+ analyze_stmt: function (print) { return print.spaced(["analyzeKw", "tableKw", "tables"]); },
6
+ };
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bigqueryMap = void 0;
4
+ exports.bigqueryMap = {
5
+ bigquery_options: function (print) { return print(["optionsKw", "options"]); },
6
+ bigquery_option_default_collate: function (print) {
7
+ return print.spaced(["defaultCollateKw", "collation"]);
8
+ },
9
+ };
@@ -1,22 +1,58 @@
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.createTableMap = void 0;
13
+ var node_utils_1 = require("../node_utils");
4
14
  var print_utils_1 = require("../print_utils");
5
15
  exports.createTableMap = {
6
- create_table_stmt: function (print, node) { return [
7
- print.spaced([
8
- "createKw",
9
- "temporaryKw",
10
- "tableKw",
11
- "ifNotExistsKw",
12
- "name",
13
- "columns",
14
- "clauses",
15
- ]),
16
- node.options ? [" ", (0, print_utils_1.group)(print("options"))] : [],
17
- ]; },
16
+ create_table_stmt: function (print, node) {
17
+ var rest = [];
18
+ for (var _i = 2; _i < arguments.length; _i++) {
19
+ rest[_i - 2] = arguments[_i];
20
+ }
21
+ return [
22
+ print.spaced([
23
+ "createKw",
24
+ "orReplaceKw",
25
+ "temporaryKw",
26
+ "externalKw",
27
+ "snapshotKw",
28
+ "tableKw",
29
+ "ifNotExistsKw",
30
+ "name",
31
+ "columns",
32
+ ]),
33
+ printClauses.apply(void 0, __spreadArray([print, node], rest, false)),
34
+ node.options ? [print_utils_1.line, (0, print_utils_1.group)(print("options"))] : [],
35
+ ];
36
+ },
18
37
  column_definition: function (print) {
19
38
  return print.spaced(["name", "dataType", "constraints"]);
20
39
  },
21
40
  table_option: function (print) { return print.spaced(["name"]); },
41
+ create_table_like_clause: function (print) { return print.spaced(["likeKw", "name"]); },
42
+ create_table_copy_clause: function (print) { return print.spaced(["copyKw", "name"]); },
43
+ create_table_clone_clause: function (print) { return print.spaced(["cloneKw", "table"]); },
44
+ with_partition_columns_clause: function (print) {
45
+ return print.spaced(["withPartitionColumnsKw", "columns"]);
46
+ },
47
+ };
48
+ var printClauses = function (print, node) {
49
+ if (node.clauses.length === 1 && (0, node_utils_1.isAsClause)(node.clauses[0])) {
50
+ return [" ", print("clauses")];
51
+ }
52
+ else if (node.clauses.length > 0) {
53
+ return [print_utils_1.line, (0, print_utils_1.join)(print_utils_1.line, print("clauses"))];
54
+ }
55
+ else {
56
+ return [];
57
+ }
22
58
  };
@@ -1,18 +1,15 @@
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
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.dataTypeMap = void 0;
4
+ var utils_1 = require("../utils");
13
5
  exports.dataTypeMap = {
14
- data_type: function (print) { return __spreadArray([print.spaced("nameKw")], print(["params"]), true); },
6
+ // print single-word types as `TYPE(10)` and multi-word types as `MY TYPE (10)`
7
+ data_type: function (print, node) {
8
+ return ((0, utils_1.isArray)(node.nameKw) ? print.spaced : print)(["nameKw", "params"]);
9
+ },
15
10
  generic_type_params: function (print) { return ["<", print("params"), ">"]; },
16
- array_type_param: function (print) { return print("dataType"); },
17
- struct_type_param: function (print) { return print.spaced(["name", "dataType"]); },
11
+ array_type_param: function (print) { return print.spaced(["dataType", "constraints"]); },
12
+ struct_type_param: function (print) {
13
+ return print.spaced(["name", "dataType", "constraints"]);
14
+ },
18
15
  };
@@ -3,6 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.dropTableMap = void 0;
4
4
  exports.dropTableMap = {
5
5
  drop_table_stmt: function (print) {
6
- return print.spaced(["dropKw", "tableKw", "ifExistsKw", "tables"]);
6
+ return print.spaced([
7
+ "dropKw",
8
+ "externalKw",
9
+ "snapshotKw",
10
+ "tableKw",
11
+ "ifExistsKw",
12
+ "tables",
13
+ ]);
7
14
  },
8
15
  };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.explainMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.explainMap = {
6
+ explain_stmt: function (print) {
7
+ return (0, print_utils_1.group)([
8
+ print.spaced(["explainKw", "queryPlanKw"]),
9
+ (0, print_utils_1.indent)([print_utils_1.line, print("statement")]),
10
+ ]);
11
+ },
12
+ };
@@ -60,8 +60,16 @@ exports.exprMap = {
60
60
  return print.spaced(["whenKw", "condition", "thenKw", "result"]);
61
61
  },
62
62
  case_else: function (print) { return print.spaced(["elseKw", "result"]); },
63
- member_expr: function (print) { return [print("object"), ".", print("property")]; },
63
+ member_expr: function (print, node) {
64
+ return (0, node_utils_1.isArraySubscript)(node.property)
65
+ ? print(["object", "property"])
66
+ : [print("object"), ".", print("property")];
67
+ },
64
68
  bigquery_quoted_member_expr: function (print) { return ["`", print("expr"), "`"]; },
69
+ array_subscript: function (print) {
70
+ return (0, print_utils_1.group)(["[", (0, print_utils_1.indent)([print_utils_1.softline, print("expr")]), print_utils_1.softline, "]"]);
71
+ },
72
+ array_subscript_specifier: function (print) { return print(["specifierKw", "args"]); },
65
73
  func_call: function (print) {
66
74
  var fnCall = print(["name", "args"]);
67
75
  var extras = print(["filter", "over"]);
@@ -14,6 +14,7 @@ exports.insertMap = {
14
14
  return (0, print_utils_1.group)([print("valuesKw"), (0, print_utils_1.indent)([print_utils_1.hardline, print("values")])]);
15
15
  },
16
16
  or_alternate_action: function (print) { return print.spaced(["orKw", "actionKw"]); },
17
+ default: function (print) { return print("defaultKw"); },
17
18
  default_values: function (print) { return print.spaced("defaultValuesKw"); },
18
19
  upsert_clause: function (print) {
19
20
  return print.spaced(["onConflictKw", "columns", "where", "doKw", "action"]);
@@ -4,4 +4,7 @@ exports.procClauseMap = void 0;
4
4
  var print_utils_1 = require("../print_utils");
5
5
  exports.procClauseMap = {
6
6
  as_clause: function (print) { return [print("asKw"), (0, print_utils_1.indent)([print_utils_1.hardline, print("expr")])]; },
7
+ with_connection_clause: function (print) {
8
+ return print.spaced(["withConnectionKw", "connection"]);
9
+ },
7
10
  };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.proceduralLanguageMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.proceduralLanguageMap = {
6
+ block_stmt: function (print) { return [
7
+ print("beginKw"),
8
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(print("program"))]),
9
+ print_utils_1.hardline,
10
+ print("endKw"),
11
+ ]; },
12
+ };
@@ -3,22 +3,21 @@ 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) { return [
7
- print("statements").map(function (doc, i) {
6
+ program: function (print, node) {
7
+ return print("statements").map(function (doc, i) {
8
8
  return printStatement(doc, i, node.statements);
9
- }),
10
- print_utils_1.hardline,
11
- ]; },
9
+ });
10
+ },
12
11
  };
13
12
  var printStatement = function (doc, i, statements) {
14
13
  var node = statements[i];
15
14
  if (i === 0) {
16
15
  return doc;
17
16
  }
18
- else if (i < statements.length - 1 || node.type !== "empty") {
17
+ else if (node.type !== "empty") {
19
18
  return [";", print_utils_1.hardline, print_utils_1.hardline, doc];
20
19
  }
21
20
  else {
22
- return [";", doc];
21
+ return [";", print_utils_1.hardline, doc];
23
22
  }
24
23
  };
@@ -22,10 +22,11 @@ exports.selectMap = {
22
22
  " ",
23
23
  print.spaced(["asKw", "materializedKw", "expr"]),
24
24
  ]; },
25
- select_clause: function (print) {
25
+ select_clause: function (print, node, path, opts) {
26
26
  return (0, print_utils_1.group)([
27
27
  print.spaced(["selectKw", "distinctKw", "asStructOrValueKw"]),
28
28
  (0, print_utils_1.indent)([print_utils_1.line, print("columns")]),
29
+ (0, print_utils_1.containsNewline)(node, opts) ? print_utils_1.breakParent : [],
29
30
  ]);
30
31
  },
31
32
  except_columns: function (print) { return print.spaced(["expr", "exceptKw", "columns"]); },
@@ -57,6 +58,39 @@ exports.selectMap = {
57
58
  join_using_specification: function (print) { return (0, print_utils_1.group)(print.spaced(["usingKw", "expr"])); },
58
59
  indexed_table: function (print) { return print.spaced(["table", "indexedByKw", "index"]); },
59
60
  not_indexed_table: function (print) { return print.spaced(["table", "notIndexedKw"]); },
61
+ unnest_expr: function (print) { return print(["unnestKw", "expr"]); },
62
+ unnest_with_offset_expr: function (print) { return print.spaced(["unnest", "withOffsetKw"]); },
63
+ pivot_expr: function (print) { return [print("left"), print_utils_1.hardline, print(["pivotKw", "args"])]; },
64
+ pivot_for_in: function (print) {
65
+ return (0, print_utils_1.group)((0, print_utils_1.join)(print_utils_1.line, [
66
+ (0, print_utils_1.group)(print("aggregations")),
67
+ print.spaced(["forKw", "inputColumn"]),
68
+ print.spaced(["inKw", "pivotColumns"]),
69
+ ]));
70
+ },
71
+ unpivot_expr: function (print, node) { return [
72
+ print("left"),
73
+ print_utils_1.hardline,
74
+ (node.nullHandlingKw ? print.spaced : print)([
75
+ "unpivotKw",
76
+ "nullHandlingKw",
77
+ "args",
78
+ ]),
79
+ ]; },
80
+ unpivot_for_in: function (print) {
81
+ return (0, print_utils_1.group)((0, print_utils_1.join)(print_utils_1.line, [
82
+ print("valuesColumn"),
83
+ print.spaced(["forKw", "nameColumn"]),
84
+ print.spaced(["inKw", "unpivotColumns"]),
85
+ ]));
86
+ },
87
+ tablesample_expr: function (print) {
88
+ return (0, print_utils_1.group)([print("left"), print_utils_1.line, print.spaced(["tablesampleKw", "args"])]);
89
+ },
90
+ tablesample_percent: function (print) { return print.spaced(["percent", "percentKw"]); },
91
+ for_system_time_as_of_expr: function (print) {
92
+ return (0, print_utils_1.group)([print("left"), print_utils_1.line, print.spaced(["forSystemTimeAsOfKw", "expr"])]);
93
+ },
60
94
  where_clause: function (print) {
61
95
  return (0, print_utils_1.group)([print("whereKw"), (0, print_utils_1.indent)([print_utils_1.line, print("expr")])]);
62
96
  },
@@ -73,6 +107,9 @@ exports.selectMap = {
73
107
  (0, print_utils_1.indent)([print_utils_1.line, print("specifications")]),
74
108
  ]);
75
109
  },
110
+ cluster_by_clause: function (print) {
111
+ return (0, print_utils_1.group)([print.spaced("clusterByKw"), (0, print_utils_1.indent)([print_utils_1.line, print("columns")])]);
112
+ },
76
113
  having_clause: function (print) {
77
114
  return (0, print_utils_1.group)([print("havingKw"), (0, print_utils_1.indent)([print_utils_1.line, print("expr")])]);
78
115
  },
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sqliteMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.sqliteMap = {
6
+ attach_database_stmt: function (print) {
7
+ return print.spaced(["attachKw", "databaseKw", "file", "asKw", "schema"]);
8
+ },
9
+ detach_database_stmt: function (print) {
10
+ return print.spaced(["detachKw", "databaseKw", "schema"]);
11
+ },
12
+ vacuum_stmt: function (print) {
13
+ return print.spaced(["vacuumKw", "schema", "intoKw", "file"]);
14
+ },
15
+ reindex_stmt: function (print) { return print.spaced(["reindexKw", "table"]); },
16
+ pragma_stmt: function (print) { return print.spaced(["pragmaKw", "pragma"]); },
17
+ pragma_assignment: function (print) { return (0, print_utils_1.join)(" = ", print(["name", "value"])); },
18
+ pragma_func_call: function (print) { return print(["name", "args"]); },
19
+ create_virtual_table_stmt: function (print) {
20
+ return (0, print_utils_1.join)(print_utils_1.hardline, [
21
+ print.spaced(["createVirtualTableKw", "ifNotExistsKw", "name"]),
22
+ print.spaced(["usingKw", "module"]),
23
+ ]);
24
+ },
25
+ };
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transactionMap = void 0;
4
+ exports.transactionMap = {
5
+ start_transaction_stmt: function (print) {
6
+ return print.spaced(["startKw", "behaviorKw", "transactionKw"]);
7
+ },
8
+ commit_transaction_stmt: function (print) {
9
+ return print.spaced(["commitKw", "transactionKw"]);
10
+ },
11
+ rollback_transaction_stmt: function (print) {
12
+ return print.spaced(["rollbackKw", "transactionKw", "savepoint"]);
13
+ },
14
+ rollback_to_savepoint: function (print) {
15
+ return print.spaced(["toKw", "savepointKw", "savepoint"]);
16
+ },
17
+ savepoint_stmt: function (print) { return print.spaced(["savepointKw", "savepoint"]); },
18
+ release_savepoint_stmt: function (print) {
19
+ return print.spaced(["releaseKw", "savepointKw", "savepoint"]);
20
+ },
21
+ };
@@ -14,19 +14,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.transformMap = void 0;
15
15
  var alias_1 = require("./alias");
16
16
  var alter_table_1 = require("./alter_table");
17
+ var analyze_1 = require("./analyze");
17
18
  var base_1 = require("./base");
19
+ var bigquery_1 = require("./bigquery");
18
20
  var constraint_1 = require("./constraint");
19
21
  var create_table_1 = require("./create_table");
20
22
  var data_type_1 = require("./data_type");
21
23
  var delete_1 = require("./delete");
22
24
  var drop_table_1 = require("./drop_table");
25
+ var explain_1 = require("./explain");
23
26
  var expr_1 = require("./expr");
24
27
  var frame_1 = require("./frame");
25
28
  var index_1 = require("./index");
26
29
  var insert_1 = require("./insert");
30
+ var procedural_language_1 = require("./procedural_language");
27
31
  var proc_clause_1 = require("./proc_clause");
28
32
  var program_1 = require("./program");
29
33
  var select_1 = require("./select");
34
+ var sqlite_1 = require("./sqlite");
35
+ var transaction_1 = require("./transaction");
36
+ var trigger_1 = require("./trigger");
37
+ var truncate_1 = require("./truncate");
30
38
  var update_1 = require("./update");
31
39
  var view_1 = require("./view");
32
- exports.transformMap = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, alias_1.aliasMap), alter_table_1.alterTableMap), base_1.baseMap), constraint_1.constraintMap), create_table_1.createTableMap), data_type_1.dataTypeMap), delete_1.deleteMap), drop_table_1.dropTableMap), expr_1.exprMap), frame_1.frameMap), index_1.indexMap), insert_1.insertMap), proc_clause_1.procClauseMap), program_1.programMap), select_1.selectMap), update_1.updateMap), view_1.viewMap);
40
+ 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({}, 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), index_1.indexMap), insert_1.insertMap), proc_clause_1.procClauseMap), procedural_language_1.proceduralLanguageMap), program_1.programMap), select_1.selectMap), sqlite_1.sqliteMap), transaction_1.transactionMap), trigger_1.triggerMap), truncate_1.truncateMap), update_1.updateMap), view_1.viewMap);
@@ -0,0 +1,39 @@
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.triggerMap = void 0;
13
+ var print_utils_1 = require("../print_utils");
14
+ exports.triggerMap = {
15
+ create_trigger_stmt: function (print, node) {
16
+ return (0, print_utils_1.join)(print_utils_1.hardline, __spreadArray(__spreadArray(__spreadArray([
17
+ print.spaced([
18
+ "createKw",
19
+ "temporaryKw",
20
+ "triggerKw",
21
+ "ifNotExistsKw",
22
+ "name",
23
+ ]),
24
+ (0, print_utils_1.group)([print("event"), (0, print_utils_1.indent)([print_utils_1.line, print.spaced(["onKw", "table"])])])
25
+ ], (node.forEachRowKw ? [print.spaced(["forEachRowKw"])] : []), true), (node.condition ? [print(["condition"])] : []), true), [
26
+ print("body"),
27
+ ], false));
28
+ },
29
+ trigger_event: function (print, node) { return [
30
+ print.spaced(["timeKw", "eventKw", "ofKw"]),
31
+ node.columns ? (0, print_utils_1.indent)([print_utils_1.line, print("columns")]) : [],
32
+ ]; },
33
+ trigger_condition: function (print) {
34
+ return (0, print_utils_1.group)([print("whenKw"), (0, print_utils_1.indent)([print_utils_1.line, print("expr")])]);
35
+ },
36
+ drop_trigger_stmt: function (print) {
37
+ return print.spaced(["dropTriggerKw", "ifExistsKw", "trigger"]);
38
+ },
39
+ };
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.truncateMap = void 0;
4
+ exports.truncateMap = {
5
+ truncate_stmt: function (print) { return print.spaced(["truncateTableKw", "table"]); },
6
+ };
@@ -3,19 +3,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.viewMap = void 0;
4
4
  var print_utils_1 = require("../print_utils");
5
5
  exports.viewMap = {
6
- create_view_stmt: function (print) { return [
7
- print.spaced([
8
- "createKw",
9
- "temporaryKw",
10
- "viewKw",
11
- "ifNotExistsKw",
12
- "name",
13
- "columns",
14
- "asKw",
15
- ]),
16
- (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.group)(print("expr"))]),
17
- ]; },
6
+ create_view_stmt: function (print, node) {
7
+ var hasOnlyAsClause = node.clauses.length === 1;
8
+ var hasManyClauses = node.clauses.length > 1;
9
+ return [
10
+ print.spaced([
11
+ "createKw",
12
+ "orReplaceKw",
13
+ "temporaryKw",
14
+ "materializedKw",
15
+ "viewKw",
16
+ "ifNotExistsKw",
17
+ "name",
18
+ "columns",
19
+ ]),
20
+ hasOnlyAsClause ? [" ", print("clauses")] : [],
21
+ hasManyClauses ? [print_utils_1.hardline, (0, print_utils_1.join)(print_utils_1.hardline, print("clauses"))] : [],
22
+ ];
23
+ },
18
24
  drop_view_stmt: function (print) {
19
- return print.spaced(["dropKw", "viewKw", "ifExistsKw", "views"]);
25
+ return print.spaced(["dropKw", "materializedKw", "viewKw", "ifExistsKw", "views"]);
20
26
  },
21
27
  };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addFinalSemicolon = void 0;
4
+ var node_utils_1 = require("../node_utils");
5
+ var utils_1 = require("../utils");
6
+ var addFinalSemicolon = function (program, originalText) {
7
+ if (!(0, node_utils_1.isEmpty)((0, utils_1.last)(program.statements))) {
8
+ var end = originalText.length;
9
+ program.statements.push({ type: "empty", range: [end, end] });
10
+ }
11
+ return program;
12
+ };
13
+ exports.addFinalSemicolon = addFinalSemicolon;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stripTrailingCommas = void 0;
4
+ var sql_parser_cst_1 = require("sql-parser-cst");
5
+ var node_utils_1 = require("../node_utils");
6
+ var utils_1 = require("../utils");
7
+ // Removes trailing comma from SELECT clauses
8
+ var stripTrailingCommas = function (cst) {
9
+ // Trailing comma is represented as an empty element at the end of column list
10
+ var removeLastEmptyColumn = (0, sql_parser_cst_1.cstVisitor)({
11
+ select_clause: function (node) {
12
+ if ((0, node_utils_1.isEmpty)((0, utils_1.last)(node.columns.items))) {
13
+ node.columns.items = node.columns.items.slice(0, -1);
14
+ }
15
+ },
16
+ });
17
+ removeLastEmptyColumn(cst);
18
+ return cst;
19
+ };
20
+ exports.stripTrailingCommas = stripTrailingCommas;
@@ -3,5 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transformCst = void 0;
4
4
  var comments_1 = require("./comments");
5
5
  var aliasAs_1 = require("./aliasAs");
6
- var transformCst = function (cst, options) { return (0, comments_1.moveCommentsToRoot)((0, aliasAs_1.processAliasAs)(cst)); };
6
+ var stripTrailingCommas_1 = require("./stripTrailingCommas");
7
+ var addFinalSemicolon_1 = require("./addFinalSemicolon");
8
+ // Note that we first perform moveCommentsToRoot transform,
9
+ // so we don't need to worry about comments interfering with other transforms
10
+ var transformCst = function (cst, originalText) {
11
+ return (0, addFinalSemicolon_1.addFinalSemicolon)((0, stripTrailingCommas_1.stripTrailingCommas)((0, aliasAs_1.processAliasAs)((0, comments_1.moveCommentsToRoot)(cst))), originalText);
12
+ };
7
13
  exports.transformCst = transformCst;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-sql-cst",
3
- "version": "0.3.0",
3
+ "version": "0.5.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.11.1"
32
+ "sql-parser-cst": "^0.12.0"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/jest": "^29.2.5",