-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sql): printer to handle eol correctly (#374)
Co-authored-by: Suh Junmin <[email protected]>
- Loading branch information
1 parent
947b0af
commit 6754c3d
Showing
9 changed files
with
90 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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""`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
CREATE TABLE "test" ( | ||
"id" uuid NOT NULL | ||
) WITH (oids = false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
CREATE TABLE "test" ( | ||
"id" uuid NOT NULL | ||
) WITH (oids = false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
CREATE TABLE "test" ( | ||
"id" uuid NOT NULL | ||
) WITH (oids = false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
CREATE TABLE "test" ( | ||
"id" uuid NOT NULL | ||
) WITH (oids = false); |