un-ts / prettier

:package: Opinionated but Incredible Prettier plugins.
https://prettier.vercel.app
MIT License
261 stars 23 forks source link

prettier-plugin-sql: Allow `dialect` as alternative to `language` #322

Closed karlhorky closed 6 months ago

karlhorky commented 7 months ago

Hi @JounQin, hope you're good! 👋

sql-formatter has an option called dialect, which since v12 is a superior version of the language option, allowing also for custom dialects.

This would mean also switching between the format() function and the new formatDialect() function when the dialect option is passed (also in the dialect docs above).

JounQin commented 7 months ago

This can be supported like #301 due to prettier's limitation, see also https://github.com/prettier/prettier/issues/14671

language and dialect should be supported at the same time.

PR welcome, or I can do it myself.

JounQin commented 7 months ago

By the way, can you help to finish #217 first?

karlhorky commented 7 months ago

By the way, can you help to finish #217 first?

Yes, for #217 I'll add the docs for prettier-plugin-sql + prettier-plugin-embed to both projects in the next days.

karlhorky commented 7 months ago

PR welcome, or I can do it myself

If you could do the dialect option, would be cool! 🙌

Here's my quick and dirty patch (using JSOX) of switching the language option to dialect (also includes upcoming dataTypeCase and functionCase options):

diff --git a/node_modules/prettier-plugin-sql/lib/index.cjs b/node_modules/prettier-plugin-sql/lib/index.cjs
index 5413abc..aac0388 100644
--- a/node_modules/prettier-plugin-sql/lib/index.cjs
+++ b/node_modules/prettier-plugin-sql/lib/index.cjs
@@ -206,7 +206,8 @@ const SqlPlugin = {
           "paramTypes"
         ]);
         const value = path.node;
-        let formatted = typeof value === "string" ? sqlFormatter.format(value, __spreadProps(__spreadValues({}, options), {
+        options.dialect = jsox.parse(options.dialect);
+        let formatted = typeof value === "string" ? sqlFormatter.formatDialect(value, __spreadProps(__spreadValues({}, options), {
           params: params == null ? void 0 : jsox.JSOX.parse(
             params
           ),
@@ -242,6 +243,13 @@ const SqlPlugin = {
         }
       ]
     },
+    dialect: {
+      // since: '0.18.0',
+      category: "Config",
+      type: "string",
+      description: "SQL Formatter dialect for `sql-formatter`"
+    },
     language: {
       // since: '0.1.0',
       category: "Config",
@@ -365,6 +373,48 @@ const SqlPlugin = {
         }
       ]
     },
+    dataTypeCase: {
+      // since: '0.18.0',
+      category: "Output",
+      type: "choice",
+      default: "preserve",
+      description: "Converts data types to upper- or lowercase for `sql-formatter`. (experimental)",
+      choices: [
+        {
+          value: "preserve",
+          description: "preserves the original case"
+        },
+        {
+          value: "upper",
+          description: "converts to uppercase"
+        },
+        {
+          value: "lower",
+          description: "converts to lowercase"
+        }
+      ]
+    },
+    functionCase: {
+      // since: '0.18.0',
+      category: "Output",
+      type: "choice",
+      default: "preserve",
+      description: "Converts function names to upper- or lowercase for `sql-formatter`.",
+      choices: [
+        {
+          value: "preserve",
+          description: "preserves the original case"
+        },
+        {
+          value: "upper",
+          description: "converts to uppercase"
+        },
+        {
+          value: "lower",
+          description: "converts to lowercase"
+        }
+      ]
+    },
     uppercase: {
       // since: '0.1.0',
       category: "Output",
diff --git a/node_modules/prettier-plugin-sql/lib/index.d.ts b/node_modules/prettier-plugin-sql/lib/index.d.ts
index 51be042..cdff6c0 100644
--- a/node_modules/prettier-plugin-sql/lib/index.d.ts
+++ b/node_modules/prettier-plugin-sql/lib/index.d.ts
@@ -1,11 +1,11 @@
 /// <reference path="../shim.d.ts" />
 import type { AST, Option } from 'node-sql-parser';
 import type { Options, ParserOptions, Plugin } from 'prettier';
-import { type FormatOptionsWithLanguage } from 'sql-formatter';
+import { type FormatOptionsWithLanguage, FormatOptions } from 'sql-formatter';
 declare const SQL_FORMATTER = "sql-formatter";
 declare const NODE_SQL_PARSER = "node-sql-parser";
 declare const SQL_CST = "sql-cst";
-export type SqlBaseOptions = Option & Partial<FormatOptionsWithLanguage> & {
+export type SqlBaseOptions = Option & Partial<FormatOptionsWithLanguage | FormatOptions & { dialect: string }> & {
     formatter?: typeof NODE_SQL_PARSER | typeof SQL_CST | typeof SQL_FORMATTER;
     params?: string;
     paramTypes?: string;

Notes:


And my Prettier config for using the dialect option, uses dialect: JSON.stringify(postgres) to pass option as string:

prettier.config.mjs

import { postgresql } from 'sql-formatter';

/** @type {import('prettier').Config} */
const prettierConfig = {
  plugins: ['prettier-plugin-embed', 'prettier-plugin-sql'],
};

/** @type {import('prettier-plugin-embed').PrettierPluginEmbedOptions} */
const prettierPluginEmbedConfig = {
  embeddedSqlIdentifiers: ['sql'],
};

/** @type {import('prettier-plugin-sql').SqlBaseOptions} */
const prettierPluginSqlConfig = {
  dialect: JSON.stringify(postgresql),
  keywordCase: 'upper',
  identifierCase: 'lower',
  dataTypeCase: 'lower',
  functionCase: 'lower',
  // - Wrap all parenthesized expressions to new lines (eg. `INSERT` columns)
  // - Do not wrap foreign keys (eg. `REFERENCES table_name (id)`)
  // - Do not wrap column type expressions (eg. `VARCHAR(255)`)
  expressionWidth: 8,
};

const config = {
  ...prettierConfig,
  ...prettierPluginEmbedConfig,
  ...prettierPluginSqlConfig,
};

export default config;
karlhorky commented 6 months ago

@JounQin I found some time to do this:

karlhorky commented 6 months ago

I can confirm the dialect option is working in prettier-plugin-sql@0.18.0 🎉 Thanks for the release.