prettier-plugin-sql-cst 0.18.0 → 0.19.1

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.
Files changed (76) hide show
  1. package/dist/CstToDocMap.d.ts +2 -2
  2. package/dist/CstToDocMap.js +1 -2
  3. package/dist/PrintFn.js +1 -2
  4. package/dist/embed.js +5 -13
  5. package/dist/embedJs.js +28 -84
  6. package/dist/embedJson.js +29 -85
  7. package/dist/embedSql.js +26 -76
  8. package/dist/index.js +28 -34
  9. package/dist/node_utils.d.ts +1 -0
  10. package/dist/node_utils.js +44 -54
  11. package/dist/options.js +1 -4
  12. package/dist/printComment.js +2 -6
  13. package/dist/printSql.js +29 -39
  14. package/dist/print_utils.js +19 -38
  15. package/dist/syntax/alias.js +2 -5
  16. package/dist/syntax/alter_action.js +115 -208
  17. package/dist/syntax/alter_table.js +18 -27
  18. package/dist/syntax/analyze.js +3 -8
  19. package/dist/syntax/base.js +6 -9
  20. package/dist/syntax/cast.js +3 -6
  21. package/dist/syntax/comment.js +53 -122
  22. package/dist/syntax/constraint.d.ts +1 -1
  23. package/dist/syntax/constraint.js +54 -90
  24. package/dist/syntax/create_table.js +49 -87
  25. package/dist/syntax/data_type.js +25 -44
  26. package/dist/syntax/dcl.js +78 -136
  27. package/dist/syntax/delete.js +4 -9
  28. package/dist/syntax/dialects/bigquery.js +70 -116
  29. package/dist/syntax/dialects/mysql.js +2 -5
  30. package/dist/syntax/dialects/postgresql.js +8 -13
  31. package/dist/syntax/dialects/sqlite.js +8 -17
  32. package/dist/syntax/do.js +3 -6
  33. package/dist/syntax/domain.js +10 -19
  34. package/dist/syntax/drop_table.js +10 -15
  35. package/dist/syntax/explain.js +6 -11
  36. package/dist/syntax/expr.js +124 -173
  37. package/dist/syntax/extension.js +12 -21
  38. package/dist/syntax/frame.js +11 -16
  39. package/dist/syntax/function.js +53 -63
  40. package/dist/syntax/index.js +54 -81
  41. package/dist/syntax/insert.js +32 -49
  42. package/dist/syntax/merge.js +25 -30
  43. package/dist/syntax/other_clauses.js +5 -14
  44. package/dist/syntax/parameter.js +13 -26
  45. package/dist/syntax/policy.js +26 -41
  46. package/dist/syntax/prepared_statements.js +23 -43
  47. package/dist/syntax/proc_clause.js +10 -15
  48. package/dist/syntax/procedural_language.js +80 -111
  49. package/dist/syntax/procedure.js +36 -40
  50. package/dist/syntax/program.js +15 -22
  51. package/dist/syntax/publication.js +26 -47
  52. package/dist/syntax/rename_table.js +7 -12
  53. package/dist/syntax/role.js +30 -55
  54. package/dist/syntax/schema.js +17 -35
  55. package/dist/syntax/select.js +157 -226
  56. package/dist/syntax/sequence.js +35 -52
  57. package/dist/syntax/subscription.js +9 -24
  58. package/dist/syntax/transaction.js +18 -33
  59. package/dist/syntax/transformMap.js +99 -63
  60. package/dist/syntax/trigger.js +48 -72
  61. package/dist/syntax/truncate.js +9 -14
  62. package/dist/syntax/type.js +11 -22
  63. package/dist/syntax/unsupported_grammar.js +3 -6
  64. package/dist/syntax/update.js +7 -14
  65. package/dist/syntax/view.js +32 -43
  66. package/dist/transform/addFinalSemicolon.js +5 -9
  67. package/dist/transform/aliasAs.js +4 -8
  68. package/dist/transform/canonicKeywords.js +16 -20
  69. package/dist/transform/canonicOperators.js +8 -12
  70. package/dist/transform/comments.js +10 -31
  71. package/dist/transform/stripTrailingCommas.js +7 -11
  72. package/dist/transform/transformCst.js +13 -17
  73. package/dist/transform/transformUtils.js +2 -6
  74. package/dist/utils.js +9 -30
  75. package/dist/visitAllNodes.js +8 -13
  76. package/package.json +9 -8
@@ -1,11 +1,11 @@
1
1
  import { Node } from "sql-parser-cst";
2
2
  import { AstPath, Doc } from "prettier";
3
- import { PrintFn } from "./PrintFn";
3
+ import { PrintableKey, PrintFn } from "./PrintFn";
4
4
  import { AllPrettierOptions } from "./options";
5
5
  export type NodeByType<TType, TNode> = Extract<TNode, {
6
6
  type: TType;
7
7
  }>;
8
- export type ToDocFn<TNode> = (print: PrintFn<TNode>, node: TNode, path: AstPath<TNode>, options: AllPrettierOptions<TNode>) => Doc;
8
+ export type ToDocFn<TNode> = (print: PrintFn<TNode, PrintableKey<TNode>>, node: TNode, path: AstPath<TNode>, options: AllPrettierOptions<TNode>) => Doc;
9
9
  export type CstToDocMap<TNode extends Node> = {
10
10
  [TType in TNode["type"]]: ToDocFn<NodeByType<TType, TNode>>;
11
11
  };
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/dist/PrintFn.js CHANGED
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/dist/embed.js CHANGED
@@ -1,14 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.embed = void 0;
4
- var embedJs_1 = require("./embedJs");
5
- var embedJson_1 = require("./embedJson");
6
- var embedSql_1 = require("./embedSql");
7
- var embed = function () {
8
- var args = [];
9
- for (var _i = 0; _i < arguments.length; _i++) {
10
- args[_i] = arguments[_i];
11
- }
12
- return embedJson_1.embedJson.apply(void 0, args) || embedJs_1.embedJs.apply(void 0, args) || embedSql_1.embedSql.apply(void 0, args);
1
+ import { embedJs } from "./embedJs";
2
+ import { embedJson } from "./embedJson";
3
+ import { embedSql } from "./embedSql";
4
+ export const embed = (...args) => {
5
+ return embedJson(...args) || embedJs(...args) || embedSql(...args);
13
6
  };
14
- exports.embed = embed;
package/dist/embedJs.js CHANGED
@@ -1,94 +1,38 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- Object.defineProperty(exports, "__esModule", { value: true });
50
- exports.embedJs = void 0;
51
- var node_utils_1 = require("./node_utils");
52
- var print_utils_1 = require("./print_utils");
53
- var embedJs = function (path, options) {
54
- var node = path.node;
55
- var parent = path.getParentNode(0);
56
- var grandParent = path.getParentNode(1);
57
- if ((0, node_utils_1.isStringLiteral)(node) &&
58
- (0, node_utils_1.isAsClause)(parent) &&
59
- (0, node_utils_1.isCreateFunctionStmt)(grandParent) &&
1
+ import { isAsClause, isCreateFunctionStmt, isLanguageClause, isStringLiteral, } from "./node_utils";
2
+ import { hardline, indent, stripTrailingHardline } from "./print_utils";
3
+ export const embedJs = (path, options) => {
4
+ const node = path.node;
5
+ const parent = path.getParentNode(0);
6
+ const grandParent = path.getParentNode(1);
7
+ if (isStringLiteral(node) &&
8
+ isAsClause(parent) &&
9
+ isCreateFunctionStmt(grandParent) &&
60
10
  grandParent.clauses.some(isJavaScriptLanguageClause)) {
61
- return function (textToDoc) { return __awaiter(void 0, void 0, void 0, function () {
62
- var quotes, js;
63
- return __generator(this, function (_a) {
64
- switch (_a.label) {
65
- case 0:
66
- quotes = detectQuotes(node.value);
67
- if (!quotes) {
68
- // Give up for now. Don't format JavaScript inside the string.
69
- // Perhaps tackle this corner-case in the future.
70
- return [2 /*return*/, undefined];
71
- }
72
- return [4 /*yield*/, textToDoc(node.value, __assign(__assign({}, options), { parser: "babel" }))];
73
- case 1:
74
- js = _a.sent();
75
- return [2 /*return*/, [
76
- quotes[0],
77
- (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(js)]),
78
- print_utils_1.hardline,
79
- quotes[1],
80
- ]];
81
- }
11
+ return async (textToDoc) => {
12
+ const quotes = detectQuotes(node.value);
13
+ if (!quotes) {
14
+ // Give up for now. Don't format JavaScript inside the string.
15
+ // Perhaps tackle this corner-case in the future.
16
+ return undefined;
17
+ }
18
+ const js = await textToDoc(node.value, {
19
+ ...options,
20
+ parser: "babel",
82
21
  });
83
- }); };
22
+ return [
23
+ quotes[0],
24
+ indent([hardline, stripTrailingHardline(js)]),
25
+ hardline,
26
+ quotes[1],
27
+ ];
28
+ };
84
29
  }
85
30
  return null;
86
31
  };
87
- exports.embedJs = embedJs;
88
- var isJavaScriptLanguageClause = function (clause) { return (0, node_utils_1.isLanguageClause)(clause) && clause.name.name === "js"; };
32
+ const isJavaScriptLanguageClause = (clause) => isLanguageClause(clause) && clause.name.name === "js";
89
33
  // Whether to quote the code with single- or double-quotes.
90
34
  // Returns undefined when neither can be used without escaping.
91
- var detectQuotes = function (js) {
35
+ const detectQuotes = (js) => {
92
36
  if (!/'''/.test(js)) {
93
37
  return ["r'''", "'''"];
94
38
  }
package/dist/embedJson.js CHANGED
@@ -1,90 +1,34 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
1
+ import { isJsonLiteral, isJsonbLiteral, isStringLiteral } from "./node_utils";
2
+ import { ifBreak, indent, softline, stripTrailingHardline, } from "./print_utils";
3
+ export const embedJson = (path, options) => {
4
+ const node = path.node;
5
+ const parent = path.getParentNode();
6
+ if (isStringLiteral(node) &&
7
+ (isJsonLiteral(parent) || isJsonbLiteral(parent))) {
8
+ return async (textToDoc) => {
9
+ if (containsTripleQuote(node.value) ||
10
+ containsBackslash(node.value) ||
11
+ isRawString(node)) {
12
+ // Give up for now. Don't format JSON inside the string.
13
+ // Tackle these corner-case in the future.
14
+ return undefined;
43
15
  }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- Object.defineProperty(exports, "__esModule", { value: true });
50
- exports.embedJson = void 0;
51
- var node_utils_1 = require("./node_utils");
52
- var print_utils_1 = require("./print_utils");
53
- var embedJson = function (path, options) {
54
- var node = path.node;
55
- var parent = path.getParentNode();
56
- if ((0, node_utils_1.isStringLiteral)(node) &&
57
- ((0, node_utils_1.isJsonLiteral)(parent) || (0, node_utils_1.isJsonbLiteral)(parent))) {
58
- return function (textToDoc) { return __awaiter(void 0, void 0, void 0, function () {
59
- var json, inlineQuote;
60
- return __generator(this, function (_a) {
61
- switch (_a.label) {
62
- case 0:
63
- if (containsTripleQuote(node.value) ||
64
- containsBackslash(node.value) ||
65
- isRawString(node)) {
66
- // Give up for now. Don't format JSON inside the string.
67
- // Tackle these corner-case in the future.
68
- return [2 /*return*/, undefined];
69
- }
70
- return [4 /*yield*/, textToDoc(node.value, __assign(__assign({}, options), { parser: "json" }))];
71
- case 1:
72
- json = _a.sent();
73
- inlineQuote = containsSingleQuote(node.value) ? "'''" : "'";
74
- return [2 /*return*/, [
75
- (0, print_utils_1.ifBreak)("'''", inlineQuote),
76
- (0, print_utils_1.indent)([print_utils_1.softline, (0, print_utils_1.stripTrailingHardline)(json)]),
77
- print_utils_1.softline,
78
- (0, print_utils_1.ifBreak)("'''", inlineQuote),
79
- ]];
80
- }
16
+ const json = await textToDoc(node.value, {
17
+ ...options,
18
+ parser: "json",
81
19
  });
82
- }); };
20
+ const inlineQuote = containsSingleQuote(node.value) ? "'''" : "'";
21
+ return [
22
+ ifBreak("'''", inlineQuote),
23
+ indent([softline, stripTrailingHardline(json)]),
24
+ softline,
25
+ ifBreak("'''", inlineQuote),
26
+ ];
27
+ };
83
28
  }
84
29
  return null;
85
30
  };
86
- exports.embedJson = embedJson;
87
- var containsSingleQuote = function (json) { return /'/.test(json); };
88
- var containsTripleQuote = function (json) { return /'''/.test(json); };
89
- var containsBackslash = function (json) { return /\\/.test(json); };
90
- var isRawString = function (node) { return /^R/i.test(node.text); };
31
+ const containsSingleQuote = (json) => /'/.test(json);
32
+ const containsTripleQuote = (json) => /'''/.test(json);
33
+ const containsBackslash = (json) => /\\/.test(json);
34
+ const isRawString = (node) => /^R/i.test(node.text);
package/dist/embedSql.js CHANGED
@@ -1,83 +1,33 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.embedSql = void 0;
40
- var node_utils_1 = require("./node_utils");
41
- var print_utils_1 = require("./print_utils");
42
- var embedSql = function (path, options) {
43
- var node = path.node;
44
- var parent = path.getParentNode(0);
45
- var grandParent = path.getParentNode(1);
46
- if ((0, node_utils_1.isStringLiteral)(node) &&
47
- (0, node_utils_1.isAsClause)(parent) &&
48
- ((0, node_utils_1.isCreateFunctionStmt)(grandParent) || (0, node_utils_1.isCreateProcedureStmt)(grandParent)) &&
1
+ import { isAsClause, isCreateFunctionStmt, isCreateProcedureStmt, isLanguageClause, isStringLiteral, } from "./node_utils";
2
+ import { hardline, indent, stripTrailingHardline } from "./print_utils";
3
+ export const embedSql = (path, options) => {
4
+ const node = path.node;
5
+ const parent = path.getParentNode(0);
6
+ const grandParent = path.getParentNode(1);
7
+ if (isStringLiteral(node) &&
8
+ isAsClause(parent) &&
9
+ (isCreateFunctionStmt(grandParent) || isCreateProcedureStmt(grandParent)) &&
49
10
  grandParent.clauses.some(isSqlLanguageClause)) {
50
- return function (textToDoc) { return __awaiter(void 0, void 0, void 0, function () {
51
- var quote, sql;
52
- return __generator(this, function (_a) {
53
- switch (_a.label) {
54
- case 0:
55
- quote = detectQuote(node);
56
- if (!quote) {
57
- return [2 /*return*/, undefined];
58
- }
59
- return [4 /*yield*/, textToDoc(node.value, options)];
60
- case 1:
61
- sql = _a.sent();
62
- return [2 /*return*/, [
63
- quote,
64
- (0, print_utils_1.indent)([print_utils_1.hardline, (0, print_utils_1.stripTrailingHardline)(sql)]),
65
- print_utils_1.hardline,
66
- quote,
67
- ]];
68
- }
69
- });
70
- }); };
11
+ return async (textToDoc) => {
12
+ const quote = detectQuote(node);
13
+ if (!quote) {
14
+ return undefined;
15
+ }
16
+ const sql = await textToDoc(node.value, options);
17
+ return [
18
+ quote,
19
+ indent([hardline, stripTrailingHardline(sql)]),
20
+ hardline,
21
+ quote,
22
+ ];
23
+ };
71
24
  }
72
25
  return null;
73
26
  };
74
- exports.embedSql = embedSql;
75
- var isSqlLanguageClause = function (clause) {
76
- return (0, node_utils_1.isLanguageClause)(clause) && clause.name.name.toLowerCase() === "sql";
77
- };
78
- var detectQuote = function (node) {
79
- var match = node.text.match(/^('|\$[^$]*\$)/);
80
- var quote = match === null || match === void 0 ? void 0 : match[1];
27
+ const isSqlLanguageClause = (clause) => isLanguageClause(clause) && clause.name.name.toLowerCase() === "sql";
28
+ const detectQuote = (node) => {
29
+ const match = node.text.match(/^('|\$[^$]*\$)/);
30
+ const quote = match?.[1];
81
31
  if (quote === "'") {
82
32
  // Convert `'` quotes to `$$`.
83
33
  // But bail out if the code already contains $$.
package/dist/index.js CHANGED
@@ -1,15 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.printers = exports.parsers = exports.languages = exports.options = void 0;
4
- var sql_parser_cst_1 = require("sql-parser-cst");
5
- var printSql_1 = require("./printSql");
6
- var printComment_1 = require("./printComment");
7
- var embed_1 = require("./embed");
8
- var utils_1 = require("./utils");
9
- var transformCst_1 = require("./transform/transformCst");
10
- var options_1 = require("./options");
11
- Object.defineProperty(exports, "options", { enumerable: true, get: function () { return options_1.options; } });
12
- exports.languages = [
1
+ import { parse } from "sql-parser-cst";
2
+ import { printSql } from "./printSql";
3
+ import { printComment } from "./printComment";
4
+ import { embed } from "./embed";
5
+ import { isNode } from "./utils";
6
+ import { transformCst } from "./transform/transformCst";
7
+ export { options } from "./options";
8
+ export const languages = [
13
9
  {
14
10
  extensions: [".sql", ".sqlite"],
15
11
  name: "SQLite SQL",
@@ -36,35 +32,33 @@ exports.languages = [
36
32
  parsers: ["postgresql"],
37
33
  },
38
34
  ];
39
- var createParser = function (dialect) { return ({
40
- parse: function (text, options) {
41
- return (0, transformCst_1.transformCst)((0, sql_parser_cst_1.parse)(text, {
42
- dialect: dialect,
43
- includeRange: true,
44
- includeComments: true,
45
- filename: options.filepath,
46
- paramTypes: options.sqlParamTypes,
47
- acceptUnsupportedGrammar: options
48
- .sqlAcceptUnsupportedGrammar,
49
- }), text, options);
50
- },
35
+ const createParser = (dialect) => ({
36
+ parse: (text, options) => transformCst(parse(text, {
37
+ dialect,
38
+ includeRange: true,
39
+ includeComments: true,
40
+ filename: options.filepath,
41
+ paramTypes: options.sqlParamTypes,
42
+ acceptUnsupportedGrammar: options
43
+ .sqlAcceptUnsupportedGrammar,
44
+ }), text, options),
51
45
  astFormat: "sql-cst",
52
- locStart: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[0]; },
53
- locEnd: function (node) { var _a; return (_a = node.range) === null || _a === void 0 ? void 0 : _a[1]; },
54
- }); };
55
- exports.parsers = {
46
+ locStart: (node) => node.range?.[0],
47
+ locEnd: (node) => node.range?.[1],
48
+ });
49
+ export const parsers = {
56
50
  sqlite: createParser("sqlite"),
57
51
  bigquery: createParser("bigquery"),
58
52
  mysql: createParser("mysql"),
59
53
  mariadb: createParser("mariadb"),
60
54
  postgresql: createParser("postgresql"),
61
55
  };
62
- exports.printers = {
56
+ export const printers = {
63
57
  "sql-cst": {
64
- print: printSql_1.printSql,
65
- embed: embed_1.embed,
66
- printComment: printComment_1.printComment,
67
- canAttachComment: utils_1.isNode,
68
- isBlockComment: function (node) { return node.type === "block_comment"; },
58
+ print: printSql,
59
+ embed: embed,
60
+ printComment: printComment,
61
+ canAttachComment: isNode,
62
+ isBlockComment: (node) => node.type === "block_comment",
69
63
  },
70
64
  };
@@ -28,6 +28,7 @@ export declare const isDynamicallyLoadedFunction: (node: any) => node is import(
28
28
  export declare const isBigqueryQuotedMemberExpr: (node: any) => node is import("sql-parser-cst").BigQueryQuotedMemberExpr;
29
29
  export declare const isCreateTriggerStmt: (node: any) => node is import("sql-parser-cst").CreateTriggerStmt;
30
30
  export declare const isExecuteClause: (node: any) => node is import("sql-parser-cst").ExecuteClause;
31
+ export declare const isFunctionSignature: (node: any) => node is import("sql-parser-cst").FunctionSignature;
31
32
  export declare const isDataTypeName: (node: any) => node is import("sql-parser-cst").DataTypeName;
32
33
  export declare const isIntervalDataType: (node: any) => node is import("sql-parser-cst").IntervalDataType;
33
34
  export declare const isIntervalUnit: (node: any) => node is import("sql-parser-cst").IntervalUnit;
@@ -1,54 +1,44 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isDataType = exports.isParametricDataType = exports.isTimeDataType = exports.isIntervalUnitRange = exports.isIntervalUnit = exports.isIntervalDataType = exports.isDataTypeName = exports.isExecuteClause = exports.isCreateTriggerStmt = exports.isBigqueryQuotedMemberExpr = exports.isDynamicallyLoadedFunction = exports.isLanguageClause = exports.isAlterActionRename = exports.isDropProcedureStmt = exports.isAlterProcedureStmt = exports.isCreateProcedureStmt = exports.isDropFunctionStmt = exports.isAlterFunctionStmt = exports.isCreateFunctionStmt = exports.isListExpr = exports.isCompoundSelectStmt = exports.isSelectStmt = exports.isMemberExpr = exports.isParenExpr = exports.isAsClause = exports.isArraySubscript = exports.isStringLiteral = exports.isJsonbLiteral = exports.isJsonLiteral = exports.isEmpty = exports.isKeyword = exports.isCreateTableStmt = exports.isFuncArgs = exports.isFuncCall = exports.isValuesClause = exports.isProgram = void 0;
4
- var utils_1 = require("./utils");
5
- var is = function (type) {
6
- return function (node) {
7
- return (0, utils_1.isObject)(node) && node.type === type;
8
- };
9
- };
10
- exports.isProgram = is("program");
11
- exports.isValuesClause = is("values_clause");
12
- exports.isFuncCall = is("func_call");
13
- exports.isFuncArgs = is("func_args");
14
- exports.isCreateTableStmt = is("create_table_stmt");
15
- exports.isKeyword = is("keyword");
16
- exports.isEmpty = is("empty");
17
- exports.isJsonLiteral = is("json_literal");
18
- exports.isJsonbLiteral = is("jsonb_literal");
19
- exports.isStringLiteral = is("string_literal");
20
- exports.isArraySubscript = is("array_subscript");
21
- exports.isAsClause = is("as_clause");
22
- exports.isParenExpr = is("paren_expr");
23
- exports.isMemberExpr = is("member_expr");
24
- exports.isSelectStmt = is("select_stmt");
25
- exports.isCompoundSelectStmt = is("compound_select_stmt");
26
- exports.isListExpr = is("list_expr");
27
- exports.isCreateFunctionStmt = is("create_function_stmt");
28
- exports.isAlterFunctionStmt = is("alter_function_stmt");
29
- exports.isDropFunctionStmt = is("drop_function_stmt");
30
- exports.isCreateProcedureStmt = is("create_procedure_stmt");
31
- exports.isAlterProcedureStmt = is("alter_procedure_stmt");
32
- exports.isDropProcedureStmt = is("drop_procedure_stmt");
33
- exports.isAlterActionRename = is("alter_action_rename");
34
- exports.isLanguageClause = is("language_clause");
35
- exports.isDynamicallyLoadedFunction = is("dynamically_loaded_function");
36
- exports.isBigqueryQuotedMemberExpr = is("bigquery_quoted_member_expr");
37
- exports.isCreateTriggerStmt = is("create_trigger_stmt");
38
- exports.isExecuteClause = is("execute_clause");
39
- exports.isDataTypeName = is("data_type_name");
40
- exports.isIntervalDataType = is("interval_data_type");
41
- exports.isIntervalUnit = is("interval_unit");
42
- exports.isIntervalUnitRange = is("interval_unit_range");
43
- exports.isTimeDataType = is("time_data_type");
44
- exports.isParametricDataType = is("parametric_data_type");
45
- var isDataType = function (_a) {
46
- var node = _a[0], ancestors = _a.slice(1);
47
- return (0, exports.isDataTypeName)(node) ||
48
- (0, exports.isIntervalDataType)(node) ||
49
- ((0, exports.isIntervalUnit)(node) && (0, exports.isDataType)(ancestors)) ||
50
- ((0, exports.isIntervalUnitRange)(node) && (0, exports.isDataType)(ancestors)) ||
51
- (0, exports.isTimeDataType)(node) ||
52
- (0, exports.isParametricDataType)(node);
53
- };
54
- exports.isDataType = isDataType;
1
+ import { isObject } from "./utils";
2
+ const is = (type) => (node) => isObject(node) && node.type === type;
3
+ export const isProgram = is("program");
4
+ export const isValuesClause = is("values_clause");
5
+ export const isFuncCall = is("func_call");
6
+ export const isFuncArgs = is("func_args");
7
+ export const isCreateTableStmt = is("create_table_stmt");
8
+ export const isKeyword = is("keyword");
9
+ export const isEmpty = is("empty");
10
+ export const isJsonLiteral = is("json_literal");
11
+ export const isJsonbLiteral = is("jsonb_literal");
12
+ export const isStringLiteral = is("string_literal");
13
+ export const isArraySubscript = is("array_subscript");
14
+ export const isAsClause = is("as_clause");
15
+ export const isParenExpr = is("paren_expr");
16
+ export const isMemberExpr = is("member_expr");
17
+ export const isSelectStmt = is("select_stmt");
18
+ export const isCompoundSelectStmt = is("compound_select_stmt");
19
+ export const isListExpr = is("list_expr");
20
+ export const isCreateFunctionStmt = is("create_function_stmt");
21
+ export const isAlterFunctionStmt = is("alter_function_stmt");
22
+ export const isDropFunctionStmt = is("drop_function_stmt");
23
+ export const isCreateProcedureStmt = is("create_procedure_stmt");
24
+ export const isAlterProcedureStmt = is("alter_procedure_stmt");
25
+ export const isDropProcedureStmt = is("drop_procedure_stmt");
26
+ export const isAlterActionRename = is("alter_action_rename");
27
+ export const isLanguageClause = is("language_clause");
28
+ export const isDynamicallyLoadedFunction = is("dynamically_loaded_function");
29
+ export const isBigqueryQuotedMemberExpr = is("bigquery_quoted_member_expr");
30
+ export const isCreateTriggerStmt = is("create_trigger_stmt");
31
+ export const isExecuteClause = is("execute_clause");
32
+ export const isFunctionSignature = is("function_signature");
33
+ export const isDataTypeName = is("data_type_name");
34
+ export const isIntervalDataType = is("interval_data_type");
35
+ export const isIntervalUnit = is("interval_unit");
36
+ export const isIntervalUnitRange = is("interval_unit_range");
37
+ export const isTimeDataType = is("time_data_type");
38
+ export const isParametricDataType = is("parametric_data_type");
39
+ export const isDataType = ([node, ...ancestors]) => isDataTypeName(node) ||
40
+ isIntervalDataType(node) ||
41
+ (isIntervalUnit(node) && isDataType(ancestors)) ||
42
+ (isIntervalUnitRange(node) && isDataType(ancestors)) ||
43
+ isTimeDataType(node) ||
44
+ isParametricDataType(node);
package/dist/options.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.options = void 0;
4
- exports.options = {
1
+ export const options = {
5
2
  sqlKeywordCase: {
6
3
  type: "choice",
7
4
  category: "SQL",
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.printComment = void 0;
4
- var printComment = function (path) {
5
- var node = path.node;
1
+ export const printComment = (path) => {
2
+ const node = path.node;
6
3
  if (node.type === "line_comment") {
7
4
  if (/^--[^ \t]/.test(node.text)) {
8
5
  return node.text.replace(/^--/, "-- ");
@@ -13,4 +10,3 @@ var printComment = function (path) {
13
10
  }
14
11
  return node.text;
15
12
  };
16
- exports.printComment = printComment;