Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sql-formatter dialect option #334

Merged
merged 11 commits into from
Dec 17, 2023
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 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 @@
} 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 @@
{
type,
database,
dialect,
endOfLine,
params,
paramTypes,
Expand All @@ -63,24 +73,33 @@
) {
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']),

Check warning on line 84 in packages/sql/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/sql/src/index.ts#L84

Added line #L84 was not covered by tests
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 @@
},
],
},
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;