Skip to content

Commit e232271

Browse files
committed
feat: adopt sql-formatter
1 parent 6113924 commit e232271

29 files changed

+13693
-12833
lines changed

.README/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# eslint-plugin-sql
22

3-
[![NPM version](http://img.shields.io/npm/v/eslint-plugin-sql.svg?style=flat-square)](https://www.npmjs.org/package/eslint-plugin-sql)
43
[![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
54
[![Twitter Follow](https://img.shields.io/twitter/follow/kuizinas.svg?style=social&label=Follow)](https://twitter.com/kuizinas)
65

.README/rules/format.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@ _The `--fix` option on the command line automatically fixes problems reported by
44

55
Matches queries in template literals. Warns when query formatting does not match the configured format (see Options).
66

7-
This rule is used to format the queries using [pg-formatter](https://github.com/gajus/pg-formatter).
7+
This rule is used to format the queries using [sql-formatter](https://github.com/sql-formatter-org/sql-formatter).
88

99
#### Options
1010

1111
The first option is an object with the following configuration.
1212

1313
|configuration|format|default|description|
1414
|---|---|---|---|
15-
|`ignoreBaseIndent`|boolean|`false`|Does not leave base indent before linting.|
1615
|`ignoreExpressions`|boolean|`false`|Does not format template literals that contain expressions.|
1716
|`ignoreInline`|boolean|`true`|Does not format queries that are written on a single line.|
1817
|`ignoreStartWithNewLine`|boolean|`true`|Does not remove `\n` at the beginning of queries.|
1918
|`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|
19+
|`retainBaseIndent`|boolean|`true`|Uses the first line of the query as the base indent.|
2020
|`sqlTag`|string|`sql`|Template tag name for SQL.|
2121

22-
The second option is an object with the [`pg-formatter` configuration](https://github.com/gajus/pg-formatter#configuration).
22+
The second option is an object with the [`sql-formatter` configuration](https://github.com/sql-formatter-org/sql-formatter?tab=readme-ov-file#configuration-options).
23+
24+
|configuration|default|description|
25+
|---|---|---|
26+
|`useTabs`|boolean|`false`|Use tabs for indentation.|
27+
|`tabSize`|2|Number of spaces per indentation.|
28+
|`language`|`sql`|Language of the query.|
2329

2430
<!-- assertions format -->

.eslintrc.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

.github/workflows/feature.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
jobs:
22
test:
3-
runs-on: ubuntu-latest
43
environment: release
54
name: Test
5+
runs-on: ubuntu-latest
66
steps:
77
- name: setup repository
88
uses: actions/checkout@v3
@@ -11,7 +11,7 @@ jobs:
1111
- name: setup node.js
1212
uses: actions/setup-node@v3
1313
with:
14-
node-version: "18"
14+
node-version: '23'
1515
- run: npm install
1616
- run: npm run lint:eslint
1717
- run: npm run lint:tsc

.github/workflows/main.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
jobs:
22
test:
3-
runs-on: ubuntu-latest
43
environment: release
54
name: Test
5+
runs-on: ubuntu-latest
66
steps:
77
- name: setup repository
88
uses: actions/checkout@v3
@@ -11,17 +11,17 @@ jobs:
1111
- name: setup node.js
1212
uses: actions/setup-node@v3
1313
with:
14-
node-version: "18"
14+
node-version: '23'
1515
- run: npm install
1616
- run: npm run lint:eslint
1717
- run: npm run lint:tsc
1818
- run: npm run lint:cspell
1919
- run: npm run test:mocha
2020
- run: npm run build
21-
- run: npx semantic-release
22-
env:
21+
- env:
2322
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2423
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
24+
run: npx semantic-release
2525
name: Test, build and release
2626
on:
2727
push:

.husky/pre-commit

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
#!/usr/bin/env sh
2-
. "$(dirname -- "$0")/_/husky.sh"
3-
4-
npm run lint:tsc
5-
npm run lint:eslint
6-
npm run lint:cspell
7-
npm run test:mocha
1+
npm test

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2023, Gajus Kuizinas (https://gajus.com/)
1+
Copyright (c) 2024, Gajus Kuizinas (https://gajus.com/)
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

README.md

Lines changed: 201 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<a name="eslint-plugin-sql"></a>
33
# eslint-plugin-sql
44

5-
[![NPM version](http://img.shields.io/npm/v/eslint-plugin-sql.svg?style=flat-square)](https://www.npmjs.org/package/eslint-plugin-sql)
65
[![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
76
[![Twitter Follow](https://img.shields.io/twitter/follow/kuizinas.svg?style=social&label=Follow)](https://twitter.com/kuizinas)
87

@@ -96,7 +95,7 @@ _The `--fix` option on the command line automatically fixes problems reported by
9695

9796
Matches queries in template literals. Warns when query formatting does not match the configured format (see Options).
9897

99-
This rule is used to format the queries using [pg-formatter](https://github.com/gajus/pg-formatter).
98+
This rule is used to format the queries using [sql-formatter](https://github.com/sql-formatter-org/sql-formatter).
10099

101100
<a name="user-content-eslint-plugin-sql-rules-format-options"></a>
102101
<a name="eslint-plugin-sql-rules-format-options"></a>
@@ -106,14 +105,176 @@ The first option is an object with the following configuration.
106105

107106
|configuration|format|default|description|
108107
|---|---|---|---|
109-
|`ignoreBaseIndent`|boolean|`false`|Does not leave base indent before linting.|
110108
|`ignoreExpressions`|boolean|`false`|Does not format template literals that contain expressions.|
111109
|`ignoreInline`|boolean|`true`|Does not format queries that are written on a single line.|
112110
|`ignoreStartWithNewLine`|boolean|`true`|Does not remove `\n` at the beginning of queries.|
113111
|`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|
112+
|`retainBaseIndent`|boolean|`true`|Uses the first line of the query as the base indent.|
114113
|`sqlTag`|string|`sql`|Template tag name for SQL.|
115114

116-
The second option is an object with the [`pg-formatter` configuration](https://github.com/gajus/pg-formatter#configuration).
115+
The second option is an object with the [`sql-formatter` configuration](https://github.com/sql-formatter-org/sql-formatter?tab=readme-ov-file#configuration-options).
116+
117+
|configuration|default|description|
118+
|---|---|---|
119+
|`useTabs`|boolean|`false`|Use tabs for indentation.|
120+
|`tabSize`|2|Number of spaces per indentation.|
121+
|`language`|`sql`|Language of the query.|
122+
123+
The following patterns are considered problems:
124+
125+
```js
126+
sql`
127+
SELECT
128+
1
129+
`
130+
// Options: [{},{"tabWidth":4}]
131+
// Message: undefined
132+
// Fixed code:
133+
// sql`
134+
// SELECT
135+
// 1
136+
// `
137+
138+
sql.type({ id: z.number() })`
139+
SELECT
140+
1
141+
`
142+
// Options: [{},{"tabWidth":4}]
143+
// Message: undefined
144+
// Fixed code:
145+
// sql.type({ id: z.number() })`
146+
// SELECT
147+
// 1
148+
// `
149+
150+
sql.typeAlias('void')`
151+
SELECT
152+
1
153+
`
154+
// Options: [{},{"tabWidth":4}]
155+
// Message: undefined
156+
// Fixed code:
157+
// sql.typeAlias('void')`
158+
// SELECT
159+
// 1
160+
// `
161+
162+
`SELECT 1`
163+
// Options: [{"ignoreInline":false,"ignoreTagless":false},{}]
164+
// Message: undefined
165+
// Fixed code:
166+
// `
167+
// SELECT
168+
// 1
169+
// `
170+
171+
`SELECT 2`
172+
// Options: [{"ignoreInline":false,"ignoreTagless":false},{"tabWidth":2}]
173+
// Message: undefined
174+
// Fixed code:
175+
// `
176+
// SELECT
177+
// 2
178+
// `
179+
180+
sql.unsafe`SELECT 3`
181+
// Options: [{"ignoreInline":false},{}]
182+
// Message: undefined
183+
// Fixed code:
184+
// sql.unsafe`
185+
// SELECT
186+
// 3
187+
// `
188+
189+
sql.type()`SELECT 3`
190+
// Options: [{"ignoreInline":false},{}]
191+
// Message: undefined
192+
// Fixed code:
193+
// sql.type()`
194+
// SELECT
195+
// 3
196+
// `
197+
198+
`SELECT ${'foo'} FROM ${'bar'}`
199+
// Options: [{"ignoreInline":false,"ignoreTagless":false},{}]
200+
// Message: undefined
201+
// Fixed code:
202+
// `
203+
// SELECT
204+
// ${'foo'}
205+
// FROM
206+
// ${'bar'}
207+
// `
208+
209+
const code = sql`
210+
SELECT
211+
foo
212+
FROM
213+
bar
214+
`
215+
// Options: [{},{}]
216+
// Message: undefined
217+
// Fixed code:
218+
// const code = sql`
219+
// SELECT
220+
// foo
221+
// FROM
222+
// bar
223+
// `
224+
225+
SQL`SELECT 1`
226+
// Options: [{"ignoreInline":false,"sqlTag":"SQL"},{}]
227+
// Message: undefined
228+
// Fixed code:
229+
// SQL`
230+
// SELECT
231+
// 1
232+
// `
233+
```
234+
235+
The following patterns are not considered problems:
236+
237+
```js
238+
`
239+
# A
240+
## B
241+
### C
242+
`
243+
244+
sql`SELECT 1`
245+
// Options: [{"ignoreInline":true},{}]
246+
247+
`SELECT 2`
248+
// Options: [{"ignoreTagless":true},{}]
249+
250+
const code = sql`
251+
SELECT
252+
${'foo'}
253+
FROM
254+
${'bar'}
255+
`
256+
// Options: [{"ignoreExpressions":true,"ignoreInline":false,"ignoreTagless":false},{}]
257+
258+
const code = sql`
259+
SELECT
260+
${'foo'}
261+
FROM
262+
${'bar'}
263+
`
264+
// Options: [{},{}]
265+
266+
const code = sql`
267+
DROP TABLE foo
268+
`
269+
// Options: [{},{}]
270+
271+
const code = sql`
272+
DROP TABLE foo;
273+
274+
DROP TABLE foo;
275+
`
276+
// Options: [{},{}]
277+
```
117278

118279

119280

@@ -139,5 +300,41 @@ The first option is an object with the following configuration.
139300
|`allowLiteral`|boolean|`false`|Controls whether `sql` tag is required for template literals containing literal queries, i.e. template literals without expressions.|
140301
|`sqlTag`|string|`sql`|Template tag name for SQL.|
141302

303+
The following patterns are considered problems:
304+
305+
```js
306+
`SELECT 1`
307+
// Message: undefined
308+
309+
`SELECT ${'foo'}`
310+
// Message: undefined
311+
312+
foo`SELECT ${'bar'}`
313+
// Message: undefined
314+
315+
`SELECT ?`
316+
// Message: undefined
317+
318+
foo`SELECT ${'bar'}`
319+
// Options: [{"sqlTag":"SQL"}]
320+
// Message: undefined
321+
```
322+
323+
The following patterns are not considered problems:
324+
325+
```js
326+
sql.unsafe`SELECT 3`
327+
328+
`SELECT 1`
329+
// Options: [{"allowLiteral":true}]
330+
331+
sql`SELECT 1`
332+
333+
sql`SELECT ${'foo'}`
334+
335+
SQL`SELECT ${'bar'}`
336+
// Options: [{"sqlTag":"SQL"}]
337+
```
338+
142339

143340

cspell.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ words:
88
- slonik
99
- tagless
1010
- gitdown
11-
- astring
12-
- quasis
11+
- quasis
12+
- singlestoredb
13+
- tidb
14+
- transactsql
15+
- trino

eslint.config.cjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const auto = require('eslint-config-canonical/configurations/auto');
2+
3+
module.exports = [
4+
...auto,
5+
{
6+
rules: {
7+
'no-template-curly-in-string': 0,
8+
},
9+
},
10+
{
11+
ignores: [
12+
'package-lock.json',
13+
'dist',
14+
'node_modules',
15+
'*.log',
16+
'.*',
17+
'!.github',
18+
'!.gitignore',
19+
'!.husky',
20+
'!.releaserc',
21+
],
22+
},
23+
];

0 commit comments

Comments
 (0)