prettier-plugin-sql-cst 0.20.0 → 0.22.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 +6 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.js +141 -61
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -119,6 +119,7 @@ The plugin provides the following parsers:
|
|
|
119
119
|
- `sqlite`
|
|
120
120
|
- `bigquery`
|
|
121
121
|
- `postgresql` (**experimental! expect crashes**)
|
|
122
|
+
- `plpgsql` (**experimental! expect crashes**)
|
|
122
123
|
- `mysql` (**experimental! expect crashes**)
|
|
123
124
|
- `mariadb` (**experimental! expect crashes**)
|
|
124
125
|
|
|
@@ -210,16 +211,12 @@ There are also several SQL-specific options:
|
|
|
210
211
|
|
|
211
212
|
_Since 0.13.0_
|
|
212
213
|
|
|
213
|
-
- **`
|
|
214
|
+
- **`sqlExperimentalPlpgsql`**: `boolean` (default `false`)
|
|
214
215
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
With this option enabled, it will skip over SQL statements it doesn't recognize, leaving them as-is.
|
|
216
|
+
When enabled, the plugin will attempt to format PL/pgSQL function bodies.
|
|
217
|
+
This might fail, as the underlying parser support for PL/pgSQL is incomplete.
|
|
218
218
|
|
|
219
|
-
|
|
220
|
-
`/* sql-parser-cst-enable */` instead.
|
|
221
|
-
|
|
222
|
-
_Since 0.12.0_
|
|
219
|
+
_Since 0.21.0_
|
|
223
220
|
|
|
224
221
|
## Usage inside VSCode
|
|
225
222
|
|
|
@@ -262,6 +259,7 @@ Currently this plugin supports two SQL dialects:
|
|
|
262
259
|
It also has experimental support for the following dialects:
|
|
263
260
|
|
|
264
261
|
- PostgreSQL
|
|
262
|
+
- PL/pgSQL
|
|
265
263
|
- MySQL
|
|
266
264
|
- MariaDB
|
|
267
265
|
|
package/dist/index.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ interface SqlPluginOptions {
|
|
|
10
10
|
sqlParamTypes: NonNullable<ParserOptions["paramTypes"]>;
|
|
11
11
|
sqlCanonicalSyntax: boolean;
|
|
12
12
|
sqlFinalSemicolon: boolean;
|
|
13
|
-
|
|
13
|
+
sqlExperimentalPlpgsql: boolean;
|
|
14
14
|
}
|
|
15
15
|
declare const options: SupportOptions;
|
|
16
16
|
|
package/dist/index.js
CHANGED
|
@@ -1082,8 +1082,20 @@ var otherClausesMap = {
|
|
|
1082
1082
|
cluster_by_clause: (print) => group([print.spaced("clusterByKw"), indent([line, print("columns")])]),
|
|
1083
1083
|
// RETURNING clause
|
|
1084
1084
|
returning_clause: (print) => group([print("returningKw"), indent([line, print("columns")])]),
|
|
1085
|
+
// INTO clause
|
|
1086
|
+
into_variables_clause: (print) => group([
|
|
1087
|
+
print.spaced(["intoKw", "strictKw"]),
|
|
1088
|
+
indent([line, print("variables")])
|
|
1089
|
+
]),
|
|
1090
|
+
as_clause: (print, node) => {
|
|
1091
|
+
if (isStringLiteral(node.expr) || isDynamicallyLoadedFunction(node.expr)) {
|
|
1092
|
+
return group(print.spaced(["asKw", "expr"]));
|
|
1093
|
+
}
|
|
1094
|
+
return group([print("asKw"), indent([hardline, print("expr")])]);
|
|
1095
|
+
},
|
|
1096
|
+
comma_clause: (print) => group([",", indent([line, print("expr")])]),
|
|
1085
1097
|
// WHERE CURRENT OF clause
|
|
1086
|
-
where_current_of_clause: (print) => print.spaced(["whereCurrentOfKw", "cursor"])
|
|
1098
|
+
where_current_of_clause: (print) => group(print.spaced(["whereCurrentOfKw", "cursor"]))
|
|
1087
1099
|
};
|
|
1088
1100
|
|
|
1089
1101
|
// src/syntax/rename_table.ts
|
|
@@ -1189,7 +1201,10 @@ var preparedStatementsMap = {
|
|
|
1189
1201
|
...print(["into", "using"]).map((clause) => group(clause))
|
|
1190
1202
|
])
|
|
1191
1203
|
),
|
|
1192
|
-
|
|
1204
|
+
execute_expr: (print) => group([
|
|
1205
|
+
print("executeKw"),
|
|
1206
|
+
indent([line, print.separated(line, ["expr", "using"])])
|
|
1207
|
+
]),
|
|
1193
1208
|
execute_using_clause: (print) => group([print("usingKw"), indent([line, print("values")])]),
|
|
1194
1209
|
// PREPARE
|
|
1195
1210
|
prepare_stmt: (print) => group(
|
|
@@ -1207,30 +1222,59 @@ var preparedStatementsMap = {
|
|
|
1207
1222
|
|
|
1208
1223
|
// src/syntax/procedural_language.ts
|
|
1209
1224
|
var proceduralLanguageMap = {
|
|
1225
|
+
// labels
|
|
1226
|
+
labeled_stmt: (print, node) => group([
|
|
1227
|
+
print.spaced(["beginLabel", "statement"]),
|
|
1228
|
+
node.endLabel ? [" ", print(["endLabel"])] : []
|
|
1229
|
+
]),
|
|
1230
|
+
colon_label: (print) => [print("label"), ":"],
|
|
1231
|
+
chevron_label: (print) => ["<<", print("label"), ">>"],
|
|
1210
1232
|
// BEGIN .. END
|
|
1211
1233
|
block_stmt: (print, node) => group([
|
|
1212
|
-
print
|
|
1213
|
-
|
|
1234
|
+
stripTrailingHardline(print("declare")),
|
|
1235
|
+
[node.declare ? hardline : [], print.spaced(["beginKw", "atomicKw"])],
|
|
1236
|
+
node.program.statements.length > 0 ? indent([hardline, stripTrailingHardline(print("program"))]) : print("program"),
|
|
1214
1237
|
node.exception ? [hardline, print("exception")] : [],
|
|
1215
1238
|
hardline,
|
|
1216
1239
|
print("endKw")
|
|
1217
1240
|
]),
|
|
1218
|
-
exception_clause: (print) => group([
|
|
1219
|
-
print.spaced(["exceptionKw", "whenKw", "condition", "thenKw"]),
|
|
1220
|
-
indent([hardline, stripTrailingHardline(print("program"))])
|
|
1221
|
-
]),
|
|
1222
|
-
error_category: (print) => print("errorKw"),
|
|
1223
1241
|
// DECLARE
|
|
1242
|
+
declare_clause: (print, node) => group([print("declareKw"), indent([hardline, print("program")])]),
|
|
1224
1243
|
declare_stmt: (print) => group(
|
|
1225
1244
|
join(" ", [
|
|
1226
|
-
print("declareKw"),
|
|
1245
|
+
...print.spaced(["declareKw"]),
|
|
1227
1246
|
group(print("names")),
|
|
1228
|
-
...print.spaced(["dataType", "
|
|
1247
|
+
...print.spaced(["constantKw", "dataType", "constraints", "init"])
|
|
1229
1248
|
])
|
|
1230
1249
|
),
|
|
1231
|
-
|
|
1250
|
+
declare_init: (print) => print.spaced(["operator", "expr"]),
|
|
1251
|
+
declare_alias_stmt: (print) => group(print.spaced(["newName", "aliasForKw", "oldName"])),
|
|
1252
|
+
// EXCEPTION
|
|
1253
|
+
exception_clause: (print, node) => {
|
|
1254
|
+
if (node.clauses.length === 1) {
|
|
1255
|
+
return group(print.spaced(["exceptionKw", "clauses"]));
|
|
1256
|
+
} else {
|
|
1257
|
+
return group([
|
|
1258
|
+
print("exceptionKw"),
|
|
1259
|
+
indent([
|
|
1260
|
+
hardline,
|
|
1261
|
+
stripTrailingHardline(print("clauses").map((doc2) => [doc2, hardline]))
|
|
1262
|
+
])
|
|
1263
|
+
]);
|
|
1264
|
+
}
|
|
1265
|
+
},
|
|
1266
|
+
exception_when_clause: (print, node) => group([
|
|
1267
|
+
join(" ", [print("whenKw"), group(print("condition")), print("thenKw")]),
|
|
1268
|
+
node.program.statements.length > 0 ? indent([hardline, stripTrailingHardline(print("program"))]) : print("program")
|
|
1269
|
+
]),
|
|
1270
|
+
error_bigquery: (print) => print("errorKw"),
|
|
1271
|
+
error_name: (print) => print("name"),
|
|
1272
|
+
error_sqlstate: (print) => group(print.spaced(["sqlstateKw", "code"])),
|
|
1273
|
+
error_format_string: (print, node) => group(print(["format", "args"])),
|
|
1232
1274
|
// SET
|
|
1233
1275
|
set_stmt: (print) => group(print.spaced(["setKw", "assignments"])),
|
|
1276
|
+
// assignment
|
|
1277
|
+
assignment_stmt: (print) => group(print.spaced(["target", "operator", "expr"])),
|
|
1234
1278
|
// IF
|
|
1235
1279
|
if_stmt: (print) => group(join(hardline, [...print("clauses"), print.spaced("endIfKw")])),
|
|
1236
1280
|
if_clause: (print) => [
|
|
@@ -1283,6 +1327,8 @@ var proceduralLanguageMap = {
|
|
|
1283
1327
|
hardline,
|
|
1284
1328
|
print.spaced("endWhileKw")
|
|
1285
1329
|
]),
|
|
1330
|
+
// WHILE .. LOOP .. END LOOP
|
|
1331
|
+
while_loop_stmt: (print) => group(print.spaced(["whileKw", "condition", "loop"])),
|
|
1286
1332
|
// FOR .. IN
|
|
1287
1333
|
for_stmt: (print) => group([
|
|
1288
1334
|
print.spaced(["forKw", "left", "inKw", "right", "doKw"]),
|
|
@@ -1290,26 +1336,53 @@ var proceduralLanguageMap = {
|
|
|
1290
1336
|
hardline,
|
|
1291
1337
|
print.spaced("endForKw")
|
|
1292
1338
|
]),
|
|
1293
|
-
//
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
"
|
|
1299
|
-
|
|
1300
|
-
|
|
1339
|
+
// FOR .. IN range LOOP .. END LOOP
|
|
1340
|
+
for_loop_stmt: (print) => group(print.spaced(["forKw", "left", "inKw", "right", "loop"])),
|
|
1341
|
+
for_range: (print) => group([
|
|
1342
|
+
print.spaced(["reverseKw", "from"]),
|
|
1343
|
+
"..",
|
|
1344
|
+
print.spaced(["to", "by"])
|
|
1345
|
+
]),
|
|
1346
|
+
for_by_clause: (print) => group(print.spaced(["byKw", "expr"])),
|
|
1347
|
+
// FOREACH
|
|
1348
|
+
foreach_stmt: (print) => group([
|
|
1349
|
+
print.spaced([
|
|
1350
|
+
"foreachKw",
|
|
1351
|
+
"left",
|
|
1352
|
+
"slice",
|
|
1353
|
+
"inArrayKw",
|
|
1354
|
+
"right",
|
|
1355
|
+
"loop"
|
|
1356
|
+
])
|
|
1301
1357
|
]),
|
|
1358
|
+
foreach_slice: (print) => group(print.spaced(["sliceKw", "count"])),
|
|
1359
|
+
// BREAK/CONTINUE
|
|
1360
|
+
break_stmt: (print) => group(print.spaced(["breakKw", "label", "when"])),
|
|
1361
|
+
continue_stmt: (print) => group(print.spaced(["continueKw", "label", "when"])),
|
|
1302
1362
|
// CALL
|
|
1303
1363
|
call_stmt: (print) => group(print.spaced(["callKw", "func"])),
|
|
1304
1364
|
// RETURN
|
|
1305
1365
|
return_stmt: (print) => group(print.spaced(["returnKw", "expr"])),
|
|
1366
|
+
return_next_stmt: (print) => group(print.spaced(["returnNextKw", "expr"])),
|
|
1367
|
+
return_query_stmt: (print, node) => {
|
|
1368
|
+
if (node.expr.type === "execute_expr") {
|
|
1369
|
+
return group([print.spaced("returnQueryKw"), " ", print("expr")]);
|
|
1370
|
+
} else {
|
|
1371
|
+
return group([print.spaced("returnQueryKw"), " ", indent(print("expr"))]);
|
|
1372
|
+
}
|
|
1373
|
+
},
|
|
1306
1374
|
// RAISE
|
|
1307
|
-
raise_stmt: (print) => group(
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
]
|
|
1375
|
+
raise_stmt: (print, node) => group([
|
|
1376
|
+
print.spaced(["raiseKw", "level", "error"]),
|
|
1377
|
+
node.using ? indent([line, print("using")]) : []
|
|
1378
|
+
]),
|
|
1379
|
+
raise_level: (print) => print("levelKw"),
|
|
1380
|
+
raise_using_clause: (print) => group([print("usingKw"), indent([line, print("options")])]),
|
|
1381
|
+
raise_option_element: (print) => group(print.spaced(["nameKw", "operator", "value"])),
|
|
1382
|
+
// ASSERT
|
|
1383
|
+
assert_stmt: (print) => group(print.spaced(["assertKw", "condition", "message"])),
|
|
1384
|
+
// NULL
|
|
1385
|
+
null_stmt: (print) => group(print.spaced(["nullKw"]))
|
|
1313
1386
|
};
|
|
1314
1387
|
|
|
1315
1388
|
// src/syntax/procedure.ts
|
|
@@ -1356,12 +1429,6 @@ var procClauseMap = {
|
|
|
1356
1429
|
returns_clause: (print) => print.spaced(["returnsKw", "dataType"]),
|
|
1357
1430
|
determinism_clause: (print) => print.spaced("deterministicKw"),
|
|
1358
1431
|
language_clause: (print) => print.spaced(["languageKw", "name"]),
|
|
1359
|
-
as_clause: (print, node) => {
|
|
1360
|
-
if (isStringLiteral(node.expr) || isDynamicallyLoadedFunction(node.expr)) {
|
|
1361
|
-
return print.spaced(["asKw", "expr"]);
|
|
1362
|
-
}
|
|
1363
|
-
return [print("asKw"), indent([hardline, print("expr")])];
|
|
1364
|
-
},
|
|
1365
1432
|
with_connection_clause: (print) => print.spaced(["withConnectionKw", "connection"])
|
|
1366
1433
|
};
|
|
1367
1434
|
|
|
@@ -1642,7 +1709,6 @@ var selectMap = {
|
|
|
1642
1709
|
},
|
|
1643
1710
|
// INTO clause
|
|
1644
1711
|
into_table_clause: (print) => print.spaced(["intoKw", "kind", "tableKw", "name"]),
|
|
1645
|
-
into_variables_clause: (print) => group([print("intoKw"), indent([line, print("variables")])]),
|
|
1646
1712
|
into_dumpfile_clause: (print) => print.spaced(["intoDumpfileKw", "filename"]),
|
|
1647
1713
|
into_outfile_clause: (print, node) => {
|
|
1648
1714
|
const hasOptions = node.charset || node.fields || node.lines;
|
|
@@ -1839,11 +1905,6 @@ var typeMap = {
|
|
|
1839
1905
|
drop_type_stmt: (print) => group(print.spaced(["dropTypeKw", "ifExistsKw", "types", "behaviorKw"]))
|
|
1840
1906
|
};
|
|
1841
1907
|
|
|
1842
|
-
// src/syntax/unsupported_grammar.ts
|
|
1843
|
-
var unsupportedGrammarMap = {
|
|
1844
|
-
unsupported_grammar_stmt: (print) => group(print("text"))
|
|
1845
|
-
};
|
|
1846
|
-
|
|
1847
1908
|
// src/syntax/update.ts
|
|
1848
1909
|
var updateMap = {
|
|
1849
1910
|
update_stmt: (print) => group(join(print.dynamicLine(), print("clauses"))),
|
|
@@ -1989,8 +2050,6 @@ var bigqueryMap = {
|
|
|
1989
2050
|
print("actions")
|
|
1990
2051
|
])
|
|
1991
2052
|
),
|
|
1992
|
-
// ASSERT
|
|
1993
|
-
assert_stmt: (print) => group(print.spaced(["assertKw", "expr", "as"])),
|
|
1994
2053
|
// EXPORT
|
|
1995
2054
|
export_data_stmt: (print) => group(
|
|
1996
2055
|
join(hardline, [
|
|
@@ -2079,7 +2138,6 @@ var transformMap = {
|
|
|
2079
2138
|
...triggerMap,
|
|
2080
2139
|
...truncateMap,
|
|
2081
2140
|
...typeMap,
|
|
2082
|
-
...unsupportedGrammarMap,
|
|
2083
2141
|
...updateMap,
|
|
2084
2142
|
...viewMap,
|
|
2085
2143
|
...bigqueryMap,
|
|
@@ -2221,24 +2279,41 @@ var embedSql = (path, options2) => {
|
|
|
2221
2279
|
const node = path.node;
|
|
2222
2280
|
const parent = path.getParentNode(0);
|
|
2223
2281
|
const grandParent = path.getParentNode(1);
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2282
|
+
const pluginOptions = options2;
|
|
2283
|
+
if (isStringLiteral(node) && isAsClause(parent) && (isCreateFunctionStmt(grandParent) || isCreateProcedureStmt(grandParent))) {
|
|
2284
|
+
if (grandParent.clauses.some(isSqlLanguageClause)) {
|
|
2285
|
+
return async (textToDoc) => {
|
|
2286
|
+
const quote = detectQuote(node);
|
|
2287
|
+
if (!quote) {
|
|
2288
|
+
return void 0;
|
|
2289
|
+
}
|
|
2290
|
+
const sql = await textToDoc(node.value, pluginOptions);
|
|
2291
|
+
return [
|
|
2292
|
+
quote,
|
|
2293
|
+
indent([hardline, stripTrailingHardline(sql)]),
|
|
2294
|
+
hardline,
|
|
2295
|
+
quote
|
|
2296
|
+
];
|
|
2297
|
+
};
|
|
2298
|
+
}
|
|
2299
|
+
if (grandParent.clauses.some(isPlpgsqlLanguageClause) && pluginOptions.sqlExperimentalPlpgsql) {
|
|
2300
|
+
return async (textToDoc) => {
|
|
2301
|
+
const quote = detectQuote(node);
|
|
2302
|
+
if (!quote) {
|
|
2303
|
+
return void 0;
|
|
2304
|
+
}
|
|
2305
|
+
const sql = await textToDoc(node.value, {
|
|
2306
|
+
...pluginOptions,
|
|
2307
|
+
parser: "plpgsql"
|
|
2308
|
+
});
|
|
2309
|
+
return [quote, [hardline, stripTrailingHardline(sql)], hardline, quote];
|
|
2310
|
+
};
|
|
2311
|
+
}
|
|
2238
2312
|
}
|
|
2239
2313
|
return null;
|
|
2240
2314
|
};
|
|
2241
2315
|
var isSqlLanguageClause = (clause) => isLanguageClause(clause) && clause.name.name.toLowerCase() === "sql";
|
|
2316
|
+
var isPlpgsqlLanguageClause = (clause) => isLanguageClause(clause) && clause.name.name.toLowerCase() === "plpgsql";
|
|
2242
2317
|
var detectQuote = (node) => {
|
|
2243
2318
|
const match = node.text.match(/^('|\$[^$]*\$)/);
|
|
2244
2319
|
const quote = match?.[1];
|
|
@@ -2568,12 +2643,12 @@ var options = {
|
|
|
2568
2643
|
description: "Enforces a semicolon at the end of last SQL statement"
|
|
2569
2644
|
// Since 0.13.0
|
|
2570
2645
|
},
|
|
2571
|
-
|
|
2646
|
+
sqlExperimentalPlpgsql: {
|
|
2572
2647
|
type: "boolean",
|
|
2573
2648
|
category: "SQL",
|
|
2574
2649
|
default: false,
|
|
2575
|
-
description: "
|
|
2576
|
-
// Since 0.
|
|
2650
|
+
description: "Enables formatting for PL/pgSQL in PostgreSQL function bodies"
|
|
2651
|
+
// Since 0.21.0
|
|
2577
2652
|
}
|
|
2578
2653
|
};
|
|
2579
2654
|
|
|
@@ -2603,6 +2678,11 @@ var languages = [
|
|
|
2603
2678
|
extensions: [],
|
|
2604
2679
|
name: "Experimental PostgreSQL SQL",
|
|
2605
2680
|
parsers: ["postgresql"]
|
|
2681
|
+
},
|
|
2682
|
+
{
|
|
2683
|
+
extensions: [],
|
|
2684
|
+
name: "Experimental PL/pgSQL",
|
|
2685
|
+
parsers: ["plpgsql"]
|
|
2606
2686
|
}
|
|
2607
2687
|
];
|
|
2608
2688
|
var createParser = (dialect) => ({
|
|
@@ -2612,8 +2692,7 @@ var createParser = (dialect) => ({
|
|
|
2612
2692
|
includeRange: true,
|
|
2613
2693
|
includeComments: true,
|
|
2614
2694
|
filename: options2.filepath,
|
|
2615
|
-
paramTypes: options2.sqlParamTypes
|
|
2616
|
-
acceptUnsupportedGrammar: options2.sqlAcceptUnsupportedGrammar
|
|
2695
|
+
paramTypes: options2.sqlParamTypes
|
|
2617
2696
|
}),
|
|
2618
2697
|
text,
|
|
2619
2698
|
options2
|
|
@@ -2627,7 +2706,8 @@ var parsers = {
|
|
|
2627
2706
|
bigquery: createParser("bigquery"),
|
|
2628
2707
|
mysql: createParser("mysql"),
|
|
2629
2708
|
mariadb: createParser("mariadb"),
|
|
2630
|
-
postgresql: createParser("postgresql")
|
|
2709
|
+
postgresql: createParser("postgresql"),
|
|
2710
|
+
plpgsql: createParser("plpgsql")
|
|
2631
2711
|
};
|
|
2632
2712
|
var printers = {
|
|
2633
2713
|
"sql-cst": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-sql-cst",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"description": "Prettier plugin for SQL",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Barna Magyarkuti <bmagyarkuti@gmail.com>",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"prettier": "^3.0.3",
|
|
42
|
-
"sql-parser-cst": "^0.
|
|
42
|
+
"sql-parser-cst": "^0.42.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/jest": "^30.0.0",
|