prettier-plugin-sql
Advanced tools
Comparing version 0.17.1 to 0.18.0
/// <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 FormatOptions, type FormatOptionsWithLanguage } 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<(FormatOptions & { | ||
dialect: string; | ||
}) | (FormatOptionsWithLanguage & { | ||
dialect?: never; | ||
})> & { | ||
formatter?: typeof NODE_SQL_PARSER | typeof SQL_CST | typeof SQL_FORMATTER; | ||
@@ -10,0 +14,0 @@ params?: string; |
@@ -1,5 +0,6 @@ | ||
import { JSOX } from 'jsox'; | ||
import * as _JSOX from 'jsox'; | ||
import nodeSqlParser from 'node-sql-parser'; | ||
import { format } from 'sql-formatter'; | ||
import { format, formatDialect, } from 'sql-formatter'; | ||
import { languages } from './languages.js'; | ||
const JSOX = _JSOX.JSOX || _JSOX; | ||
const parser = new nodeSqlParser.Parser(); | ||
@@ -30,6 +31,7 @@ const SQL_FORMATTER = 'sql-formatter'; | ||
sql: { | ||
print(path, { type, database, endOfLine, params, paramTypes, ...options }) { | ||
print(path, { type, database, dialect, endOfLine, params, paramTypes, ...options }) { | ||
const value = path.node; | ||
let formatted = typeof value === 'string' | ||
? format(value, { | ||
let formatted; | ||
if (typeof value === 'string') { | ||
const sqlFormatterOptions = { | ||
...options, | ||
@@ -42,4 +44,14 @@ params: params == null | ||
: JSOX.parse(paramTypes), | ||
}) | ||
: parser.sqlify(value, { type, database }); | ||
}; | ||
formatted = | ||
dialect == null | ||
? format(value, sqlFormatterOptions) | ||
: formatDialect(value, { | ||
...sqlFormatterOptions, | ||
dialect: JSOX.parse(dialect), | ||
}); | ||
} | ||
else { | ||
formatted = parser.sqlify(value, { type, database }); | ||
} | ||
const ending = ENDINGS[endOfLine]; | ||
@@ -72,2 +84,7 @@ formatted = formatted.replaceAll(/\r\n?|\n/g, ending); | ||
}, | ||
dialect: { | ||
category: 'Config', | ||
type: 'string', | ||
description: 'SQL dialect for `sql-formatter` formatDialect()', | ||
}, | ||
language: { | ||
@@ -77,3 +94,3 @@ category: 'Config', | ||
default: 'sql', | ||
description: 'SQL Formatter dialect for `sql-formatter`', | ||
description: 'SQL dialect for `sql-formatter` format()', | ||
choices: [ | ||
@@ -158,18 +175,58 @@ { | ||
default: 'preserve', | ||
description: 'Converts reserved keywords and builtin function names to upper or lowercase for `sql-formatter`', | ||
description: 'Converts reserved keywords to upper- or lowercase for `sql-formatter`', | ||
choices: [ | ||
{ | ||
value: 'preserve', | ||
description: 'preserves the original case', | ||
description: 'preserves the original case of reserved keywords', | ||
}, | ||
{ | ||
value: 'upper', | ||
description: 'converts to uppercase', | ||
description: 'converts reserved keywords to uppercase', | ||
}, | ||
{ | ||
value: 'lower', | ||
description: 'converts to lowercase', | ||
description: 'converts reserved keywords to lowercase', | ||
}, | ||
], | ||
}, | ||
dataTypeCase: { | ||
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: { | ||
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: { | ||
@@ -183,11 +240,11 @@ category: 'Output', | ||
value: 'preserve', | ||
description: 'preserves the original case', | ||
description: 'preserves the original case of identifiers', | ||
}, | ||
{ | ||
value: 'upper', | ||
description: 'converts to uppercase', | ||
description: 'converts identifiers to uppercase', | ||
}, | ||
{ | ||
value: 'lower', | ||
description: 'converts to lowercase', | ||
description: 'converts identifiers to lowercase', | ||
}, | ||
@@ -194,0 +251,0 @@ ], |
{ | ||
"name": "prettier-plugin-sql", | ||
"version": "0.17.1", | ||
"version": "0.18.0", | ||
"type": "module", | ||
@@ -49,4 +49,4 @@ "description": "An opinionated sql formatter plugin for Prettier", | ||
"jsox": "^1.2.118", | ||
"node-sql-parser": "^4.11.0", | ||
"sql-formatter": "^14.0.0", | ||
"node-sql-parser": "^4.12.0", | ||
"sql-formatter": "^15.0.2", | ||
"tslib": "^2.6.2" | ||
@@ -53,0 +53,0 @@ }, |
@@ -50,2 +50,59 @@ # prettier-plugin-sql ![npm bundle size](https://img.shields.io/bundlephobia/min/prettier-plugin-sql) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/prettier-plugin-sql) | ||
### SQL-in-JS with `prettier-plugin-embed` | ||
Format SQL-in-JS tagged template literals by installing [`prettier-plugin-embed`](https://github.com/Sec-ant/prettier-plugin-embed) and configuring as follows: | ||
`prettier.config.mjs` | ||
```js | ||
/** @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 = { | ||
language: 'postgresql', | ||
keywordCase: 'upper', | ||
} | ||
const config = { | ||
...prettierConfig, | ||
...prettierPluginEmbedConfig, | ||
...prettierPluginSqlConfig, | ||
} | ||
export default config | ||
``` | ||
Before formatting: | ||
```ts | ||
const animals = await sql` | ||
sELect first_name, species froM | ||
animals | ||
WhERE | ||
id = ${id} | ||
` | ||
``` | ||
After formatting: | ||
```ts | ||
const animals = await sql` | ||
SELECT | ||
first_name, | ||
species | ||
FROM | ||
animals | ||
WHERE | ||
id = ${id} | ||
` | ||
``` | ||
## Parser Options | ||
@@ -78,3 +135,6 @@ | ||
// default `sql` | ||
dialect: string // `JSOX` **stringified**, please refer https://github.com/sql-formatter-org/sql-formatter/blob/master/docs/dialect.md for more details | ||
keywordCase: 'preserve' | 'upper' | 'lower' // default `preserve` | ||
dataTypeCase: 'preserve' | 'upper' | 'lower' // default `preserve` | ||
functionCase: 'preserve' | 'upper' | 'lower' // default `preserve` | ||
identifierCase: 'preserve' | 'upper' | 'lower' // default `preserve`, experimental | ||
@@ -81,0 +141,0 @@ indentStyle: 'standard' | 'tabularLeft' | 'tabularRight' // default `standard` |
declare module 'jsox' { | ||
export const JSOX: typeof JSON | ||
const JSOX: typeof JSON & { JSOX?: typeof JSON } | ||
export = JSOX | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52687
1135
194
+ Addedsql-formatter@15.4.8(transitive)
- Removedsql-formatter@14.0.0(transitive)
Updatednode-sql-parser@^4.12.0
Updatedsql-formatter@^15.0.2