prettier-plugin-sql
Version:
An opinionated sql formatter plugin for Prettier
561 lines (553 loc) • 15.4 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var jsox = require('jsox');
var nodeSqlParser = require('node-sql-parser');
var sqlFormatter = require('sql-formatter');
const languages = [
{
"name": "HiveQL",
"aceMode": "sql",
"since": "0.1.0",
"parsers": [
"sql"
],
"linguistLanguageId": 931814087,
"vscodeLanguageIds": [
"sql"
],
"extensions": [
".q",
".hql"
],
"tmScope": "source.hql"
},
{
"name": "PLSQL",
"aceMode": "sql",
"since": "0.1.0",
"parsers": [
"sql"
],
"linguistLanguageId": 273,
"vscodeLanguageIds": [
"sql"
],
"codemirrorMode": "sql",
"codemirrorMimeType": "text/x-plsql",
"tmScope": "none",
"extensions": [
".pls",
".bdy",
".ddl",
".fnc",
".pck",
".pkb",
".pks",
".plb",
".plsql",
".prc",
".spc",
".sql",
".tpb",
".tps",
".trg",
".vw"
]
},
{
"name": "PLpgSQL",
"aceMode": "pgsql",
"since": "0.1.0",
"parsers": [
"sql"
],
"linguistLanguageId": 274,
"vscodeLanguageIds": [
"pgsql"
],
"codemirrorMode": "sql",
"codemirrorMimeType": "text/x-sql",
"tmScope": "source.sql",
"extensions": [
".pgsql",
".sql"
]
},
{
"name": "SQL",
"aceMode": "sql",
"since": "0.1.0",
"parsers": [
"sql"
],
"linguistLanguageId": 333,
"vscodeLanguageIds": [
"sql"
],
"tmScope": "source.sql",
"codemirrorMode": "sql",
"codemirrorMimeType": "text/x-sql",
"extensions": [
".sql",
".cql",
".ddl",
".inc",
".mysql",
".prc",
".tab",
".udf",
".viw"
]
},
{
"name": "SQLPL",
"aceMode": "sql",
"since": "0.1.0",
"parsers": [
"sql"
],
"linguistLanguageId": 334,
"vscodeLanguageIds": [
"sql"
],
"codemirrorMode": "sql",
"codemirrorMimeType": "text/x-sql",
"tmScope": "source.sql",
"extensions": [
".sql",
".db2"
]
},
{
"name": "TSQL",
"aceMode": "sql",
"since": "0.1.0",
"parsers": [
"sql"
],
"linguistLanguageId": 918334941,
"vscodeLanguageIds": [
"sql"
],
"extensions": [
".sql"
],
"tmScope": "source.tsql"
}
];
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
const parser = new nodeSqlParser.Parser();
const SQL_FORMATTER = "sql-formatter";
const NODE_SQL_PARSER = "node-sql-parser";
const SQL_CST = "sql-cst";
const SqlPlugin = {
languages,
parsers: {
sql: {
// eslint-disable-next-line sonarjs/function-return-type
parse(text, { formatter, type, database }) {
return formatter === SQL_FORMATTER ? text : parser.astify(text, { type, database });
},
astFormat: "sql",
locStart: () => -1,
locEnd: () => -1
}
},
printers: {
sql: {
print(path, _a) {
var _b = _a, { type, database, dialect, params, paramTypes } = _b, options = __objRest(_b, ["type", "database", "dialect", "params", "paramTypes"]);
const value = path.node;
let formatted;
if (typeof value === "string") {
const sqlFormatterOptions = __spreadProps(__spreadValues({}, options), {
params: params == null ? void 0 : jsox.JSOX.parse(params),
paramTypes: paramTypes == null ? void 0 : jsox.JSOX.parse(
paramTypes
)
});
formatted = dialect == null ? sqlFormatter.format(value, sqlFormatterOptions) : sqlFormatter.formatDialect(value, __spreadProps(__spreadValues({}, sqlFormatterOptions), {
dialect: jsox.JSOX.parse(dialect)
}));
} else {
formatted = parser.sqlify(value, { type, database });
}
return formatted + "\n";
}
}
},
options: {
formatter: {
// since: '0.1.0',
category: "Config",
type: "choice",
default: SQL_FORMATTER,
description: "Choose which formatter to be used",
choices: [
{
value: SQL_FORMATTER,
description: "use `sql-formatter` as formatter"
},
{
value: NODE_SQL_PARSER,
description: "use `node-sql-parser` as formatter"
},
{
value: SQL_CST,
description: "use `prettier-plugin-sql-cst` under the hood"
}
]
},
dialect: {
// since: '0.18.0',
category: "Config",
type: "string",
description: "SQL dialect for `sql-formatter` formatDialect()"
},
language: {
// since: '0.1.0',
category: "Config",
type: "choice",
default: "sql",
description: "SQL dialect for `sql-formatter` format()",
choices: [
{
value: "sql",
description: "Standard SQL: https://en.wikipedia.org/wiki/SQL:2011"
},
{
value: "bigquery",
description: "Google Standard SQL (Bigquery): https://cloud.google.com/bigquery"
},
{
value: "db2",
description: "IBM DB2: https://www.ibm.com/products/db2"
},
{
value: "db2i",
description: "IBM DB2i (experimental): https://www.ibm.com/docs/en/i/7.5?topic=overview-db2-i"
},
{
value: "hive",
description: "Apache Hive: https://hive.apache.org"
},
{
value: "mariadb",
description: "MariaDB: https://mariadb.com"
},
{
value: "mysql",
description: "MySQL: https://www.mysql.com"
},
{
value: "n1ql",
description: "Couchbase N1QL: https://www.couchbase.com/products/n1ql"
},
{
value: "plsql",
description: "Oracle PL/SQL: https://www.oracle.com/database/technologies/appdev/plsql.html"
},
{
value: "postgresql",
description: "PostgreSQL: https://www.postgresql.org"
},
{
value: "redshift",
description: "Amazon Redshift: https://docs.aws.amazon.com/redshift/latest/dg/cm_chap_SQLCommandRef.html"
},
{
value: "singlestoredb",
description: "SingleStoreDB: https://docs.singlestore.com/db/v7.8/en/introduction/singlestore-documentation.html"
},
{
value: "snowflake",
description: "Snowflake: https://docs.snowflake.com"
},
{
value: "spark",
description: "Spark: https://spark.apache.org"
},
{
value: "sqlite",
description: "SQLite: https://www.sqlite.org"
},
{
value: "transactsql",
description: "SQL Server Transact-SQL: https://docs.microsoft.com/en-us/sql/sql-server/"
},
{
value: "tsql",
description: "SQL Server Transact-SQL: https://docs.microsoft.com/en-us/sql/sql-server/"
},
{
value: "trino",
description: "Trino: https://trino.io"
}
]
},
keywordCase: {
// since: '0.7.0',
category: "Output",
type: "choice",
default: "preserve",
description: "Converts reserved keywords to upper- or lowercase for `sql-formatter`",
choices: [
{
value: "preserve",
description: "preserves the original case of reserved keywords"
},
{
value: "upper",
description: "converts reserved keywords to uppercase"
},
{
value: "lower",
description: "converts reserved keywords to lowercase"
}
]
},
dataTypeCase: {
// since: '0.18.0',
category: "Output",
type: "choice",
default: "preserve",
description: "Converts data types to upper- or lowercase for `sql-formatter`",
choices: [
{
value: "preserve",
description: "preserves the original case of data types"
},
{
value: "upper",
description: "converts data types to uppercase"
},
{
value: "lower",
description: "converts data types to lowercase"
}
]
},
functionCase: {
// since: '0.18.0',
category: "Output",
type: "choice",
default: "preserve",
description: "Converts functions to upper- or lowercase for `sql-formatter`",
choices: [
{
value: "preserve",
description: "preserves the original case of functions"
},
{
value: "upper",
description: "converts functions to uppercase"
},
{
value: "lower",
description: "converts functions to lowercase"
}
]
},
identifierCase: {
// since: '0.17.0',
category: "Output",
type: "choice",
default: "preserve",
description: "Converts identifiers to upper- or lowercase for `sql-formatter`. Only unquoted identifiers are converted. (experimental)",
choices: [
{
value: "preserve",
description: "preserves the original case of identifiers"
},
{
value: "upper",
description: "converts identifiers to uppercase"
},
{
value: "lower",
description: "converts identifiers to lowercase"
}
]
},
uppercase: {
// since: '0.1.0',
category: "Output",
type: "boolean",
deprecated: "0.7.0",
description: "Use `keywordCase` option instead"
},
indentStyle: {
// since: '0.7.0',
category: "Format",
type: "choice",
default: "standard",
description: `Switches between different indentation styles for \`sql-formatter\`.
Caveats of using \`"tabularLeft"\` and \`"tabularRight"\`:
- \`tabWidth\` option is ignored. Indentation will always be 10 spaces, regardless of what is specified by \`tabWidth\``,
choices: [
{
value: "standard",
description: "indents code by the amount specified by `tabWidth` option"
},
{
value: "tabularLeft",
description: "indents in tabular style with 10 spaces, aligning keywords to left"
},
{
value: "tabularRight",
description: "indents in tabular style with 10 spaces, aligning keywords to right"
}
]
},
logicalOperatorNewline: {
// since: '0.7.0',
category: "Format",
type: "choice",
default: "before",
description: "Decides newline placement before or after logical operators (AND, OR, XOR)",
choices: [
{
value: "before",
description: "adds newline before the operator"
},
{
value: "after",
description: "adds newline after the operator"
}
]
},
expressionWidth: {
// since: '0.7.0',
category: "Format",
type: "int",
default: 50,
description: "Determines maximum length of parenthesized expressions for `sql-formatter`"
},
linesBetweenQueries: {
// since: '0.1.0',
category: "Format",
type: "int",
default: 1,
description: "Decides how many empty lines to leave between SQL statements for `sql-formatter`"
},
denseOperators: {
// since: '0.7.0',
category: "Format",
type: "boolean",
default: false,
description: "Decides whitespace around operators for `sql-formatter`. Does not apply to logical operators (AND, OR, XOR)."
},
newlineBeforeSemicolon: {
// since: '0.7.0',
category: "Format",
type: "boolean",
default: false,
description: "Whether to place query separator (`;`) on a separate line for `sql-formatter`"
},
params: {
// since: '0.7.0',
category: "Format",
type: "string",
description: "Specifies `JSOX` **stringified** parameter values to fill in for placeholders inside SQL for `sql-formatter`. This option is designed to be used through API (though nothing really prevents usage from command line)."
},
paramTypes: {
// since: '0.11.0',
category: "Config",
type: "string",
description: "Specifies `JSOX` **stringified** parameter types to support when parsing SQL prepared statements for `sql-formatter`."
},
type: {
// since: '0.1.0',
category: "Config",
type: "choice",
default: "table",
description: "Check the SQL with Authority List for `node-sql-parser`",
choices: [
{
value: "table",
description: "`table` mode"
},
{
value: "column",
description: "`column` mode"
}
]
},
database: {
// since: '0.1.0',
category: "Config",
type: "choice",
default: "mysql",
description: "SQL dialect for `node-sql-parser`",
choices: [
{
value: "bigquery",
description: "BigQuery: https://cloud.google.com/bigquery"
},
{
value: "db2",
description: "IBM DB2: https://www.ibm.com/analytics/db2"
},
{
value: "hive",
description: "Hive: https://hive.apache.org"
},
{
value: "mariadb",
description: "MariaDB: https://mariadb.com"
},
{
value: "mysql",
description: "MySQL: https://www.mysql.com"
},
{
value: "postgresql",
description: "PostgreSQL: https://www.postgresql.org"
},
{
value: "transactsql",
description: "TransactSQL: https://docs.microsoft.com/en-us/sql/t-sql"
},
{
value: "flinksql",
description: "FlinkSQL: https://ci.apache.org/projects/flink/flink-docs-stable"
},
{
value: "snowflake",
description: "Snowflake (alpha): https://docs.snowflake.com"
}
]
}
}
};
exports.default = SqlPlugin;
;