prettier-plugin-sql-cst 0.12.4 → 0.14.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
@@ -121,9 +121,11 @@ There are also some SQL-specific options:
121
121
 
122
122
  | API Option | Default | Description |
123
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). |
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. |
125
126
  | `sqlParamTypes` | `[]` | Array of bound parameter types: `?`, `?nr`, `$nr`, `:name`, `@name`, `$name`. |
126
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) |
127
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. |
128
130
 
129
131
  ## Usage inside VSCode
package/dist/options.d.ts CHANGED
@@ -2,8 +2,10 @@ import { ParserOptions, SupportOptions } from "prettier";
2
2
  import { Node, ParserOptions as CstParserOptions } from "sql-parser-cst";
3
3
  export interface SqlPluginOptions {
4
4
  sqlKeywordCase: "preserve" | "upper" | "lower";
5
+ sqlLiteralCase: "preserve" | "upper" | "lower";
5
6
  sqlParamTypes: NonNullable<CstParserOptions["paramTypes"]>;
6
7
  sqlCanonicalSyntax: boolean;
8
+ sqlFinalSemicolon: boolean;
7
9
  sqlAcceptUnsupportedGrammar: boolean;
8
10
  }
9
11
  export type AllPrettierOptions<T = Node> = ParserOptions<T> & SqlPluginOptions;
package/dist/options.js CHANGED
@@ -25,6 +25,29 @@ exports.options = {
25
25
  },
26
26
  ],
27
27
  },
