prettier-plugin-sql-cst 0.1.1 → 0.3.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 +56 -29
- package/dist/embedJson.js +43 -0
- package/dist/index.js +26 -18
- package/dist/node_utils.js +4 -1
- package/dist/options.js +1 -13
- package/dist/printSql.js +2 -2
- package/dist/print_utils.js +3 -2
- package/dist/syntax/alter_table.js +17 -0
- package/dist/syntax/data_type.js +3 -0
- package/dist/syntax/drop_table.js +8 -0
- package/dist/syntax/expr.js +38 -2
- package/dist/syntax/index.js +28 -24
- package/dist/syntax/program.js +17 -14
- package/dist/syntax/select.js +7 -1
- package/dist/syntax/transformMap.js +32 -0
- package/dist/syntax/view.js +21 -0
- package/dist/transform/aliasAs.js +15 -0
- package/dist/{comments.js → transform/comments.js} +1 -1
- package/dist/transform/transformCst.js +7 -0
- package/dist/utils.js +3 -1
- package/package.json +2 -2
- package/dist/aliasAs.js +0 -39
package/README.md
CHANGED
|
@@ -40,6 +40,24 @@ ORDER BY client.name
|
|
|
40
40
|
LIMIT 100
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
+
## Formatting philosophy
|
|
44
|
+
|
|
45
|
+
- Adapt formatting based on expression length.
|
|
46
|
+
- Stick to one style and avoid configuration options.
|
|
47
|
+
- Format embedded languages (like JSON data and JavaScript programs).
|
|
48
|
+
- When unsure, preserve existing syntax.
|
|
49
|
+
|
|
50
|
+
## Non-whitespace formatting
|
|
51
|
+
|
|
52
|
+
Currently this plugin preserves most of the syntax elements
|
|
53
|
+
and concentrates mainly on the layout of whitespace.
|
|
54
|
+
For example it does not add/remove paratheses.
|
|
55
|
+
|
|
56
|
+
There are some opinionated non-whitespace changes though:
|
|
57
|
+
|
|
58
|
+
- UPPERCASE all keywords.
|
|
59
|
+
- Add `AS` keyword to all alias definitions.
|
|
60
|
+
|
|
43
61
|
## Getting started
|
|
44
62
|
|
|
45
63
|
Install it as any other Prettier plugin:
|
|
@@ -51,48 +69,57 @@ npm install --save-dev prettier prettier-plugin-sql-cst
|
|
|
51
69
|
Then use it on SQL files though Prettier command line tool or Prettier extension
|
|
52
70
|
for your editor of choice.
|
|
53
71
|
|
|
72
|
+
## Choosing an SQL dialect
|
|
73
|
+
|
|
74
|
+
By default the plugin will determine SQL dialect based on file extension:
|
|
75
|
+
|
|
76
|
+
- `.sql` or `.sqlite` - SQLite
|
|
77
|
+
- `.bigquery` - BigQuery
|
|
78
|
+
|
|
79
|
+
You can override this behavior with a prettier configuration:
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"overrides": [
|
|
84
|
+
{
|
|
85
|
+
"files": ["*.sql"],
|
|
86
|
+
"options": { "parser": "bigquery" }
|
|
87
|
+
}
|
|
88
|
+
]
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The plugin provides the following parsers:
|
|
93
|
+
|
|
94
|
+
- `sqlite`
|
|
95
|
+
- `bigquery`
|
|
96
|
+
|
|
54
97
|
## Configuration
|
|
55
98
|
|
|
56
|
-
The standard Prettier options
|
|
99
|
+
The standard Prettier options [printWidth][], [tabWidth][], [useTabs][] apply.
|
|
100
|
+
There are also some SQL-specific options:
|
|
57
101
|
|
|
58
|
-
| API Option |
|
|
59
|
-
| ---------------- |
|
|
60
|
-
| `
|
|
61
|
-
| `tabWidth` | `2` | [Same as in Prettier][prettier-tab-width] |
|
|
62
|
-
| `useTabs` | `false` | [Same as in Prettier][prettier-use-tabs] |
|
|
63
|
-
| `sqlKeywordCase` | `preserve` | Converts SQL keywords to `upper` or `lower` case. |
|
|
64
|
-
| `sqlAliasAs` | `preserve` | Uses AS keyword in aliases either `always` or `never`. |
|
|
102
|
+
| API Option | Default | Description |
|
|
103
|
+
| ---------------- | :-----: | ------------------------------------------------------------------------- |
|
|
104
|
+
| `sqlKeywordCase` | `upper` | Converts SQL keywords to `upper` or `lower` case, or `preserve` existing. |
|
|
65
105
|
|
|
66
106
|
## Limitations and development status
|
|
67
107
|
|
|
68
|
-
Currently this plugin
|
|
108
|
+
Currently this plugin supports most of **SQLite** syntax.
|
|
109
|
+
(It also supports some **BigQuery**, but only the syntax that's subset of SQLite.)
|
|
69
110
|
Formatting of the following SQL statements is fully implemented:
|
|
70
111
|
|
|
71
112
|
- SELECT
|
|
72
113
|
- UPDATE
|
|
73
114
|
- INSERT
|
|
74
115
|
- DELETE
|
|
75
|
-
- CREATE TABLE
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
For example it does not add/remove paratheses or modify the case of keywords (by default).
|
|
79
|
-
It only manipulates whitespace.
|
|
80
|
-
|
|
81
|
-
This will likely change in the future, with the goal of being more opinionated.
|
|
82
|
-
Some possibilities:
|
|
83
|
-
|
|
84
|
-
- capitalize all keywords
|
|
85
|
-
- ensure all alias definitions use the `AS` keyword.
|
|
86
|
-
- ensure consistent use of quotes (e.g. always quote keywords)
|
|
87
|
-
- eliminate redundant parenthesis.
|
|
88
|
-
|
|
89
|
-
The exact formatting style is also very much still in flux.
|
|
90
|
-
The overall goal is to have no options to configure different styles,
|
|
91
|
-
but rather to develop one opinionated style of formatting SQL.
|
|
116
|
+
- CREATE / DROP / ALTER TABLE
|
|
117
|
+
- CREATE / DROP VIEW
|
|
118
|
+
- CREATE / DROP INDEX
|
|
92
119
|
|
|
93
120
|
[prettier]: https://prettier.io/
|
|
94
|
-
[
|
|
95
|
-
[
|
|
96
|
-
[
|
|
121
|
+
[printWidth]: https://prettier.io/docs/en/options.html#print-width
|
|
122
|
+
[tabWidth]: https://prettier.io/docs/en/options.html#tab-width
|
|
123
|
+
[useTabs]: https://prettier.io/docs/en/options.html#tabs
|
|
97
124
|
[sql-parser-cst]: https://github.com/nene/sql-parser-cst
|
|
98
125
|
[wadler-prettier]: http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf
|
|
@@ -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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.embedJson = void 0;
|
|
15
|
+
var node_utils_1 = require("./node_utils");
|
|
16
|
+
var print_utils_1 = require("./print_utils");
|
|
17
|
+
var embedJson = function (path, print, textToDoc, options) {
|
|
18
|
+
var node = path.getValue();
|
|
19
|
+
var parent = path.getParentNode();
|
|
20
|
+
if ((0, node_utils_1.isStringLiteral)(node) && (0, node_utils_1.isJsonLiteral)(parent)) {
|
|
21
|
+
if (containsTripleQuote(node.value) ||
|
|
22
|
+
containsBackslash(node.value) ||
|
|
23
|
+
isRawString(node)) {
|
|
24
|
+
// Give up for now. Don't format JSON inside the string.
|
|
25
|
+
// Tackle these corner-case in the future.
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
var json = textToDoc(node.value, __assign(__assign({}, options), { parser: "json" }));
|
|
29
|
+
var inlineQuote = containsSingleQuote(node.value) ? "'''" : "'";
|
|
30
|
+
return [
|
|
31
|
+
(0, print_utils_1.ifBreak)("'''", inlineQuote),
|
|
32
|
+
(0, print_utils_1.indent)([print_utils_1.softline, (0, print_utils_1.stripTrailingHardline)(json)]),
|
|
33
|
+
print_utils_1.softline,
|
|
34
|
+
(0, print_utils_1.ifBreak)("'''", inlineQuote),
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
};
|
|
39
|
+
exports.embedJson = embedJson;
|
|
40
|
+
var containsSingleQuote = function (json) { return /'/.test(json); };
|
|
41
|
+
var containsTripleQuote = function (json) { return /'''/.test(json); };
|
|
42
|
+
var containsBackslash = function (json) { return /\\/.test(json); };
|
|
43
|
+
var isRawString = function (node) { return /^R/i.test(node.text); };
|
package/dist/index.js
CHANGED
|
@@ -3,36 +3,44 @@ 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 embedJson_1 = require("./embedJson");
|
|
6
7
|
var utils_1 = require("./utils");
|
|
7
|
-
var
|
|
8
|
-
var aliasAs_1 = require("./aliasAs");
|
|
8
|
+
var transformCst_1 = require("./transform/transformCst");
|
|
9
9
|
var options_1 = require("./options");
|
|
10
10
|
Object.defineProperty(exports, "options", { enumerable: true, get: function () { return options_1.options; } });
|
|
11
11
|
exports.languages = [
|
|
12
12
|
{
|
|
13
|
-
extensions: [".sql"],
|
|
14
|
-
name: "SQL",
|
|
15
|
-
parsers: ["
|
|
13
|
+
extensions: [".sql", ".sqlite"],
|
|
14
|
+
name: "SQLite SQL",
|
|
15
|
+
parsers: ["sqlite"],
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
extensions: [".bigquery"],
|
|
19
|
+
name: "BigQuery SQL",
|
|
20
|
+
parsers: ["bigquery"],
|
|
16
21
|
},
|
|
17
22
|
];
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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]; },
|
|
23
|
+
var createParser = function (dialect) { return ({
|
|
24
|
+
parse: function (text, parsers, options) {
|
|
25
|
+
return (0, transformCst_1.transformCst)((0, sql_parser_cst_1.parse)(text, {
|
|
26
|
+
dialect: dialect,
|
|
27
|
+
includeRange: true,
|
|
28
|
+
includeComments: true,
|
|
29
|
+
filename: options.filepath,
|
|
30
|
+
}), options);
|
|
31
31
|
},
|
|
32
|
+
astFormat: "sql-cst",
|
|
33
|
+
locStart: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[0]; },
|
|
34
|
+
locEnd: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[1]; },
|
|
35
|
+
}); };
|
|
36
|
+
exports.parsers = {
|
|
37
|
+
sqlite: createParser("sqlite"),
|
|
38
|
+
bigquery: createParser("bigquery"),
|
|
32
39
|
};
|
|
33
40
|
exports.printers = {
|
|
34
41
|
"sql-cst": {
|
|
35
42
|
print: printSql_1.printSql,
|
|
43
|
+
embed: embedJson_1.embedJson,
|
|
36
44
|
printComment: function (path) {
|
|
37
45
|
return path.getValue().text;
|
|
38
46
|
},
|
package/dist/node_utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isKeyword = exports.isCreateTableStmt = exports.isFuncCall = exports.isValuesClause = void 0;
|
|
3
|
+
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) {
|
|
@@ -11,3 +11,6 @@ exports.isValuesClause = is("values_clause");
|
|
|
11
11
|
exports.isFuncCall = is("func_call");
|
|
12
12
|
exports.isCreateTableStmt = is("create_table_stmt");
|
|
13
13
|
exports.isKeyword = is("keyword");
|
|
14
|
+
exports.isEmpty = is("empty");
|
|
15
|
+
exports.isJsonLiteral = is("json_literal");
|
|
16
|
+
exports.isStringLiteral = is("string_literal");
|
package/dist/options.js
CHANGED
|
@@ -6,7 +6,7 @@ exports.options = {
|
|
|
6
6
|
type: "choice",
|
|
7
7
|
category: "SQL",
|
|
8
8
|
since: "0.1.0",
|
|
9
|
-
default: "
|
|
9
|
+
default: "upper",
|
|
10
10
|
description: "Enforces upper/lower case for SQL keywords",
|
|
11
11
|
choices: [
|
|
12
12
|
{
|
|
@@ -17,16 +17,4 @@ exports.options = {
|
|
|
17
17
|
{ value: "lower", description: "forces all keywords to lowercase" },
|
|
18
18
|
],
|
|
19
19
|
},
|
|
20
|
-
sqlAliasAs: {
|
|
21
|
-
type: "choice",
|
|
22
|
-
category: "SQL",
|
|
23
|
-
since: "0.1.1",
|
|
24
|
-
default: "preserve",
|
|
25
|
-
description: "Enforce or forbid use of AS keyword in aliases",
|
|
26
|
-
choices: [
|
|
27
|
-
{ value: "preserve", description: "keeps existing style" },
|
|
28
|
-
{ value: "always", description: "always includes AS keyword" },
|
|
29
|
-
{ value: "never", description: "never include AS keyword" },
|
|
30
|
-
],
|
|
31
|
-
},
|
|
32
20
|
};
|
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
|
|
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 =
|
|
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
|
}
|
package/dist/print_utils.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var _a;
|
|
3
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;
|
|
4
|
+
exports.containsNewline = exports.join = exports.stripTrailingHardline = 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;
|
|
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;
|
|
8
8
|
var origJoin = prettier_1.doc.builders.join;
|
|
9
|
+
exports.stripTrailingHardline = prettier_1.doc.utils.stripTrailingHardline;
|
|
9
10
|
/** Improved join() that also accepts a non-array docs parameter */
|
|
10
11
|
var join = function (sep, docs) {
|
|
11
12
|
return (0, utils_1.isArray)(docs) ? origJoin(sep, docs) : docs;
|
|
@@ -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
|
+
};
|
package/dist/syntax/data_type.js
CHANGED
|
@@ -12,4 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.dataTypeMap = void 0;
|
|
13
13
|
exports.dataTypeMap = {
|
|
14
14
|
data_type: function (print) { return __spreadArray([print.spaced("nameKw")], print(["params"]), true); },
|
|
15
|
+
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"]); },
|
|
15
18
|
};
|
package/dist/syntax/expr.js
CHANGED
|
@@ -12,10 +12,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.exprMap = void 0;
|
|
13
13
|
var print_utils_1 = require("../print_utils");
|
|
14
14
|
var node_utils_1 = require("../node_utils");
|
|
15
|
+
var utils_1 = require("../utils");
|
|
15
16
|
exports.exprMap = {
|
|
16
17
|
list_expr: function (print, node, path) {
|
|
17
18
|
var parent = path.getParentNode();
|
|
18
|
-
|
|
19
|
+
var children = print("items").map(function (it) { return (0, print_utils_1.group)(it); });
|
|
20
|
+
var lineType = (0, node_utils_1.isValuesClause)(parent) ? print_utils_1.hardline : print_utils_1.line;
|
|
21
|
+
// When last item is type:empty, we're dealing with a trailing comma.
|
|
22
|
+
// Don't add a space (or newline) after it.
|
|
23
|
+
// We still want to print it though, as it might have comments attached.
|
|
24
|
+
if ((0, node_utils_1.isEmpty)((0, utils_1.last)(node.items))) {
|
|
25
|
+
return [
|
|
26
|
+
(0, print_utils_1.join)([",", lineType], children.slice(0, -1)),
|
|
27
|
+
",",
|
|
28
|
+
(0, utils_1.last)(children),
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
return (0, print_utils_1.join)([",", lineType], children);
|
|
33
|
+
}
|
|
19
34
|
},
|
|
20
35
|
paren_expr: function (print, node, path) {
|
|
21
36
|
var parent = path.getParentNode();
|
|
@@ -28,7 +43,9 @@ exports.exprMap = {
|
|
|
28
43
|
}
|
|
29
44
|
return print.spaced(["left", "operator", "right"]);
|
|
30
45
|
},
|
|
31
|
-
prefix_op_expr: function (print
|
|
46
|
+
prefix_op_expr: function (print, node) {
|
|
47
|
+
return ((0, utils_1.isString)(node.operator) ? print : print.spaced)(["operator", "expr"]);
|
|
48
|
+
},
|
|
32
49
|
postfix_op_expr: function (print) { return print.spaced(["expr", "operator"]); },
|
|
33
50
|
between_expr: function (print) {
|
|
34
51
|
return print.spaced(["left", "betweenKw", "begin", "andKw", "end"]);
|
|
@@ -44,6 +61,7 @@ exports.exprMap = {
|
|
|
44
61
|
},
|
|
45
62
|
case_else: function (print) { return print.spaced(["elseKw", "result"]); },
|
|
46
63
|
member_expr: function (print) { return [print("object"), ".", print("property")]; },
|
|
64
|
+
bigquery_quoted_member_expr: function (print) { return ["`", print("expr"), "`"]; },
|
|
47
65
|
func_call: function (print) {
|
|
48
66
|
var fnCall = print(["name", "args"]);
|
|
49
67
|
var extras = print(["filter", "over"]);
|
|
@@ -60,10 +78,28 @@ exports.exprMap = {
|
|
|
60
78
|
cast_expr: function (print) { return print(["castKw", "args"]); },
|
|
61
79
|
cast_arg: function (print) { return print.spaced(["expr", "asKw", "dataType"]); },
|
|
62
80
|
raise_expr: function (print) { return print(["raiseKw", "args"]); },
|
|
81
|
+
interval_expr: function (print) { return print.spaced(["intervalKw", "expr", "unit"]); },
|
|
82
|
+
interval_unit_range: function (print) {
|
|
83
|
+
return print.spaced(["fromUnitKw", "toKw", "toUnitKw"]);
|
|
84
|
+
},
|
|
85
|
+
array_expr: function (print) {
|
|
86
|
+
return (0, print_utils_1.group)(["[", (0, print_utils_1.indent)([print_utils_1.softline, print("expr")]), print_utils_1.softline, "]"]);
|
|
87
|
+
},
|
|
88
|
+
struct_expr: function (print) {
|
|
89
|
+
return (0, print_utils_1.group)(["(", (0, print_utils_1.indent)([print_utils_1.softline, print("expr")]), print_utils_1.softline, ")"]);
|
|
90
|
+
},
|
|
91
|
+
typed_expr: function (print) { return print(["dataType", "expr"]); },
|
|
63
92
|
number_literal: function (print) { return print("text"); },
|
|
64
93
|
boolean_literal: function (print) { return print("valueKw"); },
|
|
65
94
|
string_literal: function (print) { return print("text"); },
|
|
66
95
|
null_literal: function (print) { return print("nullKw"); },
|
|
96
|
+
numeric_literal: function (print) { return print.spaced(["numericKw", "string"]); },
|
|
97
|
+
bignumeric_literal: function (print) { return print.spaced(["bignumericKw", "string"]); },
|
|
98
|
+
date_literal: function (print) { return print.spaced(["dateKw", "string"]); },
|
|
99
|
+
time_literal: function (print) { return print.spaced(["timeKw", "string"]); },
|
|
100
|
+
datetime_literal: function (print) { return print.spaced(["datetimeKw", "string"]); },
|
|
101
|
+
timestamp_literal: function (print) { return print.spaced(["timestampKw", "string"]); },
|
|
102
|
+
json_literal: function (print) { return print.spaced(["jsonKw", "string"]); },
|
|
67
103
|
identifier: function (print) { return print("text"); },
|
|
68
104
|
};
|
|
69
105
|
var isBooleanOp = function (_a) {
|
package/dist/syntax/index.js
CHANGED
|
@@ -1,28 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
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.
|
|
15
|
-
var
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
+
};
|
package/dist/syntax/program.js
CHANGED
|
@@ -3,19 +3,22 @@ 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) { return [
|
|
7
|
+
print("statements").map(function (doc, i) {
|
|
8
|
+
return printStatement(doc, i, node.statements);
|
|
9
|
+
}),
|
|
10
|
+
print_utils_1.hardline,
|
|
11
|
+
]; },
|
|
7
12
|
};
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
};
|
|
13
|
+
var printStatement = function (doc, i, statements) {
|
|
14
|
+
var node = statements[i];
|
|
15
|
+
if (i === 0) {
|
|
16
|
+
return doc;
|
|
17
|
+
}
|
|
18
|
+
else if (i < statements.length - 1 || node.type !== "empty") {
|
|
19
|
+
return [";", print_utils_1.hardline, print_utils_1.hardline, doc];
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return [";", doc];
|
|
23
|
+
}
|
|
21
24
|
};
|
package/dist/syntax/select.js
CHANGED
|
@@ -24,10 +24,12 @@ exports.selectMap = {
|
|
|
24
24
|
]; },
|
|
25
25
|
select_clause: function (print) {
|
|
26
26
|
return (0, print_utils_1.group)([
|
|
27
|
-
print.spaced(["selectKw", "distinctKw"]),
|
|
27
|
+
print.spaced(["selectKw", "distinctKw", "asStructOrValueKw"]),
|
|
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
|
},
|
|
@@ -64,6 +66,7 @@ exports.selectMap = {
|
|
|
64
66
|
group_by_clause: function (print) {
|
|
65
67
|
return (0, print_utils_1.group)([print.spaced("groupByKw"), (0, print_utils_1.indent)([print_utils_1.line, print("columns")])]);
|
|
66
68
|
},
|
|
69
|
+
group_by_rollup: function (print) { return print(["rollupKw", "columns"]); },
|
|
67
70
|
partition_by_clause: function (print) {
|
|
68
71
|
return (0, print_utils_1.group)([
|
|
69
72
|
print.spaced("partitionByKw"),
|
|
@@ -73,6 +76,9 @@ exports.selectMap = {
|
|
|
73
76
|
having_clause: function (print) {
|
|
74
77
|
return (0, print_utils_1.group)([print("havingKw"), (0, print_utils_1.indent)([print_utils_1.line, print("expr")])]);
|
|
75
78
|
},
|
|
79
|
+
qualify_clause: function (print) {
|
|
80
|
+
return (0, print_utils_1.group)([print("qualifyKw"), (0, print_utils_1.indent)([print_utils_1.line, print("expr")])]);
|
|
81
|
+
},
|
|
76
82
|
returning_clause: function (print) {
|
|
77
83
|
return (0, print_utils_1.group)([print("returningKw"), (0, print_utils_1.indent)([print_utils_1.line, print("columns")])]);
|
|
78
84
|
},
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processAliasAs = void 0;
|
|
4
|
+
var sql_parser_cst_1 = require("sql-parser-cst");
|
|
5
|
+
// Adds AS keyword to aliases if none exists
|
|
6
|
+
var processAliasAs = function (cst) {
|
|
7
|
+
var mutateAliases = (0, sql_parser_cst_1.cstVisitor)({
|
|
8
|
+
alias: function (node) {
|
|
9
|
+
node.asKw = node.asKw || { type: "keyword", name: "AS", text: "AS" };
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
mutateAliases(cst);
|
|
13
|
+
return cst;
|
|
14
|
+
};
|
|
15
|
+
exports.processAliasAs = processAliasAs;
|
|
@@ -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("
|
|
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)); };
|
|
7
|
+
exports.transformCst = transformCst;
|
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.arrayWrap = exports.isNode = exports.isEmptyArray = exports.isArray = exports.isObject = exports.isString = exports.isDefined = void 0;
|
|
3
|
+
exports.last = exports.arrayWrap = exports.isNode = exports.isEmptyArray = exports.isArray = exports.isObject = exports.isString = exports.isDefined = void 0;
|
|
4
4
|
var isDefined = function (value) {
|
|
5
5
|
return value !== undefined;
|
|
6
6
|
};
|
|
@@ -26,3 +26,5 @@ var isCommentType = function (type) {
|
|
|
26
26
|
};
|
|
27
27
|
var arrayWrap = function (x) { return ((0, exports.isArray)(x) ? x : [x]); };
|
|
28
28
|
exports.arrayWrap = arrayWrap;
|
|
29
|
+
var last = function (arr) { return arr[arr.length - 1]; };
|
|
30
|
+
exports.last = last;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-sql-cst",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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.
|
|
32
|
+
"sql-parser-cst": "^0.11.1"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/jest": "^29.2.5",
|
package/dist/aliasAs.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
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.processAliasAs = void 0;
|
|
13
|
-
var sql_parser_cst_1 = require("sql-parser-cst");
|
|
14
|
-
var processAliasAs = function (cst, options) {
|
|
15
|
-
var mutateAliases = (0, sql_parser_cst_1.cstVisitor)({
|
|
16
|
-
alias: function (node) {
|
|
17
|
-
var _a, _b;
|
|
18
|
-
switch (options.sqlAliasAs) {
|
|
19
|
-
case "preserve":
|
|
20
|
-
// do nothing
|
|
21
|
-
break;
|
|
22
|
-
case "always":
|
|
23
|
-
// Add AS keyword if none exists
|
|
24
|
-
node.asKw = node.asKw || { type: "keyword", name: "AS", text: "AS" };
|
|
25
|
-
break;
|
|
26
|
-
case "never":
|
|
27
|
-
// Remove AS keyword and ensure comments attached to it don't get lost
|
|
28
|
-
if (node.asKw && (((_a = node.leading) === null || _a === void 0 ? void 0 : _a.length) || ((_b = node.trailing) === null || _b === void 0 ? void 0 : _b.length))) {
|
|
29
|
-
var comments = __spreadArray(__spreadArray([], (node.leading || []), true), (node.trailing || []), true);
|
|
30
|
-
node.alias.leading = __spreadArray(__spreadArray([], comments, true), (node.alias.leading || []), true);
|
|
31
|
-
}
|
|
32
|
-
node.asKw = undefined;
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
mutateAliases(cst);
|
|
37
|
-
return cst;
|
|
38
|
-
};
|
|
39
|
-
exports.processAliasAs = processAliasAs;
|