Closed karlhorky closed 6 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.
By the way, can you help to finish #217 first?
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.
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:
prettier-plugin-sql
should continue supporting the language
option, it will need to be modified to switch between format()
and formatDialect()
prettier-embed-sql
, but maybe I made a simple mistake there https://github.com/Sec-ant/prettier-plugin-embed/issues/45And 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;
@JounQin I found some time to do this:
I can confirm the dialect
option is working in prettier-plugin-sql@0.18.0
🎉 Thanks for the release.
Hi @JounQin, hope you're good! 👋
sql-formatter
has an option calleddialect
, which since v12 is a superior version of thelanguage
option, allowing also for custom dialects.This would mean also switching between the
format()
function and the newformatDialect()
function when thedialect
option is passed (also in thedialect
docs above).