28
+ sqlLiteralCase: {
29
+ type: "choice",
30
+ category: "SQL",
31
+ default: "upper",
32
+ description: "Enforces upper/lower case for SQL literals (TRUE, FALSE, NULL)",
33
+ choices: [
34
+ {
35
+ value: "preserve",
36
+ description: "preserves the existing case",
37
+ since: "0.14.0",
38
+ },
39
+ {
40
+ value: "upper",
41
+ description: "forces all keywords to uppercase",
42
+ since: "0.14.0",
43
+ },
44
+ {
45
+ value: "lower",
46
+ description: "forces all keywords to lowercase",
47
+ since: "0.14.0",
48
+ },
49
+ ],
50
+ },
28
51
  sqlParamTypes: {
29
52
  type: "string",
30
53
  array: true,
@@ -41,6 +64,13 @@ exports.options = {
41
64
  description: "Enforces one true style of SQL syntax (adds and removes keywords)",
42
65
  // Since 0.11.0
43
66
  },
67
+ sqlFinalSemicolon: {
68
+ type: "boolean",
69
+ category: "SQL",
70
+ default: true,
71
+ description: "Enforces a semicolon at the end of last SQL statement",
72
+ // Since 0.13.0
73
+ },
44
74
  sqlAcceptUnsupportedGrammar: {
45
75
  type: "boolean",
46
76
  category: "SQL",
@@ -0,0 +1,3 @@
1
+ import { DoStmt } from "sql-parser-cst";
2
+ import { CstToDocMap } from "../CstToDocMap";
3
+ export declare const doMap: CstToDocMap<DoStmt>;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.doMap = void 0;
4
+ exports.doMap = {
5
+ do_stmt: function (print) { return print.spaced(["doKw", "language", "body"]); },
6
+ };
@@ -45,7 +45,9 @@ exports.exprMap = {
45
45
  return print("expr");
46
46
  }
47
47
  var parent = path.getParentNode();
48
- var lineStyle = (0, node_utils_1.isCreateTableStmt)(parent) ? print_utils_1.hardline : print_utils_1.softline;
48
+ var lineStyle = (0, node_utils_1.isCreateTableStmt)(parent) && print.dynamicLine() === print_utils_1.hardline
49
+ ? print_utils_1.hardline
50
+ : print_utils_1.softline;
49
51
  return (0, print_utils_1.group)(["(", (0, print_utils_1.indent)([lineStyle, print("expr")]), lineStyle, ")"]);
50
52
  },
51
53
  binary_expr: function (print, node) {
@@ -160,13 +162,17 @@ exports.exprMap = {
160
162
  /** cst-ignore: value */
161
163
  number_literal: function (print) { return print("text"); },
162
164
  /** cst-ignore: value */
163
- boolean_literal: function (print) { return print("valueKw"); },
165
+ boolean_literal: function (print, node, path, options) {
166
+ return printLiteral(node.valueKw, options);
167
+ },
164
168
  /** cst-ignore: value */
165
169
  string_literal: function (print) { return print("text"); },
166
170
  /** cst-ignore: value */
167
171
  blob_literal: function (print) { return print("text"); },
168
172
  /** cst-ignore: value */
169
- null_literal: function (print) { return print("nullKw"); },
173
+ null_literal: function (print, node, path, options) {
174
+ return printLiteral(node.nullKw, options);
175
+ },
170
176
  numeric_literal: function (print) { return print.spaced(["numericKw", "string"]); },
171
177
  bignumeric_literal: function (print) { return print.spaced(["bignumericKw", "string"]); },
172
178
  date_literal: function (print) { return print.spaced(["dateKw", "string"]); },
@@ -181,6 +187,16 @@ exports.exprMap = {
181
187
  variable: function (print) { return print("text"); },
182
188
  parameter: function (print) { return print("text"); },
183
189
  };
190
+ var printLiteral = function (node, options) {
191
+ switch (options.sqlLiteralCase) {
192
+ case "preserve":
193
+ return node.text;
194
+ case "upper":
195
+ return node.text.toUpperCase();
196
+ case "lower":
197
+ return node.text.toLowerCase();
198
+ }
199
+ };
184
200
  var isBooleanOp = function (_a) {
185
201
  var name = _a.name;
186
202
  return name === "AND" || name === "OR";
@@ -38,12 +38,6 @@ exports.functionMap = {
38
38
  function_window_clause: function (print) { return print.spaced("windowKw"); },
39
39
  function_transform_clause: function (print) { return print.spaced(["transformKw", "types"]); },
40
40
  transform_type: function (print) { return print.spaced(["forTypeKw", "dataType"]); },
41
- set_parameter_clause: function (print) {
42
- return print.spaced(["setKw", "name", "operator", "value"]);
43
- },
44
- set_parameter_from_current_clause: function (print) {
45
- return print.spaced(["setKw", "name", "fromCurrentKw"]);
46
- },
47
41
  dynamically_loaded_function: function (print) {
48
42
  return (0, print_utils_1.join)(", ", print(["objectFile", "symbol"]));
49
43
  },
@@ -64,8 +58,6 @@ exports.functionMap = {
64
58
  node.behaviorKw ? [print.dynamicLine(), print("behaviorKw")] : [],
65
59
  ]);
66
60
  },
67
- reset_parameter_clause: function (print) { return print.spaced(["resetKw", "name"]); },
68
- reset_all_parameters_clause: function (print) { return print.spaced("resetAllKw"); },
69
61
  };
70
62
  var hasOnlyAsClause = function (node) {
71
63
  return node.clauses.length === 1 && (0, node_utils_1.isAsClause)(node.clauses[0]);
@@ -0,0 +1,3 @@
1
+ import { AllParameterNodes } from "sql-parser-cst";
2
+ import { CstToDocMap } from "../CstToDocMap";
3
+ export declare const parameterMap: CstToDocMap<AllParameterNodes>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parameterMap = void 0;
4
+ exports.parameterMap = {
5
+ // SET
6
+ set_parameter_stmt: function (print) {
7
+ return print.spaced(["setKw", "modifierKw", "name", "operator", "value"]);
8
+ },
9
+ set_time_zone_parameter_stmt: function (print) {
10
+ return print.spaced(["setKw", "modifierKw", "timeZoneKw", "value"]);
11
+ },
12
+ set_parameter_clause: function (print) {
13
+ return print.spaced(["setKw", "name", "operator", "value"]);
14
+ },
15
+ local_parameter_value: function (print) { return print("localKw"); },
16
+ set_parameter_from_current_clause: function (print) {
17
+ return print.spaced(["setKw", "name", "fromCurrentKw"]);
18
+ },
19
+ // RESET
20
+ reset_parameter_stmt: function (print) { return print.spaced(["resetKw", "name"]); },
21
+ reset_parameter_clause: function (print) { return print.spaced(["resetKw", "name"]); },
22
+ all_parameters: function (print) { return print("allKw"); },
23
+ // SHOW
24
+ show_parameter_stmt: function (print) { return print.spaced(["showKw", "name"]); },
25
+ };
@@ -12,7 +12,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.preparedStatementsMap = void 0;
13
13
  var print_utils_1 = require("../print_utils");
14
14
  exports.preparedStatementsMap = {
15
- execute_stmt: function (print) {
15
+ // EXECUTE
16
+ execute_stmt: function (print, node) {
17
+ var _a;
18
+ if (((_a = node.args) === null || _a === void 0 ? void 0 : _a.type) === "execute_using_clause") {
19
+ return (0, print_utils_1.group)(print.spaced(["executeKw", "name", "args"]));
20
+ }
21
+ else {
22
+ return (0, print_utils_1.group)([print.spaced(["executeKw", "name"]), print("args")]);
23
+ }
24
+ },
25
+ execute_immediate_stmt: function (print) {
16
26
  return (0, print_utils_1.join)(print_utils_1.hardline, __spreadArray([
17
27
  (0, print_utils_1.group)([
18
28
  print.spaced(["executeKw", "immediateKw"]),
@@ -21,5 +31,21 @@ exports.preparedStatementsMap = {
21
31
  ], print(["into", "using"]).map(function (clause) { return (0, print_utils_1.group)(clause); }), true));
22
32
  },
23
33
  execute_into_clause: function (print) { return print.spaced(["intoKw", "variables"]); },
24
- execute_using_clause: function (print) { return print.spaced(["usingKw", "values"]); },
34
+ execute_using_clause: function (print) {
35
+ return (0, print_utils_1.group)([print("usingKw"), (0, print_utils_1.indent)([print_utils_1.line, print("values")])]);
36
+ },
37
+ // PREPARE
38
+ prepare_stmt: function (print) {
39
+ return (0, print_utils_1.group)((0, print_utils_1.join)(" ", [
40
+ print("prepareKw"),
41
+ print(["name", "params"]),
42
+ print("source"),
43
+ ]));
44
+ },
45
+ prepare_from_clause: function (print) {
46
+ return (0, print_utils_1.group)([print("fromKw"), (0, print_utils_1.indent)([print_utils_1.line, print("expr")])]);
47
+ },
48
+ // DEALLOCATE
49
+ deallocate_stmt: function (print) { return print.spaced(["deallocateKw", "name"]); },
50
+ deallocate_all: function (print) { return print("allKw"); },
25
51
  };
@@ -5,7 +5,9 @@ var print_utils_1 = require("../print_utils");
5
5
  exports.programMap = {
6
6
  program: function (print, node, path, options) {
7
7
  return print("statements").map(function (doc, i) {
8
- return printStatement(doc, i, node.statements, options);
8
+ return i === node.statements.length - 1
9
+ ? printFinalStatement(doc, i, node.statements, options)
10
+ : printStatement(doc, i, node.statements, options);
9
11
  });
10
12
  },
11
13
  };
@@ -27,3 +29,18 @@ var printStatement = function (doc, i, statements, options) {
27
29
  return [";", print_utils_1.hardline, doc];
28
30
  }
29
31
  };
32
+ var printFinalStatement = function (doc, i, statements, options) {
33
+ var stmtDoc = printStatement(doc, i, statements, options);
34
+ if (statements[i].type === "empty") {
35
+ // When the final statement is an empty statement,
36
+ // this means we have inserted a semicolon and newline before it.
37
+ // So nothing else is needed.
38
+ return stmtDoc;
39
+ }
40
+ else {
41
+ // If the last statement is not empty,
42
+ // this means sqlFinalSemicolon option is false,
43
+ // so we need to add an empty line after it.
44
+ return [stmtDoc, print_utils_1.hardline];
45
+ }
46
+ };
@@ -3,10 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.sequenceMap = void 0;
4
4
  var print_utils_1 = require("../print_utils");
5
5
  exports.sequenceMap = {
6
- create_sequence_stmt: function (print) {
6
+ create_sequence_stmt: function (print, node) {
7
7
  return (0, print_utils_1.group)([
8
8
  print.spaced(["createKw", "kind", "sequenceKw", "ifNotExistsKw", "name"]),
9
- (0, print_utils_1.indent)([print.dynamicLine(), (0, print_utils_1.join)(print_utils_1.line, print("options"))]),
9
+ node.options.length > 0
10
+ ? (0, print_utils_1.indent)([print.dynamicLine(), (0, print_utils_1.join)(print_utils_1.line, print("options"))])
11
+ : [],
10
12
  ]);
11
13
  },
12
14
  alter_sequence_stmt: function (print) {
@@ -24,6 +26,11 @@ exports.sequenceMap = {
24
26
  "behaviorKw",
25
27
  ]));
26
28
  },
29
+ sequence_option_sequence_name: function (print) {
30
+ return print.spaced(["sequenceNameKw", "name"]);
31
+ },
32
+ sequence_option_logged: function (print) { return print.spaced("loggedKw"); },
33
+ sequence_option_unlogged: function (print) { return print.spaced("unloggedKw"); },
27
34
  sequence_option_as_type: function (print) { return print.spaced(["asKw", "dataType"]); },
28
35
  sequence_option_increment: function (print) {
29
36
  return print.spaced(["incrementKw", "byKw", "value"]);
@@ -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 do_1 = require("./do");
25
26
  var domain_1 = require("./domain");
26
27
  var drop_table_1 = require("./drop_table");
27
28
  var explain_1 = require("./explain");
@@ -34,6 +35,7 @@ var merge_1 = require("./merge");
34
35
  var other_clauses_1 = require("./other_clauses");
35
36
  var rename_table_1 = require("./rename_table");
36
37
  var role_1 = require("./role");
38
+ var parameter_1 = require("./parameter");
37
39
  var policy_1 = require("./policy");
38
40
  var prepared_statements_1 = require("./prepared_statements");
39
41
  var procedural_language_1 = require("./procedural_language");
@@ -54,4 +56,4 @@ var bigquery_1 = require("./dialects/bigquery");
54
56
  var mysql_1 = require("./dialects/mysql");
55
57
  var sqlite_1 = require("./dialects/sqlite");
56
58
  var postgresql_1 = require("./dialects/postgresql");
57
- 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(__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), policy_1.policyMap), prepared_statements_1.preparedStatementsMap), proc_clause_1.procClauseMap), procedural_language_1.proceduralLanguageMap), procedure_1.procedureMap), program_1.programMap), rename_table_1.renameTableMap), role_1.roleMap), 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);
59
+ 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(__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), do_1.doMap), 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), parameter_1.parameterMap), policy_1.policyMap), prepared_statements_1.preparedStatementsMap), proc_clause_1.procClauseMap), procedural_language_1.proceduralLanguageMap), procedure_1.procedureMap), program_1.programMap), rename_table_1.renameTableMap), role_1.roleMap), 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);
@@ -13,8 +13,10 @@ var transformCst = function (cst, originalText, options) {
13
13
  // so we don't need to worry about comments interfering with other transforms
14
14
  comments_1.moveCommentsToRoot,
15
15
  stripTrailingCommas_1.stripTrailingCommas,
16
- addFinalSemicolon_1.addFinalSemicolon,
17
16
  ];
17
+ if (options.sqlFinalSemicolon) {
18
+ transforms.push(addFinalSemicolon_1.addFinalSemicolon);
19
+ }
18
20
  if (options.sqlCanonicalSyntax) {
19
21
  transforms.push(aliasAs_1.processAliasAs, canonicOperators_1.canonicOperators, canonicKeywords_1.canonicKeywords);
20
22
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-sql-cst",
3
- "version": "0.12.4",
3
+ "version": "0.14.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.33.1"
40
+ "sql-parser-cst": "^0.34.1"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/jest": "^29.2.5",