prettier-plugin-sql-cst 0.18.0 → 0.19.1
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/dist/CstToDocMap.d.ts +2 -2
- package/dist/CstToDocMap.js +1 -2
- package/dist/PrintFn.js +1 -2
- package/dist/embed.js +5 -13
- package/dist/embedJs.js +28 -84
- package/dist/embedJson.js +29 -85
- package/dist/embedSql.js +26 -76
- package/dist/index.js +28 -34
- package/dist/node_utils.d.ts +1 -0
- package/dist/node_utils.js +44 -54
- package/dist/options.js +1 -4
- package/dist/printComment.js +2 -6
- package/dist/printSql.js +29 -39
- package/dist/print_utils.js +19 -38
- package/dist/syntax/alias.js +2 -5
- package/dist/syntax/alter_action.js +115 -208
- package/dist/syntax/alter_table.js +18 -27
- package/dist/syntax/analyze.js +3 -8
- package/dist/syntax/base.js +6 -9
- package/dist/syntax/cast.js +3 -6
- package/dist/syntax/comment.js +53 -122
- package/dist/syntax/constraint.d.ts +1 -1
- package/dist/syntax/constraint.js +54 -90
- package/dist/syntax/create_table.js +49 -87
- package/dist/syntax/data_type.js +25 -44
- package/dist/syntax/dcl.js +78 -136
- package/dist/syntax/delete.js +4 -9
- package/dist/syntax/dialects/bigquery.js +70 -116
- package/dist/syntax/dialects/mysql.js +2 -5
- package/dist/syntax/dialects/postgresql.js +8 -13
- package/dist/syntax/dialects/sqlite.js +8 -17
- package/dist/syntax/do.js +3 -6
- package/dist/syntax/domain.js +10 -19
- package/dist/syntax/drop_table.js +10 -15
- package/dist/syntax/explain.js +6 -11
- package/dist/syntax/expr.js +124 -173
- package/dist/syntax/extension.js +12 -21
- package/dist/syntax/frame.js +11 -16
- package/dist/syntax/function.js +53 -63
- package/dist/syntax/index.js +54 -81
- package/dist/syntax/insert.js +32 -49
- package/dist/syntax/merge.js +25 -30
- package/dist/syntax/other_clauses.js +5 -14
- package/dist/syntax/parameter.js +13 -26
- package/dist/syntax/policy.js +26 -41
- package/dist/syntax/prepared_statements.js +23 -43
- package/dist/syntax/proc_clause.js +10 -15
- package/dist/syntax/procedural_language.js +80 -111
- package/dist/syntax/procedure.js +36 -40
- package/dist/syntax/program.js +15 -22
- package/dist/syntax/publication.js +26 -47
- package/dist/syntax/rename_table.js +7 -12
- package/dist/syntax/role.js +30 -55
- package/dist/syntax/schema.js +17 -35
- package/dist/syntax/select.js +157 -226
- package/dist/syntax/sequence.js +35 -52
- package/dist/syntax/subscription.js +9 -24
- package/dist/syntax/transaction.js +18 -33
- package/dist/syntax/transformMap.js +99 -63
- package/dist/syntax/trigger.js +48 -72
- package/dist/syntax/truncate.js +9 -14
- package/dist/syntax/type.js +11 -22
- package/dist/syntax/unsupported_grammar.js +3 -6
- package/dist/syntax/update.js +7 -14
- package/dist/syntax/view.js +32 -43
- package/dist/transform/addFinalSemicolon.js +5 -9
- package/dist/transform/aliasAs.js +4 -8
- package/dist/transform/canonicKeywords.js +16 -20
- package/dist/transform/canonicOperators.js +8 -12
- package/dist/transform/comments.js +10 -31
- package/dist/transform/stripTrailingCommas.js +7 -11
- package/dist/transform/transformCst.js +13 -17
- package/dist/transform/transformUtils.js +2 -6
- package/dist/utils.js +9 -30
- package/dist/visitAllNodes.js +8 -13
- package/package.json +9 -8
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var transformUtils_1 = require("./transformUtils");
|
|
6
|
-
var canonicKeywords = function (cst) {
|
|
7
|
-
(0, sql_parser_cst_1.cstVisitor)({
|
|
1
|
+
import { cstVisitor } from "sql-parser-cst";
|
|
2
|
+
import { keyword } from "./transformUtils";
|
|
3
|
+
export const canonicKeywords = (cst) => {
|
|
4
|
+
cstVisitor({
|
|
8
5
|
// Replaces TEMP with TEMPORARY
|
|
9
|
-
relation_kind:
|
|
6
|
+
relation_kind: (node) => {
|
|
10
7
|
if (Array.isArray(node.kindKw)) {
|
|
11
8
|
// TODO...
|
|
12
9
|
}
|
|
@@ -18,14 +15,14 @@ var canonicKeywords = function (cst) {
|
|
|
18
15
|
}
|
|
19
16
|
},
|
|
20
17
|
// Replaces MySQL SELECT DISTINCTROW with SELECT DISTINCT
|
|
21
|
-
select_distinct:
|
|
18
|
+
select_distinct: (node) => {
|
|
22
19
|
if (node.distinctKw.name === "DISTINCTROW") {
|
|
23
20
|
node.distinctKw.name = "DISTINCT";
|
|
24
21
|
node.distinctKw.text = "DISTINCT";
|
|
25
22
|
}
|
|
26
23
|
},
|
|
27
24
|
// Replaces RENAME & RENAME AS with RENAME TO
|
|
28
|
-
alter_action_rename:
|
|
25
|
+
alter_action_rename: (node) => {
|
|
29
26
|
if (Array.isArray(node.renameKw)) {
|
|
30
27
|
if (node.renameKw[1].name === "AS") {
|
|
31
28
|
node.renameKw[1].name = "TO";
|
|
@@ -33,34 +30,33 @@ var canonicKeywords = function (cst) {
|
|
|
33
30
|
}
|
|
34
31
|
}
|
|
35
32
|
else {
|
|
36
|
-
node.renameKw = [node.renameKw,
|
|
33
|
+
node.renameKw = [node.renameKw, keyword("TO")];
|
|
37
34
|
}
|
|
38
35
|
},
|
|
39
36
|
// Replaces INSERT with INSERT INTO
|
|
40
|
-
insert_clause:
|
|
37
|
+
insert_clause: (node) => {
|
|
41
38
|
if (!node.intoKw) {
|
|
42
|
-
node.intoKw =
|
|
39
|
+
node.intoKw = keyword("INTO");
|
|
43
40
|
}
|
|
44
41
|
},
|
|
45
42
|
// Replaces MERGE with MERGE INTO
|
|
46
|
-
merge_clause:
|
|
43
|
+
merge_clause: (node) => {
|
|
47
44
|
if (!node.intoKw) {
|
|
48
|
-
node.intoKw =
|
|
45
|
+
node.intoKw = keyword("INTO");
|
|
49
46
|
}
|
|
50
47
|
},
|
|
51
48
|
// Replaces DELETE with DELETE FROM
|
|
52
|
-
delete_clause:
|
|
49
|
+
delete_clause: (node) => {
|
|
53
50
|
if (!node.fromKw) {
|
|
54
|
-
node.fromKw =
|
|
51
|
+
node.fromKw = keyword("FROM");
|
|
55
52
|
}
|
|
56
53
|
},
|
|
57
54
|
// Replaces TRUNCATE with TRUNCATE TABLE
|
|
58
|
-
truncate_stmt:
|
|
55
|
+
truncate_stmt: (node) => {
|
|
59
56
|
if (!node.tableKw) {
|
|
60
|
-
node.tableKw =
|
|
57
|
+
node.tableKw = keyword("TABLE");
|
|
61
58
|
}
|
|
62
59
|
},
|
|
63
60
|
})(cst);
|
|
64
61
|
return cst;
|
|
65
62
|
};
|
|
66
|
-
exports.canonicKeywords = canonicKeywords;
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var canonicOperators = function (cst, originalText, options) {
|
|
7
|
-
(0, sql_parser_cst_1.cstVisitor)({
|
|
8
|
-
binary_expr: function (node) {
|
|
1
|
+
import { cstVisitor } from "sql-parser-cst";
|
|
2
|
+
import { keyword } from "./transformUtils";
|
|
3
|
+
export const canonicOperators = (cst, originalText, options) => {
|
|
4
|
+
cstVisitor({
|
|
5
|
+
binary_expr: (node) => {
|
|
9
6
|
// Replaces <> operator with !=
|
|
10
7
|
if (node.operator === "<>") {
|
|
11
8
|
node.operator = "!=";
|
|
@@ -13,15 +10,15 @@ var canonicOperators = function (cst, originalText, options) {
|
|
|
13
10
|
// Replace MySQL && and || operators with AND and OR
|
|
14
11
|
if (options.parser === "mysql" || options.parser === "mariadb") {
|
|
15
12
|
if (node.operator === "&&") {
|
|
16
|
-
node.operator =
|
|
13
|
+
node.operator = keyword("AND");
|
|
17
14
|
}
|
|
18
15
|
if (node.operator === "||") {
|
|
19
|
-
node.operator =
|
|
16
|
+
node.operator = keyword("OR");
|
|
20
17
|
}
|
|
21
18
|
}
|
|
22
19
|
},
|
|
23
20
|
// Replaces PostgreSQL deprecated := operator with =>
|
|
24
|
-
named_arg:
|
|
21
|
+
named_arg: (node) => {
|
|
25
22
|
if (node.operator === ":=") {
|
|
26
23
|
node.operator = "=>";
|
|
27
24
|
}
|
|
@@ -29,4 +26,3 @@ var canonicOperators = function (cst, originalText, options) {
|
|
|
29
26
|
})(cst);
|
|
30
27
|
return cst;
|
|
31
28
|
};
|
|
32
|
-
exports.canonicOperators = canonicOperators;
|
|
@@ -1,27 +1,4 @@
|
|
|
1
|
-
|
|
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");
|
|
1
|
+
import { visitAllNodes } from "../visitAllNodes";
|
|
25
2
|
/**
|
|
26
3
|
* Gathers comments from .leading and .trailing fields of all nodes
|
|
27
4
|
* into a single .comments field in root Program node.
|
|
@@ -30,14 +7,16 @@ var visitAllNodes_1 = require("../visitAllNodes");
|
|
|
30
7
|
* but we need this info to check whether the empty lines between nodes
|
|
31
8
|
* are perhaps inside comments (see hasEmptyLineBetweenNodes()).
|
|
32
9
|
*/
|
|
33
|
-
|
|
34
|
-
return
|
|
10
|
+
export const moveCommentsToRoot = (cst) => {
|
|
11
|
+
return {
|
|
12
|
+
...cst,
|
|
13
|
+
comments: extractComments(cst),
|
|
14
|
+
};
|
|
35
15
|
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
comments.push.apply(comments, __spreadArray(__spreadArray([], (node.leading || []), false), (node.trailing || []), false));
|
|
16
|
+
const extractComments = (cst) => {
|
|
17
|
+
const comments = [];
|
|
18
|
+
visitAllNodes(cst, (node) => {
|
|
19
|
+
comments.push(...(node.leading || []), ...(node.trailing || []));
|
|
41
20
|
});
|
|
42
21
|
return comments;
|
|
43
22
|
};
|
|
@@ -1,19 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var sql_parser_cst_1 = require("sql-parser-cst");
|
|
5
|
-
var node_utils_1 = require("../node_utils");
|
|
6
|
-
var utils_1 = require("../utils");
|
|
1
|
+
import { cstVisitor } from "sql-parser-cst";
|
|
2
|
+
import { isEmpty } from "../node_utils";
|
|
3
|
+
import { last } from "../utils";
|
|
7
4
|
// Removes trailing comma from SELECT clauses
|
|
8
|
-
|
|
5
|
+
export const stripTrailingCommas = (cst) => {
|
|
9
6
|
// Trailing comma is represented as an empty element at the end of column list
|
|
10
|
-
|
|
11
|
-
select_clause:
|
|
12
|
-
if (node.columns &&
|
|
7
|
+
cstVisitor({
|
|
8
|
+
select_clause: (node) => {
|
|
9
|
+
if (node.columns && isEmpty(last(node.columns.items))) {
|
|
13
10
|
node.columns.items = node.columns.items.slice(0, -1);
|
|
14
11
|
}
|
|
15
12
|
},
|
|
16
13
|
})(cst);
|
|
17
14
|
return cst;
|
|
18
15
|
};
|
|
19
|
-
exports.stripTrailingCommas = stripTrailingCommas;
|
|
@@ -1,25 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var canonicOperators_1 = require("./canonicOperators");
|
|
10
|
-
var transformCst = function (cst, originalText, options) {
|
|
11
|
-
var transforms = [
|
|
1
|
+
import { moveCommentsToRoot } from "./comments";
|
|
2
|
+
import { processAliasAs } from "./aliasAs";
|
|
3
|
+
import { stripTrailingCommas } from "./stripTrailingCommas";
|
|
4
|
+
import { addFinalSemicolon } from "./addFinalSemicolon";
|
|
5
|
+
import { canonicKeywords } from "./canonicKeywords";
|
|
6
|
+
import { canonicOperators } from "./canonicOperators";
|
|
7
|
+
export const transformCst = (cst, originalText, options) => {
|
|
8
|
+
const transforms = [
|
|
12
9
|
// Note that we first perform moveCommentsToRoot transform,
|
|
13
10
|
// so we don't need to worry about comments interfering with other transforms
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
moveCommentsToRoot,
|
|
12
|
+
stripTrailingCommas,
|
|
16
13
|
];
|
|
17
14
|
if (options.sqlFinalSemicolon) {
|
|
18
|
-
transforms.push(
|
|
15
|
+
transforms.push(addFinalSemicolon);
|
|
19
16
|
}
|
|
20
17
|
if (options.sqlCanonicalSyntax) {
|
|
21
|
-
transforms.push(
|
|
18
|
+
transforms.push(processAliasAs, canonicOperators, canonicKeywords);
|
|
22
19
|
}
|
|
23
|
-
return transforms.reduce(
|
|
20
|
+
return transforms.reduce((cst, transform) => transform(cst, originalText, options), cst);
|
|
24
21
|
};
|
|
25
|
-
exports.transformCst = transformCst;
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.keyword = void 0;
|
|
4
|
-
function keyword(name) {
|
|
5
|
-
return { type: "keyword", name: name, text: name };
|
|
1
|
+
export function keyword(name) {
|
|
2
|
+
return { type: "keyword", name, text: name };
|
|
6
3
|
}
|
|
7
|
-
exports.keyword = keyword;
|
package/dist/utils.js
CHANGED
|
@@ -1,30 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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;
|
|
29
|
-
var last = function (arr) { return arr[arr.length - 1]; };
|
|
30
|
-
exports.last = last;
|
|
1
|
+
export const isDefined = (value) => value !== undefined;
|
|
2
|
+
export const isString = (value) => typeof value === "string";
|
|
3
|
+
export const isObject = (x) => typeof x === "object" && x !== null && !(x instanceof Array);
|
|
4
|
+
export const isArray = (x) => Array.isArray(x);
|
|
5
|
+
export const isEmptyArray = (x) => isArray(x) && x.length === 0;
|
|
6
|
+
export const isNode = (value) => isObject(value) && isString(value.type) && !isCommentType(value.type);
|
|
7
|
+
const isCommentType = (type) => type === "block_comment" || type === "line_comment";
|
|
8
|
+
export const arrayWrap = (x) => (isArray(x) ? x : [x]);
|
|
9
|
+
export const last = (arr) => arr[arr.length - 1];
|
package/dist/visitAllNodes.js
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.visitAllNodes = void 0;
|
|
4
|
-
var utils_1 = require("./utils");
|
|
1
|
+
import { isArray, isNode } from "./utils";
|
|
5
2
|
/* Executes a function with all nodes in syntax tree */
|
|
6
|
-
|
|
3
|
+
export const visitAllNodes = (node, visit) => {
|
|
7
4
|
visit(node);
|
|
8
5
|
// Visit all children
|
|
9
|
-
for (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
(0, exports.visitAllNodes)(child, visit);
|
|
6
|
+
for (const child of Object.values(node)) {
|
|
7
|
+
if (isNode(child)) {
|
|
8
|
+
visitAllNodes(child, visit);
|
|
13
9
|
}
|
|
14
|
-
else if (
|
|
10
|
+
else if (isArray(child)) {
|
|
15
11
|
child
|
|
16
|
-
.filter(
|
|
17
|
-
.forEach(
|
|
12
|
+
.filter(isNode)
|
|
13
|
+
.forEach((childNode) => visitAllNodes(childNode, visit));
|
|
18
14
|
}
|
|
19
15
|
}
|
|
20
16
|
};
|
|
21
|
-
exports.visitAllNodes = visitAllNodes;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-sql-cst",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.1",
|
|
4
4
|
"description": "Prettier plugin for SQL",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Barna Magyarkuti <bmagyarkuti@gmail.com>",
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
"scripts": {
|
|
26
26
|
"prepublishOnly": "yarn analyze-field-access && yarn test && yarn build",
|
|
27
27
|
"clean": "rm -rf dist",
|
|
28
|
-
"build": "yarn clean && tsc",
|
|
28
|
+
"build": "yarn clean && tsc -p tsconfig.build.json",
|
|
29
29
|
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
|
|
30
30
|
"pretty": "prettier -w src/ test/",
|
|
31
31
|
"ts:check": "tsc --noEmit",
|
|
32
|
-
"analyze-field-access": "
|
|
32
|
+
"analyze-field-access": "tsx scripts/analyze-field-access.ts src/index.ts"
|
|
33
33
|
},
|
|
34
34
|
"keywords": [
|
|
35
35
|
"prettier",
|
|
@@ -38,16 +38,17 @@
|
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"prettier": "^3.0.3",
|
|
41
|
-
"sql-parser-cst": "^0.
|
|
41
|
+
"sql-parser-cst": "^0.39.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@types/jest": "^
|
|
44
|
+
"@types/jest": "^30.0.0",
|
|
45
45
|
"@types/node": "^22.9.0",
|
|
46
46
|
"chalk": "^4.1.2",
|
|
47
47
|
"dedent-js": "^1.0.1",
|
|
48
|
-
"jest": "^
|
|
49
|
-
"ts-jest": "^29.
|
|
48
|
+
"jest": "^30.3.0",
|
|
49
|
+
"ts-jest": "^29.4.9",
|
|
50
50
|
"ts-node": "^10.9.2",
|
|
51
|
-
"
|
|
51
|
+
"tsx": "^4.21.0",
|
|
52
|
+
"typescript": "^5.9.3"
|
|
52
53
|
}
|
|
53
54
|
}
|