From 6754c3db50a46f1272d51cf8559cce4c62cdd4ef Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 4 Jul 2024 22:49:52 +0800 Subject: [PATCH] fix(sql): printer to handle eol correctly (#374) Co-authored-by: Suh Junmin --- .changeset/plenty-dancers-yawn.md | 5 ++ .gitattributes | 3 + packages/sql/src/index.ts | 24 +------- .../__snapshots__/fixtures-eol.spec.ts.snap | 9 +++ packages/sql/test/fixtures-eol.spec.ts | 55 +++++++++++++++++++ packages/sql/test/fixtures-eol/556.sql | 4 ++ packages/sql/test/fixtures-eol/557.sql | 4 ++ packages/sql/test/fixtures-eol/558.sql | 4 ++ packages/sql/test/fixtures-eol/559.sql | 4 ++ 9 files changed, 90 insertions(+), 22 deletions(-) create mode 100644 .changeset/plenty-dancers-yawn.md create mode 100644 packages/sql/test/__snapshots__/fixtures-eol.spec.ts.snap create mode 100644 packages/sql/test/fixtures-eol.spec.ts create mode 100644 packages/sql/test/fixtures-eol/556.sql create mode 100644 packages/sql/test/fixtures-eol/557.sql create mode 100644 packages/sql/test/fixtures-eol/558.sql create mode 100644 packages/sql/test/fixtures-eol/559.sql diff --git a/.changeset/plenty-dancers-yawn.md b/.changeset/plenty-dancers-yawn.md new file mode 100644 index 00000000..03e39771 --- /dev/null +++ b/.changeset/plenty-dancers-yawn.md @@ -0,0 +1,5 @@ +--- +"prettier-plugin-sql": patch +--- + +fix(sql): printer to handle eol correctly diff --git a/.gitattributes b/.gitattributes index a8357d8b..9a9d131f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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 diff --git a/packages/sql/src/index.ts b/packages/sql/src/index.ts index fe7a9acf..876950df 100644 --- a/packages/sql/src/index.ts +++ b/packages/sql/src/index.ts @@ -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 }) @@ -61,15 +55,7 @@ const SqlPlugin: Plugin = { sql: { print( path, - { - type, - database, - dialect, - endOfLine, - params, - paramTypes, - ...options - }: SqlOptions, + { type, database, dialect, params, paramTypes, ...options }: SqlOptions, ) { const value = path.node @@ -101,13 +87,7 @@ const SqlPlugin: Plugin = { 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' }, }, }, diff --git a/packages/sql/test/__snapshots__/fixtures-eol.spec.ts.snap b/packages/sql/test/__snapshots__/fixtures-eol.spec.ts.snap new file mode 100644 index 00000000..ffbccf49 --- /dev/null +++ b/packages/sql/test/__snapshots__/fixtures-eol.spec.ts.snap @@ -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""`; diff --git a/packages/sql/test/fixtures-eol.spec.ts b/packages/sql/test/fixtures-eol.spec.ts new file mode 100644 index 00000000..50485cbd --- /dev/null +++ b/packages/sql/test/fixtures-eol.spec.ts @@ -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 = { + 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) + } + } + }) +}) diff --git a/packages/sql/test/fixtures-eol/556.sql b/packages/sql/test/fixtures-eol/556.sql new file mode 100644 index 00000000..daba8962 --- /dev/null +++ b/packages/sql/test/fixtures-eol/556.sql @@ -0,0 +1,4 @@ + +CREATE TABLE "test" ( + "id" uuid NOT NULL +) WITH (oids = false); diff --git a/packages/sql/test/fixtures-eol/557.sql b/packages/sql/test/fixtures-eol/557.sql new file mode 100644 index 00000000..daba8962 --- /dev/null +++ b/packages/sql/test/fixtures-eol/557.sql @@ -0,0 +1,4 @@ + +CREATE TABLE "test" ( + "id" uuid NOT NULL +) WITH (oids = false); diff --git a/packages/sql/test/fixtures-eol/558.sql b/packages/sql/test/fixtures-eol/558.sql new file mode 100644 index 00000000..daba8962 --- /dev/null +++ b/packages/sql/test/fixtures-eol/558.sql @@ -0,0 +1,4 @@ + +CREATE TABLE "test" ( + "id" uuid NOT NULL +) WITH (oids = false); diff --git a/packages/sql/test/fixtures-eol/559.sql b/packages/sql/test/fixtures-eol/559.sql new file mode 100644 index 00000000..daba8962 --- /dev/null +++ b/packages/sql/test/fixtures-eol/559.sql @@ -0,0 +1,4 @@ + +CREATE TABLE "test" ( + "id" uuid NOT NULL +) WITH (oids = false);