prettier-plugin-sql-cst 0.1.1 → 0.2.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
@@ -53,26 +53,27 @@ for your editor of choice.
53
53
 
54
54
  ## Configuration
55
55
 
56
- The standard Prettier options apply. There's also one SQL-specific option:
56
+ The standard Prettier options [printWidth][], [tabWidth][], [useTabs][] apply.
57
+ There are also some SQL-specific options:
57
58
 
58
59
  | API Option | Default | Description |
59
60
  | ---------------- | :--------: | ------------------------------------------------------ |
60
- | `printWidth` | `80` | [Same as in Prettier][prettier-print-width] |
61
- | `tabWidth` | `2` | [Same as in Prettier][prettier-tab-width] |
62
- | `useTabs` | `false` | [Same as in Prettier][prettier-use-tabs] |
63
61
  | `sqlKeywordCase` | `preserve` | Converts SQL keywords to `upper` or `lower` case. |
64
62
  | `sqlAliasAs` | `preserve` | Uses AS keyword in aliases either `always` or `never`. |
65
63
 
66
64
  ## Limitations and development status
67
65
 
68
- Currently this plugin only supports subset of **SQLite** syntax.
66
+ Currently this plugin supports most of **SQLite** syntax.
67
+ (It also supports some **BigQuery**, but only the syntax that's subset of SQLite.)
69
68
  Formatting of the following SQL statements is fully implemented:
70
69
 
71
70
  - SELECT
72
71
  - UPDATE
73
72
  - INSERT
74
73
  - DELETE
75
- - CREATE TABLE
74
+ - CREATE / DROP / ALTER TABLE
75
+ - CREATE / DROP VIEW
76
+ - CREATE / DROP INDEX
76
77
 
77
78
  Unlike Prettier for JavaScript, this plugin currently preserves all the syntax elements.
78
79
  For example it does not add/remove paratheses or modify the case of keywords (by default).
@@ -91,8 +92,8 @@ The overall goal is to have no options to configure different styles,
91
92
  but rather to develop one opinionated style of formatting SQL.
92
93
 
93
94
  [prettier]: https://prettier.io/
94
- [prettier-print-width]: https://prettier.io/docs/en/options.html#print-width
95
- [prettier-tab-width]: https://prettier.io/docs/en/options.html#tab-width
96
- [prettier-use-tabs]: https://prettier.io/docs/en/options.html#tabs
95
+ [printWidth]: https://prettier.io/docs/en/options.html#print-width
96
+ [tabWidth]: https://prettier.io/docs/en/options.html#tab-width
97
+ [useTabs]: https://prettier.io/docs/en/options.html#tabs
97
98
  [sql-parser-cst]: https://github.com/nene/sql-parser-cst
98
99
  [wadler-prettier]: http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf
package/dist/index.js CHANGED
@@ -4,31 +4,37 @@ exports.printers = exports.parsers = exports.languages = exports.options = void
4
4
  var sql_parser_cst_1 = require("sql-parser-cst");
5
5
  var printSql_1 = require("./printSql");
6
6
  var utils_1 = require("./utils");
7
- var comments_1 = require("./comments");
8
- var aliasAs_1 = require("./aliasAs");
7
+ var transformCst_1 = require("./transform/transformCst");
9
8
  var options_1 = require("./options");
10
9
  Object.defineProperty(exports, "options", { enumerable: true, get: function () { return options_1.options; } });
11
10
  exports.languages = [
12
11
  {
13
- extensions: [".sql"],
14
- name: "SQL",
15
- parsers: ["sql-parser-cst"],
12
+ extensions: [".sql", ".sqlite"],
13
+ name: "SQLite SQL",
14
+ parsers: ["sql-parser-cst-sqlite"],
15
+ },
16
+ {
17
+ extensions: [".bigquery"],
18
+ name: "BigQuery SQL",
19
+ parsers: ["sql-parser-cst-bigquery"],
16
20
  },
17
21
  ];
18
- exports.parsers = {
19
- "sql-parser-cst": {
20
- parse: function (text, parsers, options) {
21
- return (0, comments_1.moveCommentsToRoot)((0, aliasAs_1.processAliasAs)((0, sql_parser_cst_1.parse)(text, {
22
- dialect: "sqlite",
23
- includeRange: true,
24
- includeComments: true,
25
- filename: options.filepath,
26
- }), options));
27
- },
28
- astFormat: "sql-cst",
29
- locStart: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[0]; },
30
- locEnd: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[1]; },
22
+ var createParser = function (dialect) { return ({
23
+ parse: function (text, parsers, options) {
24
+ return (0, transformCst_1.transformCst)((0, sql_parser_cst_1.parse)(text, {
25
+ dialect: dialect,
26
+ includeRange: true,
27
+ includeComments: true,
28
+ filename: options.filepath,
29
+ }), options);
31
30
  },
31
+ astFormat: "sql-cst",
32
+ locStart: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[0]; },
33
+ locEnd: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[1]; },
34
+ }); };
35
+ exports.parsers = {
36
+ "sql-parser-cst-sqlite": createParser("sqlite"),
37
+ "sql-parser-cst-bigquery": createParser("bigquery"),
32
38
  };
