Skip to content

Commit

Permalink
fix(sql): printer to handle eol correctly (#374)
Browse files Browse the repository at this point in the history
Co-authored-by: Suh Junmin <[email protected]>
  • Loading branch information
JounQin and LazyRichard authored Jul 4, 2024
1 parent 947b0af commit 6754c3d
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-dancers-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prettier-plugin-sql": patch
---

fix(sql): printer to handle eol correctly
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
* text=auto eol=lf
pnpm-lock.yaml -diff

packages/sql/test/fixtures-eol/556.sql text=auto eol=crlf
packages/sql/test/fixtures-eol/559.sql text=auto eol=crlf
24 changes: 2 additions & 22 deletions packages/sql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ const SQL_FORMATTER = 'sql-formatter'
const NODE_SQL_PARSER = 'node-sql-parser'
const SQL_CST = 'sql-cst'

const ENDINGS = {
lf: '\n',
cr: '\r',
crlf: '\r\n',
} as const

export type SqlBaseOptions = Option &
Partial<
| (FormatOptions & { dialect: string })
Expand Down Expand Up @@ -61,15 +55,7 @@ const SqlPlugin: Plugin<AST | string> = {
sql: {
print(
path,
{
type,
database,
dialect,
endOfLine,
params,
paramTypes,
...options
}: SqlOptions,
{ type, database, dialect, params, paramTypes, ...options }: SqlOptions,
) {
const value = path.node

Expand Down Expand Up @@ -101,13 +87,7 @@ const SqlPlugin: Plugin<AST | string> = {
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
const ending = ENDINGS[endOfLine as keyof typeof ENDINGS]

formatted = formatted.replaceAll(/\r\n?|\n/g, ending)

return formatted.endsWith(ending) ? formatted : formatted + ending
return formatted + '\n'
},
},
},
Expand Down
9 changes: 9 additions & 0 deletions packages/sql/test/__snapshots__/fixtures-eol.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`parser and printer > printer should handle eol correctly > 556.sql 1`] = `""CREATE TABLE \\"test\\" (\\"id\\" uuid NOT NULL)\\r\\nWITH\\r\\n (oids = false);\\r\\n""`;

exports[`parser and printer > printer should handle eol correctly > 557.sql 1`] = `""CREATE TABLE \\"test\\" (\\"id\\" uuid NOT NULL)\\rWITH\\r (oids = false);\\r""`;

exports[`parser and printer > printer should handle eol correctly > 558.sql 1`] = `""CREATE TABLE \\"test\\" (\\"id\\" uuid NOT NULL)\\nWITH\\n (oids = false);\\n""`;

exports[`parser and printer > printer should handle eol correctly > 559.sql 1`] = `""CREATE TABLE \\"test\\" (\\"id\\" uuid NOT NULL)\\r\\nWITH\\r\\n (oids = false);\\r\\n""`;
55 changes: 55 additions & 0 deletions packages/sql/test/fixtures-eol.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { format } from 'prettier'

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

const PARSER_OPTIONS: Record<string, SqlFormatOptions> = {
556: {
language: 'mysql',
endOfLine: 'crlf',
},
557: {
language: 'mysql',
endOfLine: 'cr',
},
558: {
language: 'mysql',
endOfLine: 'lf',
},
559: {
language: 'mysql',
endOfLine: 'auto',
},
}

const _dirname =
typeof __dirname === 'undefined'
? path.dirname(fileURLToPath(import.meta.url))
: __dirname

describe('parser and printer', () => {
it('printer should handle eol correctly', async () => {
const fixtures = path.resolve(_dirname, 'fixtures-eol')
for (const filepath of fs.readdirSync(fixtures)) {
const input = fs.readFileSync(path.resolve(fixtures, filepath)).toString()

const caseName = filepath.slice(0, filepath.lastIndexOf('.'))

try {
const output = await format(input, {
filepath,
parser: 'sql',
plugins: [SqlPlugin],
...PARSER_OPTIONS[caseName],
})

expect(JSON.stringify(output)).toMatchSnapshot(filepath)
} catch (error) {
expect(error).toMatchSnapshot(filepath)
}
}
})
})
4 changes: 4 additions & 0 deletions packages/sql/test/fixtures-eol/556.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

CREATE TABLE "test" (
"id" uuid NOT NULL
) WITH (oids = false);
4 changes: 4 additions & 0 deletions packages/sql/test/fixtures-eol/557.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

CREATE TABLE "test" (
"id" uuid NOT NULL
) WITH (oids = false);
4 changes: 4 additions & 0 deletions packages/sql/test/fixtures-eol/558.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

CREATE TABLE "test" (
"id" uuid NOT NULL
) WITH (oids = false);
4 changes: 4 additions & 0 deletions packages/sql/test/fixtures-eol/559.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

CREATE TABLE "test" (
"id" uuid NOT NULL
) WITH (oids = false);

0 comments on commit 6754c3d

Please sign in to comment.