prettier-plugin-sql-cst 0.11.7 → 0.12.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 +48 -7
- package/dist/index.js +2 -0
- package/dist/options.d.ts +1 -0
- package/dist/options.js +7 -0
- package/dist/syntax/alter_action.js +40 -0
- package/dist/syntax/data_type.js +1 -1
- package/dist/syntax/domain.d.ts +3 -0
- package/dist/syntax/domain.js +20 -0
- package/dist/syntax/expr.js +5 -1
- package/dist/syntax/function.js +0 -2
- package/dist/syntax/transformMap.js +4 -1
- package/dist/syntax/type.d.ts +3 -0
- package/dist/syntax/type.js +23 -0
- package/dist/syntax/unsupported_grammar.d.ts +3 -0
- package/dist/syntax/unsupported_grammar.js +6 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -60,6 +60,10 @@ Install it as any other Prettier plugin:
|
|
|
60
60
|
|
|
61
61
|
```
|
|
62
62
|
npm install --save-dev prettier prettier-plugin-sql-cst
|
|
63
|
+
# or
|
|
64
|
+
pnpm add --save-dev prettier prettier-plugin-sql-cst
|
|
65
|
+
# or
|
|
66
|
+
yarn add --dev prettier prettier-plugin-sql-cst
|
|
63
67
|
```
|
|
64
68
|
|
|
65
69
|
Then use it on SQL files through Prettier command line tool or Prettier extension
|
|
@@ -72,10 +76,11 @@ By default the plugin will determine SQL dialect based on file extension:
|
|
|
72
76
|
- `.sql` or `.sqlite` - SQLite
|
|
73
77
|
- `.bigquery` - BigQuery
|
|
74
78
|
|
|
75
|
-
You can override this behavior with a prettier configuration:
|
|
79
|
+
You can override this behavior with a prettier configuration in `.prettierrc.json` file:
|
|
76
80
|
|
|
77
81
|
```json
|
|
78
82
|
{
|
|
83
|
+
"plugins": ["prettier-plugin-sql-cst"],
|
|
79
84
|
"overrides": [
|
|
80
85
|
{
|
|
81
86
|
"files": ["*.sql"],
|
|
@@ -85,6 +90,22 @@ You can override this behavior with a prettier configuration:
|
|
|
85
90
|
}
|
|
86
91
|
```
|
|
87
92
|
|
|
93
|
+
Or you could also store it inside your `package.json`:
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
{
|
|
97
|
+
"prettier": {
|
|
98
|
+
"plugins": ["prettier-plugin-sql-cst"],
|
|
99
|
+
"overrides": [
|
|
100
|
+
{
|
|
101
|
+
"files": ["*.sql"],
|
|
102
|
+
"options": { "parser": "bigquery" }
|
|
103
|
+
}
|
|
104
|
+
]
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
88
109
|
The plugin provides the following parsers:
|
|
89
110
|
|
|
90
111
|
- `sqlite`
|
|
@@ -98,11 +119,29 @@ The plugin provides the following parsers:
|
|
|
98
119
|
The standard Prettier options [printWidth][], [tabWidth][], [useTabs][] apply.
|
|
99
120
|
There are also some SQL-specific options:
|
|
100
121
|
|
|
101
|
-
| API Option
|
|
102
|
-
|
|
|
103
|
-
| `sqlKeywordCase`
|
|
104
|
-
| `sqlParamTypes`
|
|
105
|
-
| `sqlCanonicalSyntax`
|
|
122
|
+
| API Option | Default | Description |
|
|
123
|
+
| ----------------------------- | :-----: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
124
|
+
| `sqlKeywordCase` | `upper` | Converts 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
|
+
| `sqlParamTypes` | `[]` | Array of bound parameter types: `?`, `?nr`, `$nr`, `:name`, `@name`, `$name`. |
|
|
126
|
+
| `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) |
|
|
127
|
+
| `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. |
|
|
128
|
+
|
|
129
|
+
## Usage inside VSCode
|
|
130
|
+
|
|
131
|
+
To use this plugin inside VSCode,
|
|
132
|
+
install the [Prettier VSCode](https://github.com/prettier/prettier-vscode?tab=readme-ov-file#installation) extension.
|
|
133
|
+
|
|
134
|
+
Follow [Prettier VSCode docs](https://github.com/prettier/prettier-vscode?tab=readme-ov-file#default-formatter)
|
|
135
|
+
to configure it as the default formatter.
|
|
136
|
+
|
|
137
|
+
You might also need to configure
|
|
138
|
+
[prettier.documentSelectors](https://github.com/prettier/prettier-vscode?tab=readme-ov-file#prettierdocumentselectors)
|
|
139
|
+
to enable Prettier for `*.sql` files.
|
|
140
|
+
|
|
141
|
+
To see what Prettier is, or is not doing - open the VSCode Output window and select the Prettier dropdown.
|
|
142
|
+
On format, the window should show your `inferredParser`. It should reconfirm that by showing `"parser": "sqlite"`
|
|
143
|
+
(or whichever perser you have configured inside your prettier config `overrides` section)
|
|
144
|
+
and `plugins` with the path to this package. If you don't see that part, Prettier not using this package.
|
|
106
145
|
|
|
107
146
|
## FAQ
|
|
108
147
|
|
|
@@ -135,7 +174,9 @@ The main limitation is that [the parser][sql-parser-cst] does not support full s
|
|
|
135
174
|
these dialects. One should expect the parser to crash for syntax that's more specific to
|
|
136
175
|
these dialects. But as long as the parsing succeeds, the formatting should also succeed.
|
|
137
176
|
Mainly one can expect the formatting of SELECT, INSERT, UPDATE, DELETE statements to work.
|
|
138
|
-
|
|
177
|
+
|
|
178
|
+
To overcome this limitation you can enable the `sqlAcceptUnsupportedGrammar` option,
|
|
179
|
+
which will make the plugin skip the SQL statements it doesn't recognize.
|
|
139
180
|
|
|
140
181
|
The specifics of the [SQL formatting style][STYLE_GUIDE] are still very much subject to change.
|
|
141
182
|
Though the general principles should be mostly in place by now.
|
package/dist/index.js
CHANGED
|
@@ -44,6 +44,8 @@ var createParser = function (dialect) { return ({
|
|
|
44
44
|
includeComments: true,
|
|
45
45
|
filename: options.filepath,
|
|
46
46
|
paramTypes: options.sqlParamTypes,
|
|
47
|
+
acceptUnsupportedGrammar: options
|
|
48
|
+
.sqlAcceptUnsupportedGrammar,
|
|
47
49
|
}), text, options);
|
|
48
50
|
},
|
|
49
51
|
astFormat: "sql-cst",
|
package/dist/options.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface SqlPluginOptions {
|
|
|
4
4
|
sqlKeywordCase: "preserve" | "upper" | "lower";
|
|
5
5
|
sqlParamTypes: NonNullable<CstParserOptions["paramTypes"]>;
|
|
6
6
|
sqlCanonicalSyntax: boolean;
|
|
7
|
+
sqlAcceptUnsupportedGrammar: boolean;
|
|
7
8
|
}
|
|
8
9
|
export type AllPrettierOptions<T = Node> = ParserOptions<T> & SqlPluginOptions;
|
|
9
10
|
export declare const options: SupportOptions;
|
package/dist/options.js
CHANGED
|
@@ -41,4 +41,11 @@ exports.options = {
|
|
|
41
41
|
description: "Enforces one true style of SQL syntax (adds and removes keywords)",
|
|
42
42
|
// Since 0.11.0
|
|
43
43
|
},
|
|
44
|
+
sqlAcceptUnsupportedGrammar: {
|
|
45
|
+
type: "boolean",
|
|
46
|
+
category: "SQL",
|
|
47
|
+
default: false,
|
|
48
|
+
description: "Skips formatting unsupported SQL statements instead of exiting with an error",
|
|
49
|
+
// Since 0.12.0
|
|
50
|
+
},
|
|
44
51
|
};
|
|
@@ -91,6 +91,46 @@ exports.alterActionMap = {
|
|
|
91
91
|
alter_action_attach_partition: function (print) {
|
|
92
92
|
return print.spaced(["attachPartitionKw", "index"]);
|
|
93
93
|
},
|
|
94
|
+
alter_action_add_enum_value: function (print) {
|
|
95
|
+
return print.spaced(["addValueKw", "ifNotExistsKw", "value", "position"]);
|
|
96
|
+
},
|
|
97
|
+
alter_action_add_enum_value_position: function (print) {
|
|
98
|
+
return print.spaced(["positionKw", "value"]);
|
|
99
|
+
},
|
|
100
|
+
alter_action_rename_enum_value: function (print) {
|
|
101
|
+
return print.spaced(["renameValueKw", "oldValue", "toKw", "newValue"]);
|
|
102
|
+
},
|
|
103
|
+
alter_action_add_attribute: function (print) {
|
|
104
|
+
return print.spaced([
|
|
105
|
+
"addAttributeKw",
|
|
106
|
+
"name",
|
|
107
|
+
"dataType",
|
|
108
|
+
"constraint",
|
|
109
|
+
"behaviorKw",
|
|
110
|
+
]);
|
|
111
|
+
},
|
|
112
|
+
alter_action_drop_attribute: function (print) {
|
|
113
|
+
return print.spaced(["dropAttributeKw", "ifExistsKw", "name", "behaviorKw"]);
|
|
114
|
+
},
|
|
115
|
+
alter_action_alter_attribute: function (print) {
|
|
116
|
+
return print.spaced([
|
|
117
|
+
"alterAttributeKw",
|
|
118
|
+
"name",
|
|
119
|
+
"setDataTypeKw",
|
|
120
|
+
"dataType",
|
|
121
|
+
"constraint",
|
|
122
|
+
"behaviorKw",
|
|
123
|
+
]);
|
|
124
|
+
},
|
|
125
|
+
alter_action_rename_attribute: function (print) {
|
|
126
|
+
return print.spaced([
|
|
127
|
+
"renameAttributeKw",
|
|
128
|
+
"oldName",
|
|
129
|
+
"toKw",
|
|
130
|
+
"newName",
|
|
131
|
+
"behaviorKw",
|
|
132
|
+
]);
|
|
133
|
+
},
|
|
94
134
|
// ALTER COLUMN
|
|
95
135
|
alter_action_alter_column: function (print) {
|
|
96
136
|
return (0, print_utils_1.group)([
|
package/dist/syntax/data_type.js
CHANGED
|
@@ -6,7 +6,7 @@ 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 ((0, utils_1.isArray)(node.
|
|
9
|
+
return ((0, utils_1.isArray)(node.name) ? print.spaced : print)(["name", "params"]);
|
|
10
10
|
},
|
|
11
11
|
array_data_type: function (print) { return print(["dataType", "bounds"]); },
|
|
12
12
|
array_bounds: function (print) { return ["[", print("bounds"), "]"]; },
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.domainMap = void 0;
|
|
4
|
+
var print_utils_1 = require("../print_utils");
|
|
5
|
+
exports.domainMap = {
|
|
6
|
+
create_domain_stmt: function (print, node) {
|
|
7
|
+
return (0, print_utils_1.group)([
|
|
8
|
+
print.spaced(["createDomainKw", "name", "asKw", "dataType"]),
|
|
9
|
+
node.constraints.length > 0
|
|
10
|
+
? (0, print_utils_1.indent)([print.dynamicLine(), (0, print_utils_1.join)(print_utils_1.line, print("constraints"))])
|
|
11
|
+
: [],
|
|
12
|
+
]);
|
|
13
|
+
},
|
|
14
|
+
alter_domain_stmt: function (print, node) {
|
|
15
|
+
return (0, print_utils_1.group)(print.spaced(["alterDomainKw", "name", "action"]));
|
|
16
|
+
},
|
|
17
|
+
drop_domain_stmt: function (print) {
|
|
18
|
+
return (0, print_utils_1.group)(print.spaced(["dropDomainKw", "ifExistsKw", "domains", "behaviorKw"]));
|
|
19
|
+
},
|
|
20
|
+
};
|
package/dist/syntax/expr.js
CHANGED
|
@@ -128,7 +128,11 @@ exports.exprMap = {
|
|
|
128
128
|
cast_arg: function (print) { return print.spaced(["expr", "asKw", "dataType", "format"]); },
|
|
129
129
|
cast_format: function (print) { return print.spaced(["formatKw", "string", "timezone"]); },
|
|
130
130
|
cast_format_timezone: function (print) { return print.spaced(["atTimeZoneKw", "timezone"]); },
|
|
131
|
-
cast_operator_expr: function (print) { return [
|
|
131
|
+
cast_operator_expr: function (print) { return [
|
|
132
|
+
print("left"),
|
|
133
|
+
print("operator"),
|
|
134
|
+
print("right"),
|
|
135
|
+
]; },
|
|
132
136
|
string_with_charset: function (print) { return ["_", print(["charset", "string"])]; },
|
|
133
137
|
raise_expr: function (print) { return print(["raiseKw", "args"]); },
|
|
134
138
|
raise_expr_type: function (print) { return print("typeKw"); },
|
package/dist/syntax/function.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.functionMap = void 0;
|
|
|
4
4
|
var print_utils_1 = require("../print_utils");
|
|
5
5
|
var node_utils_1 = require("../node_utils");
|
|
6
6
|
exports.functionMap = {
|
|
7
|
-
/** cst-ignore: behaviorKw */
|
|
8
7
|
create_function_stmt: function (print, node) { return [
|
|
9
8
|
print.spaced([
|
|
10
9
|
"createKw",
|
|
@@ -65,7 +64,6 @@ exports.functionMap = {
|
|
|
65
64
|
]);
|
|
66
65
|
},
|
|
67
66
|
reset_parameter_clause: function (print) { return print.spaced(["resetKw", "name"]); },
|
|
68
|
-
/** cst-ignore: name */
|
|
69
67
|
reset_all_parameters_clause: function (print) { return print.spaced("resetAllKw"); },
|
|
70
68
|
};
|
|
71
69
|
var hasOnlyAsClause = function (node) {
|
|
@@ -22,6 +22,7 @@ var create_table_1 = require("./create_table");
|
|
|
22
22
|
var data_type_1 = require("./data_type");
|
|
23
23
|
var dcl_1 = require("./dcl");
|
|
24
24
|
var delete_1 = require("./delete");
|
|
25
|
+
var domain_1 = require("./domain");
|
|
25
26
|
var drop_table_1 = require("./drop_table");
|
|
26
27
|
var explain_1 = require("./explain");
|
|
27
28
|
var expr_1 = require("./expr");
|
|
@@ -43,10 +44,12 @@ var sequence_1 = require("./sequence");
|
|
|
43
44
|
var transaction_1 = require("./transaction");
|
|
44
45
|
var trigger_1 = require("./trigger");
|
|
45
46
|
var truncate_1 = require("./truncate");
|
|
47
|
+
var type_1 = require("./type");
|
|
48
|
+
var unsupported_grammar_1 = require("./unsupported_grammar");
|
|
46
49
|
var update_1 = require("./update");
|
|
47
50
|
var view_1 = require("./view");
|
|
48
51
|
var bigquery_1 = require("./dialects/bigquery");
|
|
49
52
|
var mysql_1 = require("./dialects/mysql");
|
|
50
53
|
var sqlite_1 = require("./dialects/sqlite");
|
|
51
54
|
var postgresql_1 = require("./dialects/postgresql");
|
|
52
|
-
exports.transformMap = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, alias_1.aliasMap), alter_action_1.alterActionMap), alter_table_1.alterTableMap), analyze_1.analyzeMap), base_1.baseMap), constraint_1.constraintMap), create_table_1.createTableMap), data_type_1.dataTypeMap), dcl_1.dclMap), delete_1.deleteMap), drop_table_1.dropTableMap), explain_1.explainMap), expr_1.exprMap), frame_1.frameMap), function_1.functionMap), index_1.indexMap), insert_1.insertMap), merge_1.mergeMap), other_clauses_1.otherClausesMap), prepared_statements_1.preparedStatementsMap), proc_clause_1.procClauseMap), procedural_language_1.proceduralLanguageMap), procedure_1.procedureMap), program_1.programMap), rename_table_1.renameTableMap), schema_1.schemaMap), select_1.selectMap), sequence_1.sequenceMap), transaction_1.transactionMap), trigger_1.triggerMap), truncate_1.truncateMap), update_1.updateMap), view_1.viewMap), bigquery_1.bigqueryMap), mysql_1.mysqlMap), postgresql_1.postgresqlMap), sqlite_1.sqliteMap);
|
|
55
|
+
exports.transformMap = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, alias_1.aliasMap), alter_action_1.alterActionMap), alter_table_1.alterTableMap), analyze_1.analyzeMap), base_1.baseMap), constraint_1.constraintMap), create_table_1.createTableMap), data_type_1.dataTypeMap), dcl_1.dclMap), delete_1.deleteMap), domain_1.domainMap), drop_table_1.dropTableMap), explain_1.explainMap), expr_1.exprMap), frame_1.frameMap), function_1.functionMap), index_1.indexMap), insert_1.insertMap), merge_1.mergeMap), other_clauses_1.otherClausesMap), prepared_statements_1.preparedStatementsMap), proc_clause_1.procClauseMap), procedural_language_1.proceduralLanguageMap), procedure_1.procedureMap), program_1.programMap), rename_table_1.renameTableMap), schema_1.schemaMap), select_1.selectMap), sequence_1.sequenceMap), transaction_1.transactionMap), trigger_1.triggerMap), truncate_1.truncateMap), type_1.typeMap), unsupported_grammar_1.unsupportedGrammarMap), update_1.updateMap), view_1.viewMap), bigquery_1.bigqueryMap), mysql_1.mysqlMap), postgresql_1.postgresqlMap), sqlite_1.sqliteMap);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.typeMap = void 0;
|
|
4
|
+
var print_utils_1 = require("../print_utils");
|
|
5
|
+
exports.typeMap = {
|
|
6
|
+
create_type_stmt: function (print) {
|
|
7
|
+
return (0, print_utils_1.group)([print.spaced(["createTypeKw", "name", "definition"])]);
|
|
8
|
+
},
|
|
9
|
+
composite_type_definition: function (print) {
|
|
10
|
+
return (0, print_utils_1.group)(print.spaced(["asKw", "columns"]));
|
|
11
|
+
},
|
|
12
|
+
enum_type_definition: function (print) { return (0, print_utils_1.group)(print.spaced(["asEnumKw", "values"])); },
|
|
13
|
+
alter_type_stmt: function (print, node) {
|
|
14
|
+
return (0, print_utils_1.group)([
|
|
15
|
+
print.spaced(["alterTypeKw", "name"]),
|
|
16
|
+
print.dynamicLine(),
|
|
17
|
+
print("actions"),
|
|
18
|
+
]);
|
|
19
|
+
},
|
|
20
|
+
drop_type_stmt: function (print) {
|
|
21
|
+
return (0, print_utils_1.group)(print.spaced(["dropTypeKw", "ifExistsKw", "types", "behaviorKw"]));
|
|
22
|
+
},
|
|
23
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-sql-cst",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Prettier plugin for SQL",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Bowen Parnell <bparnell@4tel.com.au>",
|
|
@@ -36,10 +36,11 @@
|
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"prettier": "^3.0.3",
|
|
39
|
-
"sql-parser-cst": "^0.
|
|
39
|
+
"sql-parser-cst": "^0.30.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/jest": "^29.2.5",
|
|
43
|
+
"@types/node": "^22.9.0",
|
|
43
44
|
"chalk": "^4.1.2",
|
|
44
45
|
"dedent-js": "^1.0.1",
|
|
45
46
|
"jest": "^29.7.0",
|