prettier-plugin-sql-cst 0.18.0 → 0.19.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.
Files changed (213) hide show
  1. package/dist/{CstToDocMap.d.ts → src/CstToDocMap.d.ts} +2 -2
  2. package/dist/src/CstToDocMap.js +1 -0
  3. package/dist/src/PrintFn.js +1 -0
  4. package/dist/src/embed.js +6 -0
  5. package/dist/src/embedJs.js +43 -0
  6. package/dist/src/embedJson.js +34 -0
  7. package/dist/src/embedSql.js +39 -0
  8. package/dist/src/index.js +64 -0
  9. package/dist/{node_utils.d.ts → src/node_utils.d.ts} +1 -0
  10. package/dist/src/node_utils.js +44 -0
  11. package/dist/{options.js → src/options.js} +1 -4
  12. package/dist/{printComment.js → src/printComment.js} +2 -6
  13. package/dist/src/printSql.js +61 -0
  14. package/dist/src/print_utils.js +49 -0
  15. package/dist/src/syntax/alias.js +3 -0
  16. package/dist/src/syntax/alter_action.js +120 -0
  17. package/dist/src/syntax/alter_table.js +19 -0
  18. package/dist/src/syntax/analyze.js +4 -0
  19. package/dist/{syntax → src/syntax}/base.js +6 -9
  20. package/dist/src/syntax/cast.js +4 -0
  21. package/dist/src/syntax/comment.js +54 -0
  22. package/dist/{syntax → src/syntax}/constraint.d.ts +1 -1
  23. package/dist/src/syntax/constraint.js +80 -0
  24. package/dist/src/syntax/create_table.js +69 -0
  25. package/dist/src/syntax/data_type.js +29 -0
  26. package/dist/src/syntax/dcl.js +83 -0
  27. package/dist/src/syntax/delete.js +5 -0
  28. package/dist/src/syntax/dialects/bigquery.js +84 -0
  29. package/dist/src/syntax/dialects/mysql.js +3 -0
  30. package/dist/src/syntax/dialects/postgresql.js +9 -0
  31. package/dist/src/syntax/dialects/sqlite.js +9 -0
  32. package/dist/src/syntax/do.js +4 -0
  33. package/dist/src/syntax/domain.js +11 -0
  34. package/dist/src/syntax/drop_table.js +11 -0
  35. package/dist/src/syntax/explain.js +7 -0
  36. package/dist/src/syntax/expr.js +224 -0
  37. package/dist/src/syntax/extension.js +13 -0
  38. package/dist/src/syntax/frame.js +13 -0
  39. package/dist/src/syntax/function.js +55 -0
  40. package/dist/src/syntax/index.js +60 -0
  41. package/dist/src/syntax/insert.js +33 -0
  42. package/dist/src/syntax/merge.js +30 -0
  43. package/dist/src/syntax/other_clauses.js +9 -0
  44. package/dist/src/syntax/parameter.js +18 -0
  45. package/dist/src/syntax/policy.js +27 -0
  46. package/dist/src/syntax/prepared_statements.js +31 -0
  47. package/dist/src/syntax/proc_clause.js +14 -0
  48. package/dist/src/syntax/procedural_language.js +104 -0
  49. package/dist/src/syntax/procedure.js +38 -0
  50. package/dist/src/syntax/program.js +39 -0
  51. package/dist/src/syntax/publication.js +27 -0
  52. package/dist/src/syntax/rename_table.js +8 -0
  53. package/dist/src/syntax/role.js +31 -0
  54. package/dist/src/syntax/schema.js +18 -0
  55. package/dist/src/syntax/select.js +226 -0
  56. package/dist/src/syntax/sequence.js +36 -0
  57. package/dist/src/syntax/subscription.js +10 -0
  58. package/dist/src/syntax/transaction.js +19 -0
  59. package/dist/src/syntax/transformMap.js +100 -0
  60. package/dist/src/syntax/trigger.js +52 -0
  61. package/dist/src/syntax/truncate.js +10 -0
  62. package/dist/src/syntax/type.js +12 -0
  63. package/dist/src/syntax/unsupported_grammar.js +4 -0
  64. package/dist/src/syntax/update.js +14 -0
  65. package/dist/src/syntax/view.js +48 -0
  66. package/dist/src/transform/addFinalSemicolon.js +9 -0
  67. package/dist/src/transform/aliasAs.js +10 -0
  68. package/dist/{transform → src/transform}/canonicKeywords.js +16 -20
  69. package/dist/{transform → src/transform}/canonicOperators.js +8 -12
  70. package/dist/src/transform/comments.js +22 -0
  71. package/dist/src/transform/stripTrailingCommas.js +15 -0
  72. package/dist/src/transform/transformCst.js +21 -0
  73. package/dist/src/transform/transformUtils.js +3 -0
  74. package/dist/src/utils.js +9 -0
  75. package/dist/src/visitAllNodes.js +16 -0
  76. package/dist/tsconfig.json +14 -0
  77. package/package.json +9 -8
  78. package/dist/CstToDocMap.js +0 -2
  79. package/dist/PrintFn.js +0 -2
  80. package/dist/embed.js +0 -14
  81. package/dist/embedJs.js +0 -99
  82. package/dist/embedJson.js +0 -90
  83. package/dist/embedSql.js +0 -89
  84. package/dist/index.js +0 -70
  85. package/dist/node_utils.js +0 -54
  86. package/dist/printSql.js +0 -71
  87. package/dist/print_utils.js +0 -68
  88. package/dist/syntax/alias.js +0 -6
  89. package/dist/syntax/alter_action.js +0 -213
  90. package/dist/syntax/alter_table.js +0 -28
  91. package/dist/syntax/analyze.js +0 -9
  92. package/dist/syntax/cast.js +0 -7
  93. package/dist/syntax/comment.js +0 -123
  94. package/dist/syntax/constraint.js +0 -116
  95. package/dist/syntax/create_table.js +0 -107
  96. package/dist/syntax/data_type.js +0 -48
  97. package/dist/syntax/dcl.js +0 -141
  98. package/dist/syntax/delete.js +0 -10
  99. package/dist/syntax/dialects/bigquery.js +0 -130
  100. package/dist/syntax/dialects/mysql.js +0 -6
  101. package/dist/syntax/dialects/postgresql.js +0 -14
  102. package/dist/syntax/dialects/sqlite.js +0 -18
  103. package/dist/syntax/do.js +0 -7
  104. package/dist/syntax/domain.js +0 -20
  105. package/dist/syntax/drop_table.js +0 -16
  106. package/dist/syntax/explain.js +0 -12
  107. package/dist/syntax/expr.js +0 -273
  108. package/dist/syntax/extension.js +0 -22
  109. package/dist/syntax/frame.js +0 -18
  110. package/dist/syntax/function.js +0 -65
  111. package/dist/syntax/index.js +0 -87
  112. package/dist/syntax/insert.js +0 -50
  113. package/dist/syntax/merge.js +0 -35
  114. package/dist/syntax/other_clauses.js +0 -18
  115. package/dist/syntax/parameter.js +0 -31
  116. package/dist/syntax/policy.js +0 -42
  117. package/dist/syntax/prepared_statements.js +0 -51
  118. package/dist/syntax/proc_clause.js +0 -19
  119. package/dist/syntax/procedural_language.js +0 -135
  120. package/dist/syntax/procedure.js +0 -42
  121. package/dist/syntax/program.js +0 -46
  122. package/dist/syntax/publication.js +0 -48
  123. package/dist/syntax/rename_table.js +0 -13
  124. package/dist/syntax/role.js +0 -56
  125. package/dist/syntax/schema.js +0 -36
  126. package/dist/syntax/select.js +0 -295
  127. package/dist/syntax/sequence.js +0 -53
  128. package/dist/syntax/subscription.js +0 -25
  129. package/dist/syntax/transaction.js +0 -34
  130. package/dist/syntax/transformMap.js +0 -64
  131. package/dist/syntax/trigger.js +0 -76
  132. package/dist/syntax/truncate.js +0 -15
  133. package/dist/syntax/type.js +0 -23
  134. package/dist/syntax/unsupported_grammar.js +0 -7
  135. package/dist/syntax/update.js +0 -21
  136. package/dist/syntax/view.js +0 -59
  137. package/dist/transform/addFinalSemicolon.js +0 -13
  138. package/dist/transform/aliasAs.js +0 -14
  139. package/dist/transform/comments.js +0 -43
  140. package/dist/transform/stripTrailingCommas.js +0 -19
  141. package/dist/transform/transformCst.js +0 -25
  142. package/dist/transform/transformUtils.js +0 -7
  143. package/dist/utils.js +0 -30
  144. package/dist/visitAllNodes.js +0 -21
  145. /package/dist/{PrintFn.d.ts → src/PrintFn.d.ts} +0 -0
  146. /package/dist/{embed.d.ts → src/embed.d.ts} +0 -0
  147. /package/dist/{embedJs.d.ts → src/embedJs.d.ts} +0 -0
  148. /package/dist/{embedJson.d.ts → src/embedJson.d.ts} +0 -0
  149. /package/dist/{embedSql.d.ts → src/embedSql.d.ts} +0 -0
  150. /package/dist/{index.d.ts → src/index.d.ts} +0 -0
  151. /package/dist/{options.d.ts → src/options.d.ts} +0 -0
  152. /package/dist/{printComment.d.ts → src/printComment.d.ts} +0 -0
  153. /package/dist/{printSql.d.ts → src/printSql.d.ts} +0 -0
  154. /package/dist/{print_utils.d.ts → src/print_utils.d.ts} +0 -0
  155. /package/dist/{syntax → src/syntax}/alias.d.ts +0 -0
  156. /package/dist/{syntax → src/syntax}/alter_action.d.ts +0 -0
  157. /package/dist/{syntax → src/syntax}/alter_table.d.ts +0 -0
  158. /package/dist/{syntax → src/syntax}/analyze.d.ts +0 -0
  159. /package/dist/{syntax → src/syntax}/base.d.ts +0 -0
  160. /package/dist/{syntax → src/syntax}/cast.d.ts +0 -0
  161. /package/dist/{syntax → src/syntax}/comment.d.ts +0 -0
  162. /package/dist/{syntax → src/syntax}/create_table.d.ts +0 -0
  163. /package/dist/{syntax → src/syntax}/data_type.d.ts +0 -0
  164. /package/dist/{syntax → src/syntax}/dcl.d.ts +0 -0
  165. /package/dist/{syntax → src/syntax}/delete.d.ts +0 -0
  166. /package/dist/{syntax → src/syntax}/dialects/bigquery.d.ts +0 -0
  167. /package/dist/{syntax → src/syntax}/dialects/mysql.d.ts +0 -0
  168. /package/dist/{syntax → src/syntax}/dialects/postgresql.d.ts +0 -0
  169. /package/dist/{syntax → src/syntax}/dialects/sqlite.d.ts +0 -0
  170. /package/dist/{syntax → src/syntax}/do.d.ts +0 -0
  171. /package/dist/{syntax → src/syntax}/domain.d.ts +0 -0
  172. /package/dist/{syntax → src/syntax}/drop_table.d.ts +0 -0
  173. /package/dist/{syntax → src/syntax}/explain.d.ts +0 -0
  174. /package/dist/{syntax → src/syntax}/expr.d.ts +0 -0
  175. /package/dist/{syntax → src/syntax}/extension.d.ts +0 -0
  176. /package/dist/{syntax → src/syntax}/frame.d.ts +0 -0
  177. /package/dist/{syntax → src/syntax}/function.d.ts +0 -0
  178. /package/dist/{syntax → src/syntax}/index.d.ts +0 -0
  179. /package/dist/{syntax → src/syntax}/insert.d.ts +0 -0
  180. /package/dist/{syntax → src/syntax}/merge.d.ts +0 -0
  181. /package/dist/{syntax → src/syntax}/other_clauses.d.ts +0 -0
  182. /package/dist/{syntax → src/syntax}/parameter.d.ts +0 -0
  183. /package/dist/{syntax → src/syntax}/policy.d.ts +0 -0
  184. /package/dist/{syntax → src/syntax}/prepared_statements.d.ts +0 -0
  185. /package/dist/{syntax → src/syntax}/proc_clause.d.ts +0 -0
  186. /package/dist/{syntax → src/syntax}/procedural_language.d.ts +0 -0
  187. /package/dist/{syntax → src/syntax}/procedure.d.ts +0 -0
  188. /package/dist/{syntax → src/syntax}/program.d.ts +0 -0
  189. /package/dist/{syntax → src/syntax}/publication.d.ts +0 -0
  190. /package/dist/{syntax → src/syntax}/rename_table.d.ts +0 -0
  191. /package/dist/{syntax → src/syntax}/role.d.ts +0 -0
  192. /package/dist/{syntax → src/syntax}/schema.d.ts +0 -0
  193. /package/dist/{syntax → src/syntax}/select.d.ts +0 -0
  194. /package/dist/{syntax → src/syntax}/sequence.d.ts +0 -0
  195. /package/dist/{syntax → src/syntax}/subscription.d.ts +0 -0
  196. /package/dist/{syntax → src/syntax}/transaction.d.ts +0 -0
  197. /package/dist/{syntax → src/syntax}/transformMap.d.ts +0 -0
  198. /package/dist/{syntax → src/syntax}/trigger.d.ts +0 -0
  199. /package/dist/{syntax → src/syntax}/truncate.d.ts +0 -0
  200. /package/dist/{syntax → src/syntax}/type.d.ts +0 -0
  201. /package/dist/{syntax → src/syntax}/unsupported_grammar.d.ts +0 -0
  202. /package/dist/{syntax → src/syntax}/update.d.ts +0 -0
  203. /package/dist/{syntax → src/syntax}/view.d.ts +0 -0
  204. /package/dist/{transform → src/transform}/addFinalSemicolon.d.ts +0 -0
  205. /package/dist/{transform → src/transform}/aliasAs.d.ts +0 -0
  206. /package/dist/{transform → src/transform}/canonicKeywords.d.ts +0 -0
  207. /package/dist/{transform → src/transform}/canonicOperators.d.ts +0 -0
  208. /package/dist/{transform → src/transform}/comments.d.ts +0 -0
  209. /package/dist/{transform → src/transform}/stripTrailingCommas.d.ts +0 -0
  210. /package/dist/{transform → src/transform}/transformCst.d.ts +0 -0
  211. /package/dist/{transform → src/transform}/transformUtils.d.ts +0 -0
  212. /package/dist/{utils.d.ts → src/utils.d.ts} +0 -0
  213. /package/dist/{visitAllNodes.d.ts → src/visitAllNodes.d.ts} +0 -0