33
39
  exports.printers = {
34
40
  "sql-cst": {
package/dist/printSql.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.printSql = void 0;
4
4
  var utils_1 = require("./utils");
5
- var syntax_1 = require("./syntax");
5
+ var transformMap_1 = require("./syntax/transformMap");
6
6
  var print_utils_1 = require("./print_utils");
7
7
  function printSql(path, options, oldPrint) {
8
8
  return printNode(path, options, createPrintFn(path, oldPrint));
@@ -58,7 +58,7 @@ function printNode(path, options, print) {
58
58
  if (!(node === null || node === void 0 ? void 0 : node.type)) {
59
59
  throw new Error("No type field on node: ".concat(JSON.stringify(node)));
60
60
  }
61
- var fn = syntax_1.transformMap[node.type];
61
+ var fn = transformMap_1.transformMap[node.type];
62
62
  if (!fn) {
63
63
  throw new Error("Unexpected node type: ".concat(node.type));
64
64
  }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.alterTableMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.alterTableMap = {
6
+ alter_table_stmt: function (print) { return [
7
+ print.spaced(["alterTableKw", "table"]),
8
+ print_utils_1.hardline,
9
+ print("actions"),
10
+ ]; },
11
+ alter_action_rename_table: function (print) { return print.spaced(["renameKw", "newName"]); },
12
+ alter_action_rename_column: function (print) {
13
+ return print.spaced(["renameKw", "oldName", "toKw", "newName"]);
14
+ },
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
+ };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dropTableMap = void 0;
4
+ exports.dropTableMap = {
5
+ drop_table_stmt: function (print) {
6
+ return print.spaced(["dropKw", "tableKw", "ifExistsKw", "tables"]);
7
+ },
8
+ };
@@ -44,6 +44,7 @@ exports.exprMap = {
44
44
  },
45
45
  case_else: function (print) { return print.spaced(["elseKw", "result"]); },
46
46
  member_expr: function (print) { return [print("object"), ".", print("property")]; },
47
+ bigquery_quoted_member_expr: function (print) { return ["`", print("expr"), "`"]; },
47
48
  func_call: function (print) {
48
49
  var fnCall = print(["name", "args"]);
49
50
  var extras = print(["filter", "over"]);
@@ -1,28 +1,32 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
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];
8
7
  }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
12
10
  };
13
11
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.transformMap = void 0;
15
- var alias_1 = require("./alias");
16
- var base_1 = require("./base");
17
- var constraint_1 = require("./constraint");
18
- var create_table_1 = require("./create_table");
19
- var data_type_1 = require("./data_type");
20
- var delete_1 = require("./delete");
21
- var expr_1 = require("./expr");
22
- var frame_1 = require("./frame");
23
- var insert_1 = require("./insert");
24
- var proc_clause_1 = require("./proc_clause");
25
- var program_1 = require("./program");
26
- var select_1 = require("./select");
27
- var update_1 = require("./update");
28
- exports.transformMap = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, alias_1.aliasMap), base_1.baseMap), constraint_1.constraintMap), create_table_1.createTableMap), data_type_1.dataTypeMap), delete_1.deleteMap), expr_1.exprMap), frame_1.frameMap), insert_1.insertMap), proc_clause_1.procClauseMap), program_1.programMap), select_1.selectMap), update_1.updateMap);
12
+ exports.indexMap = void 0;
13
+ var print_utils_1 = require("../print_utils");
14
+ exports.indexMap = {
15
+ create_index_stmt: function (print, node) {
16
+ return (0, print_utils_1.join)(print_utils_1.hardline, __spreadArray([
17
+ print.spaced([
18
+ "createKw",
19
+ "indexTypeKw",
20
+ "indexKw",
21
+ "ifNotExistsKw",
22
+ "name",
23
+ "onKw",
24
+ "table",
25
+ "columns",
26
+ ])
27
+ ], (node.clauses.length > 0 ? [(0, print_utils_1.join)(print_utils_1.hardline, print("clauses"))] : []), true));
28
+ },
29
+ drop_index_stmt: function (print) {
30
+ return print.spaced(["dropKw", "indexKw", "ifExistsKw", "indexes"]);
31
+ },
32
+ };
@@ -12,7 +12,7 @@ var printLineWith = function (print) {
12
12
  return print(childPath);
13
13
  }
