Skip to content

Commit

Permalink
feat(sql)!: respect endOfLine option from prettier core (#207)
Browse files Browse the repository at this point in the history
Co-authored-by: JounQin <admin@1stg.me>
  • Loading branch information
frozenbonito and JounQin committed Jul 23, 2022
1 parent 2c1f8c8 commit 6ca7374
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-actors-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-sql': minor
---

feat!: respect `endOfLine` option from `prettier` core
24 changes: 20 additions & 4 deletions packages/sql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const parser = new nodeSqlParser.Parser()
const SQL_FORMATTER = 'sql-formatter'
const NODE_SQL_PARSER = 'node-sql-parser'

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

export type SqlBaseOptions = Option &
Partial<FormatFnOptions> & {
formatter?: typeof NODE_SQL_PARSER | typeof SQL_FORMATTER
Expand All @@ -34,11 +40,21 @@ const SqlPlugin: Plugin<AST | string> = {
},
printers: {
sql: {
print(path, { type, database, ...options }: SqlOptions) {
print(path, { type, database, endOfLine, ...options }: SqlOptions) {
const value = path.getValue()
return typeof value === 'string'
? format(value, options)
: parser.sqlify(value, { type, database })

let formatted =
typeof value === 'string'
? format(value, options)
: 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.replace(/\r\n?|\n/g, ending)

return formatted.endsWith(ending) ? formatted : formatted + ending
},
},
},
Expand Down
6 changes: 4 additions & 2 deletions packages/sql/test/__snapshots__/fixtures.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ exports[`parser and printer should format all fixtures: 144.sql 1`] = `
"CREATE TABLE
\\"test\\" (\\"id\\" uuid NOT NULL)
WITH
(oids = false);"
(oids = false);
"
`;

exports[`parser and printer should format all fixtures: basic.sql 1`] = `
Expand All @@ -26,5 +27,6 @@ WHERE
name
FROM
b
)"
)
"
`;
15 changes: 12 additions & 3 deletions packages/sql/test/__snapshots__/sql.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`node-sql-parser 1`] = `"SELECT * FROM \`t\`"`;
exports[`node-sql-parser 1`] = `
"SELECT * FROM \`t\`
"
`;

exports[`node-sql-parser 2`] = `"UPDATE \`a\` SET \`id\` = 1 WHERE \`name\` IN (SELECT \`name\` FROM \`b\`)"`;
exports[`node-sql-parser 2`] = `
"UPDATE \`a\` SET \`id\` = 1 WHERE \`name\` IN (SELECT \`name\` FROM \`b\`)
"
`;

exports[`node-sql-parser 3`] = `"SELECT * FROM \`t\` ; UPDATE \`a\` SET \`id\` = 1 WHERE \`name\` IN (SELECT \`name\` FROM \`b\`)"`;
exports[`node-sql-parser 3`] = `
"SELECT * FROM \`t\` ; UPDATE \`a\` SET \`id\` = 1 WHERE \`name\` IN (SELECT \`name\` FROM \`b\`)
"
`;

0 comments on commit 6ca7374

Please sign in to comment.