prettier-plugin-sql-cst 0.14.2 → 0.16.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
|
@@ -69,6 +69,14 @@ yarn add --dev prettier prettier-plugin-sql-cst
|
|
|
69
69
|
Then use it on SQL files through Prettier command line tool or Prettier extension
|
|
70
70
|
for your editor of choice.
|
|
71
71
|
|
|
72
|
+
For a one-off run you can execute:
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
prettier --plugin prettier-plugin-sql-cst --parser sqlite myfile.sql
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
But generally you're better off using a configuration file.
|
|
79
|
+
|
|
72
80
|
## Choosing an SQL dialect
|
|
73
81
|
|
|
74
82
|
By default the plugin will determine SQL dialect based on file extension:
|
|
@@ -119,14 +127,14 @@ The plugin provides the following parsers:
|
|
|
119
127
|
The standard Prettier options [printWidth][], [tabWidth][], [useTabs][] apply.
|
|
120
128
|
There are also some SQL-specific options:
|
|
121
129
|
|
|
122
|
-
| API Option | Default | Description
|
|
123
|
-
| ----------------------------- | :-----: |
|
|
124
|
-
| `sqlKeywordCase` | `upper` | Convert SQL keywords to `upper` or `lower` case, or `preserve` existing. Note that for now `preserve` is somewhat incompatible with `sqlCanonicalSyntax: true` (e.g. the added `AS` keywords will always be in uppercase).
|
|
125
|
-
| `sqlLiteralCase` | `upper` | Convert SQL literals TRUE, FALSE and NULL to `upper` or `lower` case, or `preserve` existing. Also applies to `on` & `off` literals in PostgreSQL SET statements.
|
|
126
|
-
| `sqlParamTypes` | `[]` | Array of bound parameter types: `?`, `?nr`, `$nr`, `:name`, `@name`, `$name`.
|
|
127
|
-
| `sqlCanonicalSyntax` | `true` | When enabled, performs some opinionated changes of keywords and operators, like enforcing the use of `AS` in aliases and replacing `<>` comparisons with `!=`. See [STYLE_GUIDE][] for more details. (Since 0.11.0)
|
|
128
|
-
| `sqlFinalSemicolon` | `true` | When enabled, enforces a semicolon at the end of last statement. When disabled leaves it up to the author whether to add a final semicolon or not. (Since 0.13.0)
|
|
129
|
-
| `sqlAcceptUnsupportedGrammar` | `false` | Normally when the plugin encounters SQL syntax it doesn't support it will throw an error and won't format anything at all. With this option enabled, it will skip over SQL statements it doesn't recognize, leaving them as-is.
|
|
130
|
+
| API Option | Default | Description |
|
|
131
|
+
| ----------------------------- | :-----: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
132
|
+
| `sqlKeywordCase` | `upper` | Convert SQL keywords to `upper` or `lower` case, or `preserve` existing. Note that for now `preserve` is somewhat incompatible with `sqlCanonicalSyntax: true` (e.g. the added `AS` keywords will always be in uppercase). In most dialects this also applies to data types, but not in PostgreSQL where data type names are treated as identifiers. |
|
|
133
|
+
| `sqlLiteralCase` | `upper` | Convert SQL literals TRUE, FALSE and NULL to `upper` or `lower` case, or `preserve` existing. Also applies to `on` & `off` literals in PostgreSQL SET statements. |
|
|
134
|
+
| `sqlParamTypes` | `[]` | Array of bound parameter types: `?`, `?nr`, `$nr`, `:name`, `@name`, `$name`. |
|
|
135
|
+
| `sqlCanonicalSyntax` | `true` | When enabled, performs some opinionated changes of keywords and operators, like enforcing the use of `AS` in aliases and replacing `<>` comparisons with `!=`. See [STYLE_GUIDE][] for more details. (Since 0.11.0) |
|
|
136
|
+
| `sqlFinalSemicolon` | `true` | When enabled, enforces a semicolon at the end of last statement. When disabled leaves it up to the author whether to add a final semicolon or not. (Since 0.13.0) |
|
|
137
|
+
| `sqlAcceptUnsupportedGrammar` | `false` | Normally when the plugin encounters SQL syntax it doesn't support it will throw an error and won't format anything at all. With this option enabled, it will skip over SQL statements it doesn't recognize, leaving them as-is. |
|
|
130
138
|
|
|
131
139
|
## Usage inside VSCode
|
|
132
140
|
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.alterActionMap = void 0;
|
|
4
4
|
var print_utils_1 = require("../print_utils");
|
|
5
|
+
var constraint_1 = require("./constraint");
|
|
5
6
|
exports.alterActionMap = {
|
|
6
7
|
alter_action_rename: function (print) { return print.spaced(["renameKw", "newName"]); },
|
|
7
8
|
alter_action_rename_column: function (print) {
|
|
@@ -19,8 +20,12 @@ exports.alterActionMap = {
|
|
|
19
20
|
alter_action_set_default_collate: function (print) {
|
|
20
21
|
return print.spaced(["setDefaultCollateKw", "collation"]);
|
|
21
22
|
},
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
/** cst-ignore: constraint, modifiers */
|
|
24
|
+
alter_action_add_constraint: function (print, node) {
|
|
25
|
+
return (0, print_utils_1.group)([
|
|
26
|
+
print.spaced(["addKw", "name"]),
|
|
27
|
+
(0, print_utils_1.indent)([print_utils_1.line, (0, constraint_1.printUnnamedConstraint)(print, node)]),
|
|
28
|
+
]);
|
|
24
29
|
},
|
|
25
30
|
alter_action_add_constraint_constraint_name: function (print) {
|
|
26
31
|
return print.spaced(["constraintKw", "ifNotExistsKw", "name"]);
|
package/dist/syntax/base.js
CHANGED
|
@@ -4,6 +4,12 @@ exports.baseMap = void 0;
|
|
|
4
4
|
exports.baseMap = {
|
|
5
5
|
/** cst-ignore: name */
|
|
6
6
|
keyword: function (print, node, path, options) {
|
|
7
|
+
var _a;
|
|
8
|
+
// Treat PostgreSQL "WITH TIME ZONE" as part of data type, not keyword,
|
|
9
|
+
// so skip keyword case conversion for it
|
|
10
|
+
if (((_a = path.parent) === null || _a === void 0 ? void 0 : _a.type) === "with_time_zone_data_type") {
|
|
11
|
+
return path.node.text;
|
|
12
|
+
}
|
|
7
13
|
switch (options.sqlKeywordCase) {
|
|
8
14
|
case "preserve":
|
|
9
15
|
return path.node.text;
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Doc } from "prettier";
|
|
2
|
+
import { AllConstraintNodes, AlterActionAddConstraint, Constraint } from "sql-parser-cst";
|
|
2
3
|
import { CstToDocMap } from "../CstToDocMap";
|
|
4
|
+
import { PrintFn } from "../PrintFn";
|
|
3
5
|
export declare const constraintMap: CstToDocMap<AllConstraintNodes>;
|
|
6
|
+
export declare const printUnnamedConstraint: <T>(print: PrintFn<Constraint<T>, "name" | "modifiers" | "constraint"> | PrintFn<AlterActionAddConstraint, "name" | "modifiers" | "constraint" | "addKw">, node: AlterActionAddConstraint | Constraint<T>) => Doc;
|
|
@@ -9,7 +9,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
9
9
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.constraintMap = void 0;
|
|
12
|
+
exports.printUnnamedConstraint = exports.constraintMap = void 0;
|
|
13
13
|
var print_utils_1 = require("../print_utils");
|
|
14
14
|
exports.constraintMap = {
|
|
15
15
|
/** cst-ignore: constraint, modifiers */
|
|
@@ -17,11 +17,11 @@ exports.constraintMap = {
|
|
|
17
17
|
if (node.name) {
|
|
18
18
|
return (0, print_utils_1.group)([
|
|
19
19
|
print("name"),
|
|
20
|
-
(0, print_utils_1.indent)([print_utils_1.line, printUnnamedConstraint(print, node)]),
|
|
20
|
+
(0, print_utils_1.indent)([print_utils_1.line, (0, exports.printUnnamedConstraint)(print, node)]),
|
|
21
21
|
]);
|
|
22
22
|
}
|
|
23
23
|
else {
|
|
24
|
-
return printUnnamedConstraint(print, node);
|
|
24
|
+
return (0, exports.printUnnamedConstraint)(print, node);
|
|
25
25
|
}
|
|
26
26
|
},
|
|
27
27
|
constraint_name: function (print) { return print.spaced(["constraintKw", "name"]); },
|
|
@@ -113,3 +113,4 @@ var printUnnamedConstraint = function (print, node) {
|
|
|
113
113
|
return print("constraint");
|
|
114
114
|
}
|
|
115
115
|
};
|
|
116
|
+
exports.printUnnamedConstraint = printUnnamedConstraint;
|
package/dist/syntax/data_type.js
CHANGED
|
@@ -6,8 +6,10 @@ var print_utils_1 = require("../print_utils");
|
|
|
6
6
|
exports.dataTypeMap = {
|
|
7
7
|
// print single-word types as `TYPE(10)` and multi-word types as `MY TYPE (10)`
|
|
8
8
|
named_data_type: function (print, node) {
|
|
9
|
-
return ((
|
|
9
|
+
return (isMultiWordTypeName(node) ? print.spaced : print)(["name", "params"]);
|
|
10
10
|
},
|
|
11
|
+
data_type_identifier: function (print) { return print.spaced("name"); },
|
|
12
|
+
setof_data_type: function (print) { return print.spaced(["setofKw", "dataType"]); },
|
|
11
13
|
array_data_type: function (print) { return print(["dataType", "bounds"]); },
|
|
12
14
|
array_bounds: function (print) { return ["[", print("bounds"), "]"]; },
|
|
13
15
|
with_time_zone_data_type: function (print) {
|
|
@@ -22,3 +24,7 @@ exports.dataTypeMap = {
|
|
|
22
24
|
},
|
|
23
25
|
table_data_type: function (print) { return print.spaced(["tableKw", "columns"]); },
|
|
24
26
|
};
|
|
27
|
+
function isMultiWordTypeName(node) {
|
|
28
|
+
return ((0, utils_1.isArray)(node.name) ||
|
|
29
|
+
(node.name.type === "data_type_identifier" && (0, utils_1.isArray)(node.name.name)));
|
|
30
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-sql-cst",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Prettier plugin for SQL",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Barna Magyarkuti <bmagyarkuti@gmail.com>",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"prettier": "^3.0.3",
|
|
40
|
-
"sql-parser-cst": "^0.
|
|
40
|
+
"sql-parser-cst": "^0.36.1"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/jest": "^29.2.5",
|