14
14
  else if (i < all.length - 1 || node.type !== "empty") {
15
- return [";", print_utils_1.hardline, print(childPath)];
15
+ return [";", print_utils_1.hardline, print_utils_1.hardline, print(childPath)];
16
16
  }
17
17
  else {
18
18
  return [";", print(childPath)];
@@ -28,6 +28,8 @@ exports.selectMap = {
28
28
  (0, print_utils_1.indent)([print_utils_1.line, print("columns")]),
29
29
  ]);
30
30
  },
31
+ except_columns: function (print) { return print.spaced(["expr", "exceptKw", "columns"]); },
32
+ replace_columns: function (print) { return print.spaced(["expr", "replaceKw", "columns"]); },
31
33
  from_clause: function (print) {
32
34
  return (0, print_utils_1.group)([print("fromKw"), (0, print_utils_1.indent)([print_utils_1.line, print("expr")])]);
33
35
  },
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.transformMap = void 0;
15
+ var alias_1 = require("./alias");
16
+ var alter_table_1 = require("./alter_table");
17
+ var base_1 = require("./base");
18
+ var constraint_1 = require("./constraint");
19
+ var create_table_1 = require("./create_table");
20
+ var data_type_1 = require("./data_type");
21
+ var delete_1 = require("./delete");
22
+ var drop_table_1 = require("./drop_table");
23
+ var expr_1 = require("./expr");
24
+ var frame_1 = require("./frame");
25
+ var index_1 = require("./index");
26
+ var insert_1 = require("./insert");
27
+ var proc_clause_1 = require("./proc_clause");
28
+ var program_1 = require("./program");
29
+ var select_1 = require("./select");
30
+ var update_1 = require("./update");
31
+ 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);
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.viewMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
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
+ ]; },
18
+ drop_view_stmt: function (print) {
19
+ return print.spaced(["dropKw", "viewKw", "ifExistsKw", "views"]);
20
+ },
21
+ };
File without changes
@@ -21,7 +21,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.moveCommentsToRoot = void 0;
24
- var visitAllNodes_1 = require("./visitAllNodes");
24
+ var visitAllNodes_1 = require("../visitAllNodes");
25
25
  /**
26
26
  * Gathers comments from .leading and .trailing fields of all nodes
27
27
  * into a single .comments field in root Program node.
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transformCst = void 0;
4
+ var comments_1 = require("./comments");
5
+ var aliasAs_1 = require("./aliasAs");
6
+ var transformCst = function (cst, options) { return (0, comments_1.moveCommentsToRoot)((0, aliasAs_1.processAliasAs)(cst, options)); };
7
+ exports.transformCst = transformCst;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-sql-cst",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Prettier plugin for SQL",
5
5
  "author": "Rene Saarsoo <nene@triin.net>",
6
6
  "license": "GPL-3.0-or-later",