prettier-plugin-sql-cst 0.19.3 → 0.21.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
@@ -221,6 +221,13 @@ There are also several SQL-specific options:
221
221
 
222
222
  _Since 0.12.0_
223
223
 
224
+ - **`sqlExperimentalPlpgsql`**: `boolean` (default `false`)
225
+
226
+ When enabled, the plugin will attempt to format PL/pgSQL function bodies.
227
+ This might fail, as the underlying parser support for PL/pgSQL is incomplete.
228
+
229
+ _Since 0.21.0_
230
+
224
231
  ## Usage inside VSCode
225
232
 
226
233
  To use this plugin inside VSCode,
package/dist/index.d.ts CHANGED
@@ -11,6 +11,7 @@ interface SqlPluginOptions {
11
11
  sqlCanonicalSyntax: boolean;
12
12
  sqlFinalSemicolon: boolean;
13
13
  sqlAcceptUnsupportedGrammar: boolean;
14
+ sqlExperimentalPlpgsql: boolean;
14
15
  }
15
16
  declare const options: SupportOptions;
16
17
 
package/dist/index.js CHANGED
@@ -1082,8 +1082,15 @@ 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
+ as_clause: (print, node) => {
1086
+ if (isStringLiteral(node.expr) || isDynamicallyLoadedFunction(node.expr)) {
1087
+ return group(print.spaced(["asKw", "expr"]));
1088
+ }
1089
+ return group([print("asKw"), indent([hardline, print("expr")])]);
1090
+ },
1091
+ comma_clause: (print) => group([",", indent([line, print("expr")])]),
1085
1092
  // WHERE CURRENT OF clause
1086
- where_current_of_clause: (print) => print.spaced(["whereCurrentOfKw", "cursor"])
1093
+ where_current_of_clause: (print) => group(print.spaced(["whereCurrentOfKw", "cursor"]))
1087
1094
  };
1088
1095
 
1089
1096
  // src/syntax/rename_table.ts
@@ -1189,6 +1196,10 @@ var preparedStatementsMap = {
1189
1196
  ...print(["into", "using"]).map((clause) => group(clause))
1190
1197
  ])
1191
1198
  ),
1199
+ execute_expr: (print) => group([
1200
+ print("executeKw"),
1201
+ indent([line, print.separated(line, ["expr", "using"])])
1202
+ ]),
1192
1203
  execute_into_clause: (print) => print.spaced(["intoKw", "variables"]),
1193
1204
  execute_using_clause: (print) => group([print("usingKw"), indent([line, print("values")])]),
1194
1205
  // PREPARE
@@ -1207,30 +1218,61 @@ var preparedStatementsMap = {
1207
1218
 
1208
1219
  // src/syntax/procedural_language.ts
1209
1220
  var proceduralLanguageMap = {
1221
+ // labels
1222
+ labeled_stmt: (print, node) => group([
1223
+ print.spaced(["beginLabel", "statement"]),
1224
+ node.endLabel ? [" ", print(["endLabel"])] : []
1225
+ ]),
1226
+ colon_label: (print) => [print("label"), ":"],
1227
+ chevron_label: (print) => ["<<", print("label"), ">>"],
1210
1228
  // BEGIN .. END
1211
1229
  block_stmt: (print, node) => group([
1212
- print.spaced(["beginKw", "atomicKw"]),
1213
- indent([hardline, stripTrailingHardline(print("program"))]),
1230
+ stripTrailingHardline(print("declareClause")),
1231
+ [
1232
+ node.declareClause ? hardline : [],
1233
+ print.spaced(["beginKw", "atomicKw"])
1234
+ ],
1235
+ node.program.statements.length > 0 ? indent([hardline, stripTrailingHardline(print("program"))]) : print("program"),
1214
1236
  node.exception ? [hardline, print("exception")] : [],
1215
1237
  hardline,
1216
1238
  print("endKw")
1217
1239
  ]),
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
1240
  // DECLARE
1241
+ declare_clause: (print, node) => group([print("declareKw"), indent([hardline, print("program")])]),
1224
1242
  declare_stmt: (print) => group(
1225
1243
  join(" ", [
1226
- print("declareKw"),
1244
+ ...print.spaced(["declareKw"]),
1227
1245
  group(print("names")),
1228
- ...print.spaced(["dataType", "default"])
1246
+ ...print.spaced(["constantKw", "dataType", "constraints", "init"])
1229
1247
  ])
1230
1248
  ),
1231
- declare_default: (print) => print.spaced(["defaultKw", "expr"]),
1249
+ declare_init: (print) => print.spaced(["operator", "expr"]),
1250
+ // EXCEPTION
1251
+ exception_clause: (print, node) => {
1252
+ if (node.clauses.length === 1) {
1253
+ return group(print.spaced(["exceptionKw", "clauses"]));
1254
+ } else {
1255
+ return group([
1256
+ print("exceptionKw"),
1257
+ indent([
1258
+ hardline,
1259
+ stripTrailingHardline(print("clauses").map((doc2) => [doc2, hardline]))
1260
+ ])
1261
+ ]);
1262
+ }
1263
+ },
1264
+ exception_when_clause: (print, node) => group([
1265
+ join(" ", [print("whenKw"), group(print("condition")), print("thenKw")]),
1266
+ node.program.statements.length > 0 ? indent([hardline, stripTrailingHardline(print("program"))]) : print("program")
1267
+ ]),
1268
+ error_bigquery: (print) => print("errorKw"),
1269
+ error_name: (print) => print("name"),
1270
+ error_sqlstate: (print) => group(print.spaced(["sqlstateKw", "code"])),
1271
+ error_format_string: (print, node) => group(print(["format", "args"])),
1232
1272
  // SET
1233
1273
  set_stmt: (print) => group(print.spaced(["setKw", "assignments"])),
1274
+ // assignment
1275
+ assignment_stmt: (print) => group(print.spaced(["target", "operator", "expr"])),
1234
1276
  // IF
1235
1277
  if_stmt: (print) => group(join(hardline, [...print("clauses"), print.spaced("endIfKw")])),
1236
1278
  if_clause: (print) => [
@@ -1283,6 +1325,8 @@ var proceduralLanguageMap = {
1283
1325
  hardline,
1284
1326
  print.spaced("endWhileKw")
1285
1327
  ]),
1328
+ // WHILE .. LOOP .. END LOOP
1329
+ while_loop_stmt: (print) => group(print.spaced(["whileKw", "condition", "loop"])),
1286
1330
  // FOR .. IN
1287
1331
  for_stmt: (print) => group([
1288
1332
  print.spaced(["forKw", "left", "inKw", "right", "doKw"]),
@@ -1290,26 +1334,53 @@ var proceduralLanguageMap = {
1290
1334
  hardline,
1291
1335
  print.spaced("endForKw")
1292
1336
  ]),
1293
- // BREAK/CONTINUE
1294
- break_stmt: (print) => group(print.spaced(["breakKw", "label"])),
1295
- continue_stmt: (print) => group(print.spaced(["continueKw", "label"])),
1296
- labeled_stmt: (print, node) => group([
1297
- print("beginLabel"),
1298
- ": ",
1299
- print("statement"),
1300
- node.endLabel ? [" ", print(["endLabel"])] : []
1337
+ // FOR .. IN range LOOP .. END LOOP
1338
+ for_loop_stmt: (print) => group(print.spaced(["forKw", "left", "inKw", "right", "loop"])),
1339
+ for_range: (print) => group([
1340
+ print.spaced(["reverseKw", "from"]),
1341
+ "..",
1342
+ print.spaced(["to", "by"])
1301
1343
  ]),
1344
+ for_by_clause: (print) => group(print.spaced(["byKw", "expr"])),
1345
+ // FOREACH
1346
+ foreach_stmt: (print) => group([
1347
+ print.spaced([
1348
+ "foreachKw",
1349
+ "left",
1350
+ "slice",
1351
+ "inArrayKw",
1352
+ "right",
1353
+ "loop"
1354
+ ])
1355
+ ]),
1356
+ foreach_slice: (print) => group(print.spaced(["sliceKw", "count"])),
1357
+ // BREAK/CONTINUE
1358
+ break_stmt: (print) => group(print.spaced(["breakKw", "label", "when"])),
1359
+ continue_stmt: (print) => group(print.spaced(["continueKw", "label", "when"])),
1302
1360
  // CALL
1303
1361
  call_stmt: (print) => group(print.spaced(["callKw", "func"])),
1304
1362
  // RETURN
1305
1363
  return_stmt: (print) => group(print.spaced(["returnKw", "expr"])),
1364
+ return_next_stmt: (print) => group(print.spaced(["returnNextKw", "expr"])),
1365
+ return_query_stmt: (print, node) => {
1366
+ if (node.expr.type === "execute_expr") {
1367
+ return group([print.spaced("returnQueryKw"), " ", print("expr")]);
1368
+ } else {
1369
+ return group([print.spaced("returnQueryKw"), " ", indent(print("expr"))]);
1370
+ }
1371
+ },
1306
1372
  // RAISE
1307
- raise_stmt: (print) => group(print.spaced(["raiseKw", "message"])),
1308
- raise_message: (print) => [
1309
- print.spaced("usingMessageKw"),
1310
- " = ",
1311
- print("string")
1312
- ]
1373
+ raise_stmt: (print, node) => group([
1374
+ print.spaced(["raiseKw", "level", "error"]),
1375
+ node.using ? indent([line, print("using")]) : []
1376
+ ]),
1377
+ raise_level: (print) => print("levelKw"),
1378
+ raise_using_clause: (print) => group([print("usingKw"), indent([line, print("options")])]),
1379
+ raise_option_element: (print) => group(print.spaced(["nameKw", "operator", "value"])),
1380
+ // ASSERT
1381
+ assert_stmt: (print) => group(print.spaced(["assertKw", "condition", "message"])),
1382
+ // NULL
1383
+ null_stmt: (print) => group(print.spaced(["nullKw"]))
1313
1384
  };
1314
1385
 
1315
1386
  // src/syntax/procedure.ts
@@ -1356,12 +1427,6 @@ var procClauseMap = {
1356
1427
  returns_clause: (print) => print.spaced(["returnsKw", "dataType"]),
1357
1428
  determinism_clause: (print) => print.spaced("deterministicKw"),
1358
1429
  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
1430
  with_connection_clause: (print) => print.spaced(["withConnectionKw", "connection"])
1366
1431
  };
1367
1432
 
@@ -1989,8 +2054,6 @@ var bigqueryMap = {
1989
2054
  print("actions")
1990
2055
  ])
1991
2056
  ),
1992
- // ASSERT
1993
- assert_stmt: (print) => group(print.spaced(["assertKw", "expr", "as"])),
1994
2057
  // EXPORT
1995
2058
  export_data_stmt: (print) => group(
1996
2059
  join(hardline, [
@@ -2221,24 +2284,41 @@ var embedSql = (path, options2) => {
2221
2284
  const node = path.node;
2222
2285
  const parent = path.getParentNode(0);
2223
2286
  const grandParent = path.getParentNode(1);
2224
- if (isStringLiteral(node) && isAsClause(parent) && (isCreateFunctionStmt(grandParent) || isCreateProcedureStmt(grandParent)) && grandParent.clauses.some(isSqlLanguageClause)) {
2225
- return async (textToDoc) => {
2226
- const quote = detectQuote(node);
2227
- if (!quote) {
2228
- return void 0;
2229
- }
2230
- const sql = await textToDoc(node.value, options2);
2231
- return [
2232
- quote,
2233
- indent([hardline, stripTrailingHardline(sql)]),
2234
- hardline,
2235
- quote
2236
- ];
2237
- };
2287
+ const pluginOptions = options2;
2288
+ if (isStringLiteral(node) && isAsClause(parent) && (isCreateFunctionStmt(grandParent) || isCreateProcedureStmt(grandParent))) {
2289
+ if (grandParent.clauses.some(isSqlLanguageClause)) {
2290
+ return async (textToDoc) => {
2291
+ const quote = detectQuote(node);
2292
+ if (!quote) {
2293
+ return void 0;
2294
+ }
2295
+ const sql = await textToDoc(node.value, pluginOptions);
2296
+ return [
2297
+ quote,
2298
+ indent([hardline, stripTrailingHardline(sql)]),
2299
+ hardline,
2300
+ quote
2301
+ ];
2302
+ };
2303
+ }
2304
+ if (grandParent.clauses.some(isPlpgsqlLanguageClause) && pluginOptions.sqlExperimentalPlpgsql) {
2305
+ return async (textToDoc) => {
2306
+ const quote = detectQuote(node);
2307
+ if (!quote) {
2308
+ return void 0;
2309
+ }
2310
+ const sql = await textToDoc(node.value, {
2311
+ ...pluginOptions,
2312
+ parser: "plpgsql"
2313
+ });
2314
+ return [quote, [hardline, stripTrailingHardline(sql)], hardline, quote];
2315
+ };
2316
+ }
2238
2317
  }
2239
2318
  return null;
2240
2319
  };
2241
2320
  var isSqlLanguageClause = (clause) => isLanguageClause(clause) && clause.name.name.toLowerCase() === "sql";
2321
+ var isPlpgsqlLanguageClause = (clause) => isLanguageClause(clause) && clause.name.name.toLowerCase() === "plpgsql";
2242
2322
  var detectQuote = (node) => {
2243
2323
  const match = node.text.match(/^('|\$[^$]*\$)/);
2244
2324
  const quote = match?.[1];
@@ -2574,6 +2654,13 @@ var options = {
2574
2654
  default: false,
2575
2655
  description: "Skips formatting unsupported SQL statements instead of exiting with an error"
2576
2656
  // Since 0.12.0
2657
+ },
2658
+ sqlExperimentalPlpgsql: {
2659
+ type: "boolean",
2660
+ category: "SQL",
2661
+ default: false,
2662
+ description: "Enables formatting for PL/pgSQL in PostgreSQL function bodies"
2663
+ // Since 0.21.0
2577
2664
  }
2578
2665
  };
2579
2666
 
@@ -2603,6 +2690,11 @@ var languages = [
2603
2690
  extensions: [],
2604
2691
  name: "Experimental PostgreSQL SQL",
2605
2692
  parsers: ["postgresql"]
2693
+ },
2694
+ {
2695
+ extensions: [],
2696
+ name: "Experimental PL/pgSQL",
2697
+ parsers: ["plpgsql"]
2606
2698
  }
2607
2699
  ];
2608
2700
  var createParser = (dialect) => ({
@@ -2627,7 +2719,8 @@ var parsers = {
2627
2719
  bigquery: createParser("bigquery"),
2628
2720
  mysql: createParser("mysql"),
2629
2721
  mariadb: createParser("mariadb"),
2630
- postgresql: createParser("postgresql")
2722
+ postgresql: createParser("postgresql"),
2723
+ plpgsql: createParser("plpgsql")
2631
2724
  };
2632
2725
  var printers = {
2633
2726
  "sql-cst": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-sql-cst",
3
- "version": "0.19.3",
3
+ "version": "0.21.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.39.0"
42
+ "sql-parser-cst": "^0.41.2"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/jest": "^30.0.0",