@@ -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
  };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
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);
6
+ };
@@ -0,0 +1,43 @@
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) &&
10
+ grandParent.clauses.some(isJavaScriptLanguageClause)) {
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",
21
+ });
22
+ return [
23
+ quotes[0],
24
+ indent([hardline, stripTrailingHardline(js)]),
25
+ hardline,
26
+ quotes[1],
27
+ ];
28
+ };
29
+ }
30
+ return null;
31
+ };
32
+ const isJavaScriptLanguageClause = (clause) => isLanguageClause(clause) && clause.name.name === "js";
33
+ // Whether to quote the code with single- or double-quotes.
34
+ // Returns undefined when neither can be used without escaping.
35
+ const detectQuotes = (js) => {
36
+ if (!/'''/.test(js)) {
37
+ return ["r'''", "'''"];
38
+ }
39
+ if (!/"""/.test(js)) {
40
+ return ['r"""', '"""'];
41
+ }
42
+ return undefined;
43
+ };
@@ -0,0 +1,34 @@
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;
15
+ }
16
+ const json = await textToDoc(node.value, {
17
+ ...options,
18
+ parser: "json",
19
+ });
20
+ const inlineQuote = containsSingleQuote(node.value) ? "'''" : "'";
21
+ return [
22
+ ifBreak("'''", inlineQuote),
23
+ indent([softline, stripTrailingHardline(json)]),
24
+ softline,
25
+ ifBreak("'''", inlineQuote),
26
+ ];
27
+ };
28
+ }
29
+ return null;
30
+ };
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);
@@ -0,0 +1,39 @@
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)) &&
10
+ grandParent.clauses.some(isSqlLanguageClause)) {
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
+ };
24
+ }
25
+ return null;
26
+ };
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];
31
+ if (quote === "'") {
32
+ // Convert `'` quotes to `$$`.
33
+ // But bail out if the code already contains $$.
34
+ return node.value.includes("$$") ? undefined : "$$";
35
+ }
36
+ else {
37
+ return quote;
38
+ }
39
+ };
@@ -0,0 +1,64 @@
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 = [
9
+ {
10
+ extensions: [".sql", ".sqlite"],
11
+ name: "SQLite SQL",
12
+ parsers: ["sqlite"],
13
+ },
14
+ {
15
+ extensions: [".bigquery"],
16
+ name: "BigQuery SQL",
17
+ parsers: ["bigquery"],
18
+ },
19
+ {
20
+ extensions: [],
21
+ name: "Experimental MySQL SQL",
22
+ parsers: ["mysql"],
23
+ },
24
+ {
25
+ extensions: [],
26
+ name: "Experimental MariaDB SQL",
27
+ parsers: ["mariadb"],
28
+ },
29
+ {
30
+ extensions: [],
31
+ name: "Experimental PostgreSQL SQL",
32
+ parsers: ["postgresql"],
33
+ },
34
+ ];
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),
45
+ astFormat: "sql-cst",
46
+ locStart: (node) => node.range?.[0],
47
+ locEnd: (node) => node.range?.[1],
48
+ });
49
+ export const parsers = {
50
+ sqlite: createParser("sqlite"),
51
+ bigquery: createParser("bigquery"),
52
+ mysql: createParser("mysql"),
53
+ mariadb: createParser("mariadb"),
54
+ postgresql: createParser("postgresql"),
55
+ };
56
+ export const printers = {
57
+ "sql-cst": {
58
+ print: printSql,
59
+ embed: embed,
60
+ printComment: printComment,
61
+ canAttachComment: isNode,
62
+ isBlockComment: (node) => node.type === "block_comment",
63
+ },
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;
@@ -0,0 +1,44 @@
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);
@@ -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;
@@ -0,0 +1,61 @@
1
+ import { arrayWrap, isArray, isDefined, isEmptyArray, isString } from "./utils";
2
+ import { transformMap } from "./syntax/transformMap";
3
+ import { containsNewline, hardline, join, line } from "./print_utils";
4
+ export function printSql(path, options, oldPrint) {
5
+ return printNode(path, options, createPrintFn(path, oldPrint, options));
6
+ }
7
+ let cachedPrintFn;
8
+ let cachedPath;
9
+ function createPrintFn(path, oldPrint, opts) {
10
+ // The path parameter will reference the same AstPath instance
11
+ // during the whole printing cycle.
12
+ // So we can keep using the same print function as long as
13
+ // the path stays the same.
14
+ if (cachedPath === path) {
15
+ return cachedPrintFn;
16
+ }
17
+ cachedPath = path;
18
+ cachedPrintFn = ((selector) => {
19
+ if (isArray(selector)) {
20
+ const node = path.node;
21
+ return selector.filter((sel) => isDefined(node[sel])).map(oldPrint);
22
+ }
23
+ else {
24
+ return oldPrint(selector);
25
+ }
26
+ });
27
+ cachedPrintFn.separated = (separator, selector) => {
28
+ const node = path.node;
29
+ const docs = arrayWrap(selector)
30
+ .filter((sel) => isDefined(node[sel]))
31
+ .map((sel) => [sel, oldPrint(sel)])
32
+ // skip empty arrays
33
+ .filter(([sel, doc]) => !isEmptyArray(doc))
34
+ // only join with spaces when input to print was Node[]
35
+ .map(([sel, doc]) => (isArray(node[sel]) ? join(" ", doc) : doc));
36
+ return docs.length > 0 ? [join(separator, docs)] : [];
37
+ };
38
+ cachedPrintFn.spaced = (selector) => cachedPrintFn.separated(" ", selector);
39
+ cachedPrintFn.dynamicLine = () => {
40
+ const node = path.node;
41
+ return containsNewline(node, opts) ? hardline : line;
42
+ };
43
+ return cachedPrintFn;
44
+ }
45
+ function printNode(path, options, print) {
46
+ const node = path.node;
47
+ if (isArray(node)) {
48
+ return path.map(print);
49
+ }
50
+ if (isString(node)) {
51
+ return node;
52
+ }
53
+ if (!node?.type) {
54
+ throw new Error(`No type field on node: ${JSON.stringify(node)}`);
55
+ }
56
+ const fn = transformMap[node.type];
57
+ if (!fn) {
58
+ throw new Error(`Unexpected node type: ${node.type}`);
59
+ }
60
+ return fn(print, node, path, options);
61
+ }
@@ -0,0 +1,49 @@
1
+ import { doc, util } from "prettier";
2
+ import { isArray } from "./utils";
3
+ export const { line, hardline, softline, indent, group, lineSuffix, ifBreak, breakParent, } = doc.builders;
4
+ const { join: origJoin } = doc.builders;
5
+ export const { stripTrailingHardline } = doc.utils;
6
+ /** Improved join() that also accepts a non-array docs parameter */
7
+ export const join = (sep, docs) => isArray(docs) ? origJoin(sep, docs) : docs;
8
+ /** True when the Node contains a newline in original source code */
9
+ export const containsNewline = (node, opts) => {
10
+ if (!node.range) {
11
+ throw new Error("containsNewline() expected a Node with range info");
12
+ }
13
+ return util.hasNewlineInRange(opts.originalText, node.range[0], node.range[1]);
14
+ };
15
+ export const hasEmptyLineBetweenNodes = (node1, node2, opts) => {
16
+ if (!node1.range || !node2.range) {
17
+ throw new Error("emptyLineBetweenNodes() expects Nodes with range info");
18
+ }
19
+ const start = node1.range[1];
20
+ const end = node2.range[0];
21
+ const indexes = emptyLineIndexes(opts.originalText.slice(start, end)).map((i) => start + i);
22
+ // when no empty lines between nodes, we can return early
23
+ if (indexes.length === 0) {
24
+ return false;
25
+ }
26
+ // otherwise we need to check whether the empty line happens to be
27
+ // inside a comment or between comments
28
+ const comments = [...(node1.trailing ?? []), ...(node2.leading ?? [])];
29
+ return (indexes.filter((index) => !isIndexInsideOrBetweenComments(index, comments))
30
+ .length > 0);
31
+ };
32
+ function emptyLineIndexes(text) {
33
+ const indexes = [];
34
+ const re = /\n[ \t]*\r?\n/g;
35
+ let match;
36
+ while ((match = re.exec(text))) {
37
+ indexes.push(match.index);
38
+ }
39
+ return indexes;
40
+ }
41
+ function isIndexInsideOrBetweenComments(index, comments) {
42
+ if (comments.length === 0) {
43
+ return false;
44
+ }
45
+ // Assume all nodes have range info
46
+ const start = comments[0].range[0];
47
+ const end = comments[comments.length - 1].range[1];
48
+ return start <= index && index <= end;
49
+ }
@@ -0,0 +1,3 @@
1
+ export const aliasMap = {
2
+ alias: (print) => print.spaced(["expr", "asKw", "alias", "columnAliases"]),
3
+ };
@@ -0,0 +1,120 @@
1
+ import { group, indent, join, line } from "../print_utils";
2
+ import { printUnnamedConstraint } from "./constraint";
3
+ export const alterActionMap = {
4
+ alter_action_rename: (print) => print.spaced(["renameKw", "newName"]),
5
+ alter_action_rename_column: (print) => print.spaced(["renameKw", "ifExistsKw", "oldName", "toKw", "newName"]),
6
+ alter_action_add_column: (print) => print.spaced(["addKw", "ifNotExistsKw", "column"]),
7
+ alter_action_drop_column: (print) => print.spaced(["dropKw", "ifExistsKw", "column", "behaviorKw"]),
8
+ alter_action_set_bigquery_options: (print) => print.spaced(["setKw", "options"]),
9
+ alter_action_set_default_collate: (print) => print.spaced(["setDefaultCollateKw", "collation"]),
10
+ /** cst-ignore: constraint, modifiers */
11
+ alter_action_add_constraint: (print, node) => group([
12
+ print.spaced(["addKw", "name"]),
13
+ indent([line, printUnnamedConstraint(print, node)]),
14
+ ]),
15
+ alter_action_add_constraint_constraint_name: (print) => print.spaced(["constraintKw", "ifNotExistsKw", "name"]),
16
+ alter_action_drop_constraint: (print) => print.spaced([
17
+ "dropConstraintKw",
18
+ "ifExistsKw",
19
+ "constraint",
20
+ "behaviorKw",
21
+ ]),
22
+ alter_action_drop_primary_key: (print) => print.spaced(["dropPrimaryKeyKw", "ifExistsKw"]),
23
+ alter_action_alter_constraint: (print) => print.spaced(["alterConstraintKw", "constraint", "modifiers"]),
24
+ alter_action_rename_constraint: (print) => print.spaced(["renameConstraintKw", "oldName", "toKw", "newName"]),
25
+ alter_action_validate_constraint: (print) => print.spaced(["validateConstraintKw", "constraint"]),
26
+ alter_action_set_visible: (print) => print.spaced("setVisibleKw"),
27
+ alter_action_set_invisible: (print) => print.spaced("setInvisibleKw"),
28
+ alter_action_set_tablespace: (print) => print.spaced(["setTablespaceKw", "tablespace", "nowaitKw"]),
29
+ alter_action_set_schema: (print) => print.spaced(["setSchemaKw", "schema"]),
30
+ alter_action_enable: (print) => print.spaced(["enableKw", "modeKw", "item"]),
31
+ alter_action_disable: (print) => print.spaced(["disableKw", "item"]),
32
+ alter_action_force: (print) => print.spaced(["forceKw", "item"]),
33
+ alter_action_no_force: (print) => print.spaced(["noForceKw", "item"]),
34
+ alter_action_cluster_on: (print) => print.spaced(["clusterOnKw", "index"]),
35
+ alter_action_set_without_cluster: (print) => print.spaced("setWithoutClusterKw"),
36
+ alter_action_set_without_oids: (print) => print.spaced("setWithoutOidsKw"),
37
+ alter_action_set_access_method: (print) => print.spaced(["setAccessMethodKw", "method"]),
38
+ alter_action_set_logged: (print) => print.spaced("setLoggedKw"),
39
+ alter_action_set_unlogged: (print) => print.spaced("setUnloggedKw"),
40
+ alter_action_inherit: (print) => print.spaced(["inheritKw", "table"]),
41
+ alter_action_no_inherit: (print) => print.spaced(["noInheritKw", "table"]),
42
+ alter_action_of_type: (print) => print.spaced(["ofKw", "typeName"]),
43
+ alter_action_not_of_type: (print) => print.spaced("notOfKw"),
44
+ alter_action_owner_to: (print) => print.spaced(["ownerToKw", "owner"]),
45
+ alter_action_replica_identity: (print) => print.spaced(["replicaIdentityKw", "identity"]),
46
+ replica_identity_using_index: (print) => print.spaced(["usingIndexKw", "index"]),
47
+ alter_action_set_postgresql_options: (print) => print.spaced(["setKw", "options"]),
48
+ alter_action_reset_postgresql_options: (print) => print.spaced(["resetKw", "options"]),
49
+ alter_action_depends_on_extension: (print) => print.spaced(["dependsOnExtensionKw", "extension"]),
50
+ alter_action_no_depends_on_extension: (print) => print.spaced(["noDependsOnExtensionKw", "extension"]),
51
+ alter_action_attach_partition: (print) => print.spaced(["attachPartitionKw", "index"]),
52
+ alter_action_add_enum_value: (print) => print.spaced(["addValueKw", "ifNotExistsKw", "value", "position"]),
53
+ alter_action_add_enum_value_position: (print) => print.spaced(["positionKw", "value"]),
54
+ alter_action_rename_enum_value: (print) => print.spaced(["renameValueKw", "oldValue", "toKw", "newValue"]),
55
+ alter_action_add_attribute: (print) => print.spaced([
56
+ "addAttributeKw",
57
+ "name",
58
+ "dataType",
59
+ "constraint",
60
+ "behaviorKw",
61
+ ]),
62
+ alter_action_drop_attribute: (print) => print.spaced(["dropAttributeKw", "ifExistsKw", "name", "behaviorKw"]),
63
+ alter_action_alter_attribute: (print) => print.spaced([
64
+ "alterAttributeKw",
65
+ "name",
66
+ "setDataTypeKw",
67
+ "dataType",
68
+ "constraint",
69
+ "behaviorKw",
70
+ ]),
71
+ alter_action_rename_attribute: (print) => print.spaced([
72
+ "renameAttributeKw",
73
+ "oldName",
74
+ "toKw",
75
+ "newName",
76
+ "behaviorKw",
77
+ ]),
78
+ // ALTER COLUMN
79
+ alter_action_alter_column: (print) => group([
80
+ print.spaced(["alterKw", "ifExistsKw", "column"]),
81
+ print.dynamicLine(),
82
+ print("action"),
83
+ ]),
84
+ alter_action_set_default: (print) => print.spaced(["setDefaultKw", "expr"]),
85
+ alter_action_drop_default: (print) => print.spaced("dropDefaultKw"),
86
+ alter_action_set_not_null: (print) => print.spaced("setNotNullKw"),
87
+ alter_action_drop_not_null: (print) => print.spaced("dropNotNullKw"),
88
+ alter_action_set_data_type: (print) => print.spaced(["setDataTypeKw", "dataType", "clauses"]),
89
+ alter_action_set_statistics: (print) => print.spaced(["setStatisticsKw", "value"]),
90
+ alter_action_set_compression: (print) => print.spaced(["setCompressionKw", "method"]),
91
+ alter_action_set_storage: (print) => print.spaced(["setStorageKw", "typeKw"]),
92
+ alter_action_drop_expression: (print) => print.spaced(["dropExpressionKw", "ifExistsKw"]),
93
+ alter_action_drop_identity: (print) => print.spaced(["dropIdentityKw", "ifExistsKw"]),
94
+ alter_action_add_identity: (print) => print.spaced([
95
+ "addGeneratedKw",
96
+ "whenKw",
97
+ "asIdentityKw",
98
+ "sequenceOptions",
99
+ ]),
100
+ alter_action_alter_identity: (print) => group(join(line, print("actions"))),
101
+ alter_action_set_generated: (print) => print.spaced(["setGeneratedKw", "whenKw"]),
102
+ alter_action_restart: (print) => print.spaced(["restartKw", "withKw", "value"]),
103
+ alter_action_set_sequence_option: (print) => print.spaced(["setKw", "option"]),
104
+ alter_action_with_role_options: (print, node) => node.withKw
105
+ ? group([print("withKw"), indent([line, join(line, print("options"))])])
106
+ : // This works in tandem with the logic inside alter_role_stmt
107
+ group(join(line, print("options"))),
108
+ alter_action_set_postgresql_option: (print) => print.spaced(["setKw", "name", "operator", "value"]),
109
+ alter_action_set_postgresql_option_from_current: (print) => print.spaced(["setKw", "name", "fromCurrentKw"]),
110
+ alter_action_reset_postgresql_option: (print) => print.spaced(["resetKw", "name"]),
111
+ alter_action_add_user: (print) => group(print.spaced(["addUserKw", "users"])),
112
+ alter_action_drop_user: (print) => group(print.spaced(["dropUserKw", "users"])),
113
+ // ENABLE/DISABLE ..
114
+ toggle_trigger: (print) => print.spaced(["triggerKw", "name"]),
115
+ toggle_rule: (print) => print.spaced(["ruleKw", "name"]),
116
+ toggle_row_level_security: (print) => print.spaced("rowLevelSecurityKw"),
117
+ // SET DATA TYPE ...
118
+ set_data_type_collate_clause: (print) => print.spaced(["collateKw", "collation"]),
119
+ set_data_type_using_clause: (print) => print.spaced(["usingKw", "expr"]),
120
+ };
@@ -0,0 +1,19 @@
1
+ import { group, indent, line } from "../print_utils";
2
+ export const alterTableMap = {
3
+ alter_table_stmt: (print) => group([
4
+ print.spaced(["alterTableKw", "ifExistsKw", "table"]),
5
+ print.dynamicLine(),
6
+ print("actions"),
7
+ ]),
8
+ alter_table_all_in_tablespace_stmt: (print) => group([
9
+ print.spaced([
10
+ "alterTableKw",
11
+ "allInTablespaceKw",
12
+ "tablespace",
13
+ "ownedBy",
14
+ ]),
15
+ print.dynamicLine(),
16
+ print("action"),
17
+ ]),
18
+ owned_by_clause: (print) => group([print.spaced("ownedByKw"), indent([line, print("owners")])]),
19
+ };
@@ -0,0 +1,4 @@
1
+ import { group } from "../print_utils";
2
+ export const analyzeMap = {
3
+ analyze_stmt: (print) => group(print.spaced(["analyzeKw", "tableKw", "tables"])),
4
+ };
@@ -1,11 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.baseMap = void 0;
4
- var node_utils_1 = require("../node_utils");
5
- exports.baseMap = {
1
+ import { isDataType } from "../node_utils";
2
+ export const baseMap = {
6
3
  /** cst-ignore: name */
7
- keyword: function (print, node, path, options) {
8
- var keywordCase = (0, node_utils_1.isDataType)(path.ancestors)
4
+ keyword: (print, node, path, options) => {
5
+ const keywordCase = isDataType(path.ancestors)
9
6
  ? options.sqlTypeCase
10
7
  : options.sqlKeywordCase;
11
8
  switch (keywordCase) {
@@ -17,6 +14,6 @@ exports.baseMap = {
17
14
  return path.node.text.toLowerCase();
18
15
  }
19
16
  },
20
- all_columns: function () { return "*"; },
21
- empty: function () { return []; },
17
+ all_columns: () => "*",
18
+ empty: () => [],
22
19
  };
@@ -0,0 +1,4 @@
1
+ import { group } from "../print_utils";
2
+ export const castMap = {
3
+ cast_definition: (print) => group(print.spaced(["from", "asKw", "to"])),
4
+ };
@@ -0,0 +1,54 @@
1
+ import { group, indent, line } from "../print_utils";
2
+ export const commentMap = {
3
+ comment_stmt: (print) => group([
4
+ print.spaced(["commentKw", "onKw", "target", "isKw"]),
5
+ indent([line, print("message")]),
6
+ ]),
7
+ comment_target_aggregate: (print) => group([print.spaced(["aggregateKw", "name"]), print(["params"])]),
8
+ comment_target_function: (print) => group([print.spaced(["functionKw", "name"]), print(["params"])]),
9
+ comment_target_procedure: (print) => group([print.spaced(["procedureKw", "name"]), print(["params"])]),
10
+ comment_target_routine: (print) => group([print.spaced(["routineKw", "name"]), print(["params"])]),
11
+ comment_target_access_method: (print) => group(print.spaced(["accessMethodKw", "name"])),
12
+ comment_target_cast: (print) => group(print.spaced(["castKw", "args"])),
13
+ comment_target_collation: (print) => group(print.spaced(["collationKw", "name"])),
14
+ comment_target_column: (print) => group(print.spaced(["columnKw", "name"])),
15
+ comment_target_table_constraint: (print) => group(print.spaced(["constraintKw", "name", "onKw", "tableName"])),
16
+ comment_target_domain_constraint: (print) => group(print.spaced(["constraintKw", "name", "onKw", "domainKw", "domainName"])),
17
+ comment_target_conversion: (print) => group(print.spaced(["conversionKw", "name"])),
18
+ comment_target_database: (print) => group(print.spaced(["databaseKw", "name"])),
19
+ comment_target_domain: (print) => group(print.spaced(["domainKw", "name"])),
20
+ comment_target_extension: (print) => group(print.spaced(["extensionKw", "name"])),
21
+ comment_target_event_trigger: (print) => group(print.spaced(["eventTriggerKw", "name"])),
22
+ comment_target_foreign_data_wrapper: (print) => group(print.spaced(["foreignDataWrapperKw", "name"])),
23
+ comment_target_foreign_table: (print) => group(print.spaced(["foreignTableKw", "name"])),
24
+ comment_target_index: (print) => group(print.spaced(["indexKw", "name"])),
25
+ comment_target_language: (print) => group(print.spaced(["languageKw", "name"])),
26
+ comment_target_large_object: (print) => group(print.spaced(["largeObjectKw", "oid"])),
27
+ comment_target_materialized_view: (print) => group(print.spaced(["materializedViewKw", "name"])),
28
+ comment_target_operator_class: (print) => group(print.spaced(["operatorClassKw", "name", "usingKw", "methodName"])),
29
+ comment_target_operator_family: (print) => group(print.spaced(["operatorFamilyKw", "name", "usingKw", "methodName"])),
30
+ comment_target_policy: (print) => group(print.spaced(["policyKw", "name", "onKw", "tableName"])),
31
+ comment_target_publication: (print) => group(print.spaced(["publicationKw", "name"])),
32
+ comment_target_role: (print) => group(print.spaced(["roleKw", "name"])),
33
+ comment_target_rule: (print) => group(print.spaced(["ruleKw", "name", "onKw", "tableName"])),
34
+ comment_target_schema: (print) => group(print.spaced(["schemaKw", "name"])),
35
+ comment_target_sequence: (print) => group(print.spaced(["sequenceKw", "name"])),
36
+ comment_target_server: (print) => group(print.spaced(["serverKw", "name"])),
37
+ comment_target_statistics: (print) => group(print.spaced(["statisticsKw", "name"])),
38
+ comment_target_subscription: (print) => group(print.spaced(["subscriptionKw", "name"])),
39
+ comment_target_table: (print) => group(print.spaced(["tableKw", "name"])),
40
+ comment_target_tablespace: (print) => group(print.spaced(["tablespaceKw", "name"])),
41
+ comment_target_text_search_configuration: (print) => group(print.spaced(["textSearchConfigurationKw", "name"])),
42
+ comment_target_text_search_dictionary: (print) => group(print.spaced(["textSearchDictionaryKw", "name"])),
43
+ comment_target_text_search_parser: (print) => group(print.spaced(["textSearchParserKw", "name"])),
44
+ comment_target_text_search_template: (print) => group(print.spaced(["textSearchTemplateKw", "name"])),
45
+ comment_target_transform: (print) => group(print.spaced([
46
+ "transformForKw",
47
+ "typeName",
48
+ "languageKw",
49
+ "languageName",
50
+ ])),
51
+ comment_target_trigger: (print) => group(print.spaced(["triggerKw", "name", "onKw", "tableName"])),
52
+ comment_target_type: (print) => group(print.spaced(["typeKw", "name"])),
53
+ comment_target_view: (print) => group(print.spaced(["viewKw", "name"])),
54
+ };