Skip to content

Commit

Permalink
feat: add sql-formatter dialect option (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
karlhorky committed Dec 17, 2023
1 parent 909e30a commit 372ea30
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/objective-rock-sedge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-sql': minor
---

feat: add sql-formatter `dialect` option
1 change: 1 addition & 0 deletions packages/sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ interface SqlOptions {
| 'tsql'
| 'trino'
// 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`
Expand Down
67 changes: 46 additions & 21 deletions packages/sql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import * as _JSOX from 'jsox'
import type { AST, Option } from 'node-sql-parser'
import nodeSqlParser from 'node-sql-parser'
import type { Options, ParserOptions, Plugin } from 'prettier'
import { format, type FormatOptionsWithLanguage } from 'sql-formatter'
import {
DialectOptions,
format,
formatDialect,
type FormatOptions,
type FormatOptionsWithLanguage,
} from 'sql-formatter'

import { languages } from './languages.js'

Expand All @@ -24,7 +30,10 @@ const ENDINGS = {
} as const

export type SqlBaseOptions = Option &
Partial<FormatOptionsWithLanguage> & {
Partial<
| (FormatOptions & { dialect: string })
| (FormatOptionsWithLanguage & { dialect?: never })
> & {
formatter?: typeof NODE_SQL_PARSER | typeof SQL_CST | typeof SQL_FORMATTER
params?: string
paramTypes?: string
Expand Down Expand Up @@ -55,6 +64,7 @@ const SqlPlugin: Plugin<AST | string> = {
{
type,
database,
dialect,
endOfLine,
params,
paramTypes,
Expand All @@ -63,24 +73,33 @@ const SqlPlugin: Plugin<AST | string> = {
) {
const value = path.node

let formatted =
typeof value === 'string'
? format(value, {
...options,
params:
params == null
? undefined
: (JSOX.parse(
params,
) as FormatOptionsWithLanguage['params']),
paramTypes:
paramTypes == null
? undefined
: (JSOX.parse(
paramTypes,
) as FormatOptionsWithLanguage['paramTypes']),
})
: parser.sqlify(value, { type, database })
let formatted: string

if (typeof value === 'string') {
const sqlFormatterOptions = {
...options,
params:
params == null
? undefined
: (JSOX.parse(params) as FormatOptionsWithLanguage['params']),
paramTypes:
paramTypes == null
? undefined
: (JSOX.parse(
paramTypes,
) as FormatOptionsWithLanguage['paramTypes']),
}

formatted =
dialect == null
? format(value, sqlFormatterOptions)
: formatDialect(value, {
...sqlFormatterOptions,
dialect: JSOX.parse(dialect) as DialectOptions,
})
} else {
formatted = parser.sqlify(value, { type, database })
}

// It can never be `auto`
// @see https://github.com/prettier/prettier/blob/ab72a2c11c806f3a8a5ef42314e291843e1b3e68/src/common/end-of-line.js#L3-L9
Expand Down Expand Up @@ -114,12 +133,18 @@ const SqlPlugin: Plugin<AST | string> = {
},
],
},
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 Formatter dialect for `sql-formatter`',
description: 'SQL dialect for `sql-formatter` format()',
choices: [
{
value: 'sql',
Expand Down
6 changes: 6 additions & 0 deletions packages/sql/test/__snapshots__/fixtures.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ END;
"
`;

exports[`parser and printer > should format all fixtures > 334.sql 1`] = `
"SELECT
1::int;
"
`;

exports[`parser and printer > should format all fixtures > basic.sql 1`] = `
"-- this is a comment
SELECT
Expand Down
5 changes: 4 additions & 1 deletion packages/sql/test/fixtures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { format } from 'prettier'
import type { ParamTypes } from 'sql-formatter'
import { type ParamTypes, postgresql } from 'sql-formatter'

import SqlPlugin, { type SqlFormatOptions } from 'prettier-plugin-sql'

Expand Down Expand Up @@ -32,6 +32,9 @@ const PARSER_OPTIONS: Record<string, SqlFormatOptions> = {
custom: [{ regex: String.raw`:\w+(\$\w+)?` }],
} satisfies ParamTypes),
},
334: {
dialect: JSON.stringify(postgresql),
},
}

const _dirname =
Expand Down
1 change: 1 addition & 0 deletions packages/sql/test/fixtures/334.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT 1::int;

0 comments on commit 372ea30

Please sign in to comment.