prettier-plugin-sql-cst 0.1.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 ADDED
@@ -0,0 +1,45 @@
1
+ # Prettier plugin SQL-CST
2
+
3
+ A [Prettier][] plugin for SQL that uses [sql-parser-cst][] and the
4
+ actual [Prettier formatting algorithm][wadler-prettier].
5
+
6
+ Like Prettier for JavaScript,
7
+ this plugin formats SQL expressions differently depending on their length.
8
+ A short SQL query will be formatted on a single line:
9
+
10
+ ```sql
11
+ SELECT a, b, c FROM tbl WHERE x > 10
12
+ ```
13
+
14
+ A longer query, will get each clause printed on a separate line:
15
+
16
+ ```sql
17
+ SELECT id, client.name, client.priority
18
+ FROM client
19
+ WHERE client.id IN (12, 18, 121)
20
+ ```
21
+
22
+ An even longer one gets the contents of each clause indented:
23
+
24
+ ```sql
25
+ SELECT
26
+ client.id,
27
+ client.name AS client_name,
28
+ organization.name AS org_name,
29
+ count(order.id) AS nr_of_orders
30
+ FROM
31
+ client
32
+ LEFT JOIN organization ON client.organization_id = organization.id
33
+ LEFT JOIN order ON order.client_id = client.id
34
+ WHERE
35
+ client.status = 'active'
36
+ AND client.id IN (28, 214, 457)
37
+ AND order.status IN ('active', 'pending', 'processing')
38
+ GROUP BY client.id
39
+ ORDER BY client.name
40
+ LIMIT 100
41
+ ```
42
+
43
+ [prettier]: https://prettier.io/
44
+ [sql-parser-cst]: https://github.com/nene/sql-parser-cst
45
+ [wadler-prettier]: http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,43 @@
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
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
+ if (ar || !(i in from)) {
16
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
+ ar[i] = from[i];
18
+ }
19
+ }
20
+ return to.concat(ar || Array.prototype.slice.call(from));
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.moveCommentsToRoot = void 0;
24
+ var visitAllNodes_1 = require("./visitAllNodes");
25
+ /**
26
+ * Gathers comments from .leading and .trailing fields of all nodes
27
+ * into a single .comments field in root Program node.
28
+ *
29
+ * Deletes the .leading and .trailing fields of all nodes.
30
+ */
31
+ var moveCommentsToRoot = function (cst) {
32
+ return __assign(__assign({}, cst), { comments: extractComments(cst) });
33
+ };
34
+ exports.moveCommentsToRoot = moveCommentsToRoot;
35
+ var extractComments = function (cst) {
36
+ var comments = [];
37
+ (0, visitAllNodes_1.visitAllNodes)(cst, function (node) {
38
+ comments.push.apply(comments, __spreadArray(__spreadArray([], (node.leading || []), false), (node.trailing || []), false));
39
+ delete node.leading;
40
+ delete node.trailing;
41
+ });
42
+ return comments;
43
+ };
package/dist/index.js ADDED
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.printers = exports.parsers = exports.languages = exports.options = void 0;
4
+ var sql_parser_cst_1 = require("sql-parser-cst");
5
+ var printSql_1 = require("./printSql");
6
+ var utils_1 = require("./utils");
7
+ var comments_1 = require("./comments");
8
+ var options_1 = require("./options");
9
+ Object.defineProperty(exports, "options", { enumerable: true, get: function () { return options_1.options; } });
10
+ exports.languages = [
11
+ {
12
+ extensions: [".sql"],
13
+ name: "SQL",
14
+ parsers: ["sql-parser-cst"],
15
+ },
16
+ ];
17
+ exports.parsers = {
18
+ "sql-parser-cst": {
19
+ parse: function (text, parsers, options) {
20
+ return (0, comments_1.moveCommentsToRoot)((0, sql_parser_cst_1.parse)(text, {
21
+ dialect: "sqlite",
22
+ includeRange: true,
23
+ includeComments: true,
24
+ filename: options.filepath,
25
+ }));
26
+ },
27
+ astFormat: "sql-cst",
28
+ locStart: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[0]; },
29
+ locEnd: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[1]; },
30
+ },
31
+ };
32
+ exports.printers = {
33
+ "sql-cst": {
34
+ print: printSql_1.printSql,
35
+ printComment: function (path) {
36
+ return path.getValue().text;
37
+ },
38
+ canAttachComment: utils_1.isNode,
39
+ isBlockComment: function (node) { return node.type === "block_comment"; },
40
+ },
41
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isKeyword = exports.isCreateTableStmt = exports.isFuncCall = exports.isValuesClause = void 0;
4
+ var utils_1 = require("./utils");
5
+ var is = function (type) {
6
+ return function (node) {
7
+ return (0, utils_1.isObject)(node) && node.type === type;
8
+ };
9
+ };
10
+ exports.isValuesClause = is("values_clause");
11
+ exports.isFuncCall = is("func_call");
12
+ exports.isCreateTableStmt = is("create_table_stmt");
13
+ exports.isKeyword = is("keyword");
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.options = void 0;
4
+ exports.options = {
5
+ sqlKeywordCase: {
6
+ type: "choice",
7
+ category: "SQL",
8
+ since: "0.1.0",
9
+ default: "preserve",
10
+ description: "Enforces upper/lower case for SQL keywords",
11
+ choices: [
12
+ {
13
+ value: "preserve",
14
+ description: "preserves the existing case",
15
+ },
16
+ { value: "upper", description: "forces all keywords to uppercase" },
17
+ { value: "lower", description: "forces all keywords to lowercase" },
18
+ ],
19
+ },
20
+ };
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.printSql = void 0;
4
+ var utils_1 = require("./utils");
5
+ var syntax_1 = require("./syntax");
6
+ var print_utils_1 = require("./print_utils");
7
+ function printSql(path, options, oldPrint) {
8
+ return printNode(path, options, createPrintFn(path, oldPrint));
9
+ }
10
+ exports.printSql = printSql;
11
+ var cachedPrintFn;
12
+ var cachedPath;
13
+ function createPrintFn(path, oldPrint) {
14
+ // The path parameter will reference the same AstPath instance
15
+ // during the whole printing cycle.
16
+ // So we can keep using the same print function as long as
17
+ // the path stays the same.
18
+ if (cachedPath === path) {
19
+ return cachedPrintFn;
20
+ }
21
+ cachedPath = path;
22
+ cachedPrintFn = (function (selector) {
23
+ if ((0, utils_1.isArray)(selector)) {
24
+ var node_1 = path.getValue();
25
+ return selector.filter(function (sel) { return (0, utils_1.isDefined)(node_1[sel]); }).map(oldPrint);
26
+ }
27
+ else {
28
+ return oldPrint(selector);
29
+ }
30
+ });
31
+ cachedPrintFn.spaced = function (selector) {
32
+ var node = path.getValue();
33
+ var docs = (0, utils_1.arrayWrap)(selector)
34
+ .filter(function (sel) { return (0, utils_1.isDefined)(node[sel]); })
35
+ .map(function (sel) { return [sel, oldPrint(sel)]; })
36
+ // skip empty arrays
37
+ .filter(function (_a) {
38
+ var sel = _a[0], doc = _a[1];
39
+ return !(0, utils_1.isEmptyArray)(doc);
40
+ })
41
+ // only join with spaces when input to print was Node[]
42
+ .map(function (_a) {
43
+ var sel = _a[0], doc = _a[1];
44
+ return ((0, utils_1.isArray)(node[sel]) ? (0, print_utils_1.join)(" ", doc) : doc);
45
+ });
46
+ return docs.length > 0 ? [(0, print_utils_1.join)(" ", docs)] : [];
47
+ };
48
+ return cachedPrintFn;
49
+ }
50
+ function printNode(path, options, print) {
51
+ var node = path.getValue();
52
+ if ((0, utils_1.isArray)(node)) {
53
+ return path.map(print);
54
+ }
55
+ if ((0, utils_1.isString)(node)) {
56
+ return node;
57
+ }
58
+ if (!(node === null || node === void 0 ? void 0 : node.type)) {
59
+ throw new Error("No type field on node: ".concat(JSON.stringify(node)));
60
+ }
61
+ var fn = syntax_1.transformMap[node.type];
62
+ if (!fn) {
63
+ throw new Error("Unexpected node type: ".concat(node.type));
64
+ }
65
+ return fn(print, node, path, options);
66
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.containsNewline = exports.join = exports.lineSuffix = exports.group = exports.indent = exports.softline = exports.hardline = exports.line = void 0;
5
+ var prettier_1 = require("prettier");
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;
8
+ var origJoin = prettier_1.doc.builders.join;
9
+ /** Improved join() that also accepts a non-array docs parameter */
10
+ var join = function (sep, docs) {
11
+ return (0, utils_1.isArray)(docs) ? origJoin(sep, docs) : docs;
12
+ };
13
+ exports.join = join;
14
+ /** True when the Node contains a newline in original source code */
15
+ var containsNewline = function (node, opts) {
16
+ if (!node.range) {
17
+ throw new Error("containsNewline() expected a Node with range info");
18
+ }
19
+ return prettier_1.util.hasNewlineInRange(opts.originalText, node.range[0], node.range[1]);
20
+ };
21
+ exports.containsNewline = containsNewline;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.aliasMap = void 0;
4
+ exports.aliasMap = {
5
+ alias: function (print) { return print.spaced(["expr", "asKw", "alias"]); },
6
+ };
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.baseMap = void 0;
4
+ exports.baseMap = {
5
+ keyword: function (print, node, path, options) {
6
+ switch (options.sqlKeywordCase) {
7
+ case "preserve":
8
+ return path.getValue().text;
9
+ case "upper":
10
+ return path.getValue().text.toUpperCase();
11
+ case "lower":
12
+ return path.getValue().text.toLowerCase();
13
+ }
14
+ },
15
+ all_columns: function () { return "*"; },
16
+ empty: function () { return []; },
17
+ };
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.constraintMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.constraintMap = {
6
+ constraint: function (print, node) {
7
+ if (node.name) {
8
+ return (0, print_utils_1.group)([
9
+ print("name"),
10
+ (0, print_utils_1.indent)([print_utils_1.line, printUnnamedConstraint(print, node)]),
11
+ ]);
12
+ }
13
+ else {
14
+ return printUnnamedConstraint(print, node);
15
+ }
16
+ },
17
+ constraint_name: function (print) { return print.spaced(["constraintKw", "name"]); },
18
+ constraint_deferrable: function (print) {
19
+ return print.spaced(["deferrableKw", "initiallyKw"]);
20
+ },
21
+ constraint_not_null: function (print) {
22
+ return (0, print_utils_1.group)(print.spaced(["notNullKw", "onConflict"]));
23
+ },
24
+ constraint_default: function (print) { return (0, print_utils_1.group)(print.spaced(["defaultKw", "expr"])); },
25
+ constraint_primary_key: function (print) {
26
+ return (0, print_utils_1.group)(print.spaced(["primaryKeyKw", "orderKw", "columns", "onConflict"]));
27
+ },
28
+ constraint_unique: function (print) {
29
+ return (0, print_utils_1.group)(print.spaced(["uniqueKw", "columns", "onConflict"]));
30
+ },
31
+ constraint_check: function (print) { return (0, print_utils_1.group)(print.spaced(["checkKw", "expr"])); },
32
+ constraint_collate: function (print) {
33
+ return (0, print_utils_1.group)(print.spaced(["collateKw", "collation"]));
34
+ },
35
+ constraint_foreign_key: function (print) {
36
+ return (0, print_utils_1.group)(print.spaced(["foreignKeyKw", "columns", "references"]));
37
+ },
38
+ references_specification: function (print, node) {
39
+ var baseDoc = print.spaced(["referencesKw", "table", "columns"]);
40
+ if (node.options.length > 0) {
41
+ return (0, print_utils_1.group)([
42
+ baseDoc,
43
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.join)(print_utils_1.hardline, print("options"))]),
44
+ ]);
45
+ }
46
+ else {
47
+ return baseDoc;
48
+ }
49
+ },
50
+ referential_action: function (print) { return print.spaced(["onKw", "eventKw", "actionKw"]); },
51
+ referential_match: function (print) { return print.spaced(["matchKw", "typeKw"]); },
52
+ constraint_generated: function (print) {
53
+ return print.spaced(["generatedKw", "asKw", "expr", "storageKw"]);
54
+ },
55
+ constraint_auto_increment: function (print) { return print("autoIncrementKw"); },
56
+ on_conflict_clause: function (print) { return print.spaced(["onConflictKw", "resolutionKw"]); },
57
+ };
58
+ var printUnnamedConstraint = function (print, node) {
59
+ if (node.deferrable) {
60
+ return (0, print_utils_1.group)([
61
+ print("constraint"),
62
+ (0, print_utils_1.indent)([print_utils_1.hardline, print("deferrable")]),
63
+ ]);
64
+ }
65
+ else {
66
+ return print("constraint");
67
+ }
68
+ };
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTableMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ 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
+ ]; },
18
+ column_definition: function (print) {
19
+ return print.spaced(["name", "dataType", "constraints"]);
20
+ },
21
+ table_option: function (print) { return print.spaced(["name"]); },
22
+ };
@@ -0,0 +1,15 @@
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.dataTypeMap = void 0;
13
+ exports.dataTypeMap = {
14
+ data_type: function (print) { return __spreadArray([print.spaced("nameKw")], print(["params"]), true); },
15
+ };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deleteMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.deleteMap = {
6
+ delete_stmt: function (print) { return (0, print_utils_1.join)(print_utils_1.hardline, print("clauses")); },
7
+ delete_clause: function (print) { return print.spaced(["deleteKw", "fromKw", "table"]); },
8
+ };
@@ -0,0 +1,72 @@
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.exprMap = void 0;
13
+ var print_utils_1 = require("../print_utils");
14
+ var node_utils_1 = require("../node_utils");
15
+ exports.exprMap = {
16
+ list_expr: function (print, node, path) {
17
+ var parent = path.getParentNode();
18
+ return (0, print_utils_1.join)([",", (0, node_utils_1.isValuesClause)(parent) ? print_utils_1.hardline : print_utils_1.line], print("items").map(function (it) { return (0, print_utils_1.group)(it); }));
19
+ },
20
+ paren_expr: function (print, node, path) {
21
+ var parent = path.getParentNode();
22
+ var lineStyle = (0, node_utils_1.isCreateTableStmt)(parent) ? print_utils_1.hardline : print_utils_1.softline;
23
+ return (0, print_utils_1.group)(["(", (0, print_utils_1.indent)([lineStyle, print("expr")]), lineStyle, ")"]);
24
+ },
25
+ binary_expr: function (print, node) {
26
+ if ((0, node_utils_1.isKeyword)(node.operator) && isBooleanOp(node.operator)) {
27
+ return (0, print_utils_1.join)(print_utils_1.line, [print("left"), print.spaced(["operator", "right"])]);
28
+ }
29
+ return print.spaced(["left", "operator", "right"]);
30
+ },
31
+ prefix_op_expr: function (print) { return print.spaced(["operator", "expr"]); },
32
+ postfix_op_expr: function (print) { return print.spaced(["expr", "operator"]); },
33
+ between_expr: function (print) {
34
+ return print.spaced(["left", "betweenKw", "begin", "andKw", "end"]);
35
+ },
36
+ case_expr: function (print) { return [
37
+ print.spaced(["caseKw", "expr"]),
38
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.join)(print_utils_1.hardline, print("clauses"))]),
39
+ print_utils_1.hardline,
40
+ print("endKw"),
41
+ ]; },
42
+ case_when: function (print) {
43
+ return print.spaced(["whenKw", "condition", "thenKw", "result"]);
44
+ },
45
+ case_else: function (print) { return print.spaced(["elseKw", "result"]); },
46
+ member_expr: function (print) { return [print("object"), ".", print("property")]; },
47
+ func_call: function (print) {
48
+ var fnCall = print(["name", "args"]);
49
+ var extras = print(["filter", "over"]);
50
+ if (extras.length > 1) {
51
+ return (0, print_utils_1.group)([fnCall, (0, print_utils_1.indent)([print_utils_1.line, (0, print_utils_1.join)(print_utils_1.line, extras)])]);
52
+ }
53
+ else {
54
+ return (0, print_utils_1.group)((0, print_utils_1.join)(" ", __spreadArray([fnCall], extras, true)));
55
+ }
56
+ },
57
+ func_args: function (print) { return print.spaced(["distinctKw", "args"]); },
58
+ filter_arg: function (print) { return print.spaced(["filterKw", "where"]); },
59
+ over_arg: function (print) { return print.spaced(["overKw", "window"]); },
60
+ cast_expr: function (print) { return print(["castKw", "args"]); },
61
+ cast_arg: function (print) { return print.spaced(["expr", "asKw", "dataType"]); },
62
+ raise_expr: function (print) { return print(["raiseKw", "args"]); },
63
+ number_literal: function (print) { return print("text"); },
64
+ boolean_literal: function (print) { return print("valueKw"); },
65
+ string_literal: function (print) { return print("text"); },
66
+ null_literal: function (print) { return print("nullKw"); },
67
+ identifier: function (print) { return print("text"); },
68
+ };
69
+ var isBooleanOp = function (_a) {
70
+ var name = _a.name;
71
+ return name === "AND" || name === "OR";
72
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.frameMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.frameMap = {
6
+ frame_clause: function (print, node) { return [
7
+ print.spaced(["unitKw", "extent"]),
8
+ node.exclusion ? [print_utils_1.hardline, print("exclusion")] : [],
9
+ ]; },
10
+ frame_bound_current_row: function (print) { return print.spaced("currentRowKw"); },
11
+ frame_bound_preceding: function (print) { return print.spaced(["expr", "precedingKw"]); },
12
+ frame_bound_following: function (print) { return print.spaced(["expr", "followingKw"]); },
13
+ frame_unbounded: function (print) { return print.spaced("unboundedKw"); },
14
+ frame_between: function (print) {
15
+ return print.spaced(["betweenKw", "begin", "andKw", "end"]);
16
+ },
17
+ frame_exclusion: function (print) { return print.spaced(["excludeKw", "kindKw"]); },
18
+ };
@@ -0,0 +1,28 @@
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 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);
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.insertMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.insertMap = {
6
+ insert_stmt: function (print) { return (0, print_utils_1.join)(print_utils_1.hardline, print("clauses")); },
7
+ insert_clause: function (print, node) {
8
+ return (0, print_utils_1.group)([
9
+ print.spaced(["insertKw", "orAction", "intoKw", "table"]),
10
+ node.columns ? (0, print_utils_1.indent)([print_utils_1.hardline, print("columns")]) : [],
11
+ ]);
12
+ },
13
+ values_clause: function (print) {
14
+ return (0, print_utils_1.group)([print("valuesKw"), (0, print_utils_1.indent)([print_utils_1.hardline, print("values")])]);
15
+ },
16
+ or_alternate_action: function (print) { return print.spaced(["orKw", "actionKw"]); },
17
+ default_values: function (print) { return print.spaced("defaultValuesKw"); },
18
+ upsert_clause: function (print) {
19
+ return print.spaced(["onConflictKw", "columns", "where", "doKw", "action"]);
20
+ },
21
+ upsert_action_nothing: function (print) { return print("nothingKw"); },
22
+ upsert_action_update: function (print) { return [
23
+ print("updateKw"),
24
+ (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.join)(print_utils_1.hardline, print(["set", "where"]))]),
25
+ ]; },
26
+ };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.procClauseMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.procClauseMap = {
6
+ as_clause: function (print) { return [print("asKw"), (0, print_utils_1.indent)([print_utils_1.hardline, print("expr")])]; },
7
+ };
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.programMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.programMap = {
6
+ program: function (print, node, path) { return path.map(printLineWith(print), "statements"); },
7
+ };
8
+ var printLineWith = function (print) {
9
+ return function (childPath, i, all) {
10
+ var node = childPath.getValue();
11
+ if (i === 0) {
12
+ return print(childPath);
13
+ }
14
+ else if (i < all.length - 1 || node.type !== "empty") {
15
+ return [";", print_utils_1.hardline, print(childPath)];
16
+ }
17
+ else {
18
+ return [";", print(childPath)];
19
+ }
20
+ };
21
+ };
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.selectMap = void 0;
4
+ var utils_1 = require("../utils");
5
+ var print_utils_1 = require("../print_utils");
6
+ exports.selectMap = {
7
+ compound_select_stmt: function (print) {
8
+ return (0, print_utils_1.join)(print_utils_1.hardline, [print("left"), print.spaced("operator"), print("right")]);
9
+ },
10
+ select_stmt: function (print, node, path, opts) {
11
+ var lineType = (0, print_utils_1.containsNewline)(node, opts) ? print_utils_1.hardline : print_utils_1.line;
12
+ return (0, print_utils_1.group)((0, print_utils_1.join)(lineType, print("clauses")));
13
+ },
14
+ with_clause: function (print) {
15
+ return (0, print_utils_1.group)([
16
+ print.spaced(["withKw", "recursiveKw"]),
17
+ (0, print_utils_1.indent)([print_utils_1.line, print("tables")]),
18
+ ]);
19
+ },
20
+ common_table_expression: function (print) { return [
21
+ print(["table", "columns"]),
22
+ " ",
23
+ print.spaced(["asKw", "materializedKw", "expr"]),
24
+ ]; },
25
+ select_clause: function (print) {
26
+ return (0, print_utils_1.group)([
27
+ print.spaced(["selectKw", "distinctKw"]),
28
+ (0, print_utils_1.indent)([print_utils_1.line, print("columns")]),
29
+ ]);
30
+ },
31
+ from_clause: function (print) {
32
+ return (0, print_utils_1.group)([print("fromKw"), (0, print_utils_1.indent)([print_utils_1.line, print("expr")])]);
33
+ },
34
+ join_expr: function (print, node) {
35
+ if (node.operator === ",") {
36
+ return (0, print_utils_1.join)([",", print_utils_1.hardline], print(["left", "right"]));
37
+ }
38
+ else {
39
+ return [
40
+ print("left"),
41
+ print_utils_1.hardline,
42
+ (0, print_utils_1.group)((0, print_utils_1.join)(" ", [
43
+ print.spaced("operator"),
44
+ [
45
+ print("right"),
46
+ node.specification
47
+ ? (0, print_utils_1.indent)([print_utils_1.line, print(["specification"])])
48
+ : [],
49
+ ],
50
+ ])),
51
+ ];
52
+ }
53
+ },
54
+ join_on_specification: function (print) { return (0, print_utils_1.group)(print.spaced(["onKw", "expr"])); },
55
+ join_using_specification: function (print) { return (0, print_utils_1.group)(print.spaced(["usingKw", "expr"])); },
56
+ indexed_table: function (print) { return print.spaced(["table", "indexedByKw", "index"]); },
57
+ not_indexed_table: function (print) { return print.spaced(["table", "notIndexedKw"]); },
58
+ where_clause: function (print) {
59
+ return (0, print_utils_1.group)([print("whereKw"), (0, print_utils_1.indent)([print_utils_1.line, print("expr")])]);
60
+ },
61
+ order_by_clause: function (print) {
62
+ return (0, print_utils_1.group)([print.spaced("orderByKw"), (0, print_utils_1.indent)([print_utils_1.line, print("specifications")])]);
63
+ },
64
+ group_by_clause: function (print) {
65
+ return (0, print_utils_1.group)([print.spaced("groupByKw"), (0, print_utils_1.indent)([print_utils_1.line, print("columns")])]);
66
+ },
67
+ partition_by_clause: function (print) {
68
+ return (0, print_utils_1.group)([
69
+ print.spaced("partitionByKw"),
70
+ (0, print_utils_1.indent)([print_utils_1.line, print("specifications")]),
71
+ ]);
72
+ },
73
+ having_clause: function (print) {
74
+ return (0, print_utils_1.group)([print("havingKw"), (0, print_utils_1.indent)([print_utils_1.line, print("expr")])]);
75
+ },
76
+ returning_clause: function (print) {
77
+ return (0, print_utils_1.group)([print("returningKw"), (0, print_utils_1.indent)([print_utils_1.line, print("columns")])]);
78
+ },
79
+ limit_clause: function (print, node) {
80
+ return (0, print_utils_1.group)([print("limitKw"), (0, print_utils_1.indent)([print_utils_1.line, printLimitValues(print, node)])]);
81
+ },
82
+ sort_specification: function (print) {
83
+ return print.spaced(["expr", "orderKw", "nullHandlingKw"]);
84
+ },
85
+ window_clause: function (print, node) {
86
+ var lineType = node.namedWindows.items.length > 1 ? print_utils_1.hardline : print_utils_1.line;
87
+ return (0, print_utils_1.group)([
88
+ print("windowKw"),
89
+ (0, print_utils_1.indent)([lineType, print("namedWindows")]),
90
+ ]);
91
+ },
92
+ named_window: function (print) { return print.spaced(["name", "asKw", "window"]); },
93
+ window_definition: function (print, node) {
94
+ var itemCount = [
95
+ node.baseWindowName,
96
+ node.partitionBy,
97
+ node.orderBy,
98
+ node.frame,
99
+ ].filter(utils_1.isDefined).length;
100
+ var lineType = itemCount > 1 ? print_utils_1.hardline : print_utils_1.line;
101
+ return (0, print_utils_1.group)((0, print_utils_1.join)(lineType, print(["baseWindowName", "partitionBy", "orderBy", "frame"])));
102
+ },
103
+ };
104
+ var printLimitValues = function (print, node) {
105
+ if (node.offsetKw) {
106
+ return print.spaced(["count", "offsetKw", "offset"]);
107
+ }
108
+ else if (node.offset) {
109
+ return (0, print_utils_1.join)([",", " "], print(["offset", "count"]));
110
+ }
111
+ else {
112
+ return print("count");
113
+ }
114
+ };
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.updateMap = void 0;
4
+ var print_utils_1 = require("../print_utils");
5
+ exports.updateMap = {
6
+ update_stmt: function (print) { return (0, print_utils_1.join)(print_utils_1.hardline, print("clauses")); },
7
+ update_clause: function (print) {
8
+ return print.spaced(["updateKw", "options", "orAction", "tables"]);
9
+ },
10
+ set_clause: function (print, node) {
11
+ if (node.assignments.items.length > 1) {
12
+ return [print("setKw"), (0, print_utils_1.indent)([print_utils_1.hardline, print("assignments")])];
13
+ }
14
+ else {
15
+ return print.spaced(["setKw", "assignments"]);
16
+ }
17
+ },
18
+ column_assignment: function (print) {
19
+ return (0, print_utils_1.join)([" ", "=", " "], print(["column", "expr"]));
20
+ },
21
+ };
package/dist/utils.js ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.arrayWrap = exports.isNode = exports.isEmptyArray = exports.isArray = exports.isObject = exports.isString = exports.isDefined = void 0;
4
+ var isDefined = function (value) {
5
+ return value !== undefined;
6
+ };
7
+ exports.isDefined = isDefined;
8
+ var isString = function (value) {
9
+ return typeof value === "string";
10
+ };
11
+ exports.isString = isString;
12
+ var isObject = function (x) {
13
+ return typeof x === "object" && x !== null && !(x instanceof Array);
14
+ };
15
+ exports.isObject = isObject;
16
+ var isArray = function (x) { return Array.isArray(x); };
17
+ exports.isArray = isArray;
18
+ var isEmptyArray = function (x) { return (0, exports.isArray)(x) && x.length === 0; };
19
+ exports.isEmptyArray = isEmptyArray;
20
+ var isNode = function (value) {
21
+ return (0, exports.isObject)(value) && (0, exports.isString)(value.type) && !isCommentType(value.type);
22
+ };
23
+ exports.isNode = isNode;
24
+ var isCommentType = function (type) {
25
+ return type === "block_comment" || type === "line_comment";
26
+ };
27
+ var arrayWrap = function (x) { return ((0, exports.isArray)(x) ? x : [x]); };
28
+ exports.arrayWrap = arrayWrap;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.visitAllNodes = void 0;
4
+ var utils_1 = require("./utils");
5
+ /* Executes a function with all nodes in syntax tree */
6
+ var visitAllNodes = function (node, visit) {
7
+ visit(node);
8
+ // Visit all children
9
+ for (var _i = 0, _a = Object.values(node); _i < _a.length; _i++) {
10
+ var child = _a[_i];
11
+ if ((0, utils_1.isNode)(child)) {
12
+ (0, exports.visitAllNodes)(child, visit);
13
+ }
14
+ else if ((0, utils_1.isArray)(child)) {
15
+ child
16
+ .filter(utils_1.isNode)
17
+ .forEach(function (childNode) { return (0, exports.visitAllNodes)(childNode, visit); });
18
+ }
19
+ }
20
+ };
21
+ exports.visitAllNodes = visitAllNodes;
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "prettier-plugin-sql-cst",
3
+ "version": "0.1.0",
4
+ "description": "Prettier plugin for SQL",
5
+ "author": "Rene Saarsoo <nene@triin.net>",
6
+ "license": "GPL-3.0-or-later",
7
+ "main": "dist/index.js",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/nene/prettier-plugin-sql-cst"
11
+ },
12
+ "homepage": "https://github.com/nene/prettier-plugin-sql-cst",
13
+ "bugs": {
14
+ "url": "https://github.com/nene/prettier-plugin-sql-cst/issues"
15
+ },
16
+ "files": [
17
+ "dist/"
18
+ ],
19
+ "scripts": {
20
+ "prepublishOnly": "yarn test && yarn build",
21
+ "clean": "rm -rf dist",
22
+ "build": "yarn clean && tsc",
23
+ "test": "jest"
24
+ },
25
+ "keywords": [
26
+ "prettier",
27
+ "sql",
28
+ "plugin"
29
+ ],
30
+ "dependencies": {
31
+ "prettier": "^2.8.2",
32
+ "sql-parser-cst": "^0.11.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/jest": "^29.2.5",
36
+ "dedent-js": "^1.0.1",
37
+ "jest": "^29.3.1",
38
+ "ts-jest": "^29.0.3",
39
+ "typescript": "^4.9.4"
40
+ }
41
+ }