Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 2 additions & 51 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,58 +23,8 @@ npm install eslint-plugin-sql --save-dev

## Configuration

### ESLint 9+ (Flat Config)

1. Import `eslint-plugin-sql`.
2. Use the recommended configuration or customize rules.

```javascript
import sql from 'eslint-plugin-sql';

export default [
// Use the recommended configuration
sql.configs['flat/recommended'],
{
rules: {
'sql/format': ['error', { ignoreInline: false }]
}
}
];
```

### ESLint 8 (.eslintrc)

1. Add `plugins` section and specify `eslint-plugin-sql` as a plugin.
2. Enable rules.

```json
{
"plugins": [
"sql"
],
"rules": {
"sql/format": [
2,
{
"ignoreExpressions": false,
"ignoreInline": true,
"ignoreTagless": true
}
],
"sql/no-unsafe-query": [
2,
{
"allowLiteral": false
}
]
},
"settings": {
"sql": {
"placeholderRule": "\\?"
}
}
}
```
1. Enable rules.

<!-- -->

Expand All @@ -100,6 +50,7 @@ export default [
]
}
}

```

## Settings
Expand Down
2 changes: 1 addition & 1 deletion .README/rules/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The second option is an object with the [`sql-formatter` configuration](https://
|configuration|default|description|
|---|---|---|
|`useTabs`|`false`|Use tabs for indentation.|
|`tabWidth`|2|Number of spaces per indentation.|
|`tabSize`|2|Number of spaces per indentation.|
|`language`|`sql`|Language of the query.|
|`keywordCase`|`preserve`|Determines the case of keywords (`preserve`, `upper`, `lower`).|
|`dataTypeCase`|`preserve`|Determines the case of data types (`preserve`, `upper`, `lower`).|
Expand Down
104 changes: 26 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ SQL linting rules for ESLint.
* [eslint-plugin-sql](#user-content-eslint-plugin-sql)
* [Installation](#user-content-eslint-plugin-sql-installation)
* [Configuration](#user-content-eslint-plugin-sql-configuration)
* [ESLint 9+ (Flat Config)](#user-content-eslint-plugin-sql-configuration-eslint-9-flat-config)
* [ESLint 8 (.eslintrc)](#user-content-eslint-plugin-sql-configuration-eslint-8-eslintrc)
* [Settings](#user-content-eslint-plugin-sql-settings)
* [`placeholderRule`](#user-content-eslint-plugin-sql-settings-placeholderrule)
* [Rules](#user-content-eslint-plugin-sql-rules)
Expand All @@ -39,33 +37,10 @@ npm install eslint-plugin-sql --save-dev
<a name="eslint-plugin-sql-configuration"></a>
## Configuration

<a name="user-content-eslint-plugin-sql-configuration-eslint-9-flat-config"></a>
<a name="eslint-plugin-sql-configuration-eslint-9-flat-config"></a>
### ESLint 9+ (Flat Config)

1. Import `eslint-plugin-sql`.
2. Use the recommended configuration or customize rules.

```javascript
import sql from 'eslint-plugin-sql';

export default [
// Use the recommended configuration
sql.configs['flat/recommended'],
{
rules: {
'sql/format': ['error', { ignoreInline: false }]
}
}
];
```

<a name="user-content-eslint-plugin-sql-configuration-eslint-8-eslintrc"></a>
<a name="eslint-plugin-sql-configuration-eslint-8-eslintrc"></a>
### ESLint 8 (.eslintrc)

1. Add `plugins` section and specify `eslint-plugin-sql` as a plugin.
2. Enable rules.
1. Enable rules.

<!-- -->

```json
{
Expand Down Expand Up @@ -94,32 +69,7 @@ export default [
}
}
}
```

<!-- -->

```json
{
"plugins": [
"sql"
],
"rules": {
"sql/format": [
2,
{
"ignoreExpressions": false,
"ignoreInline": true,
"ignoreTagless": true
}
],
"sql/no-unsafe-query": [
2,
{
"allowLiteral": false
}
]
}
}
```

<a name="user-content-eslint-plugin-sql-settings"></a>
Expand Down Expand Up @@ -192,7 +142,7 @@ sql.fragment`
m1.ID = ${message.id}
`
// Options: [{},{"identifierCase":"lower"}]
// Message: Format the query
// Message: undefined
// Fixed code:
// sql.fragment`
// SELECT
Expand All @@ -207,7 +157,7 @@ sql.fragment`
SELECT id::NUMERIC
`
// Options: [{},{"dataTypeCase":"lower","language":"postgresql"}]
// Message: Format the query
// Message: undefined
// Fixed code:
// sql.fragment`
// SELECT
Expand All @@ -223,7 +173,7 @@ sql.fragment`
id = ${message.id}
`
// Options: [{},{"keywordCase":"lower"}]
// Message: Format the query
// Message: undefined
// Fixed code:
// sql.fragment`
// select
Expand All @@ -243,7 +193,7 @@ sql.fragment`
id = ${message.id}
`
// Options: [{},{"keywordCase":"upper"}]
// Message: Format the query
// Message: undefined
// Fixed code:
// sql.fragment`
// SELECT
Expand All @@ -262,7 +212,7 @@ SET
WHERE id = ${message.id}
`;
// Options: [{},{"tabWidth":4}]
// Message: Format the query
// Message: undefined
// Fixed code:
// sql.fragment`
// ${null}
Expand All @@ -280,7 +230,7 @@ await pool.query(sql.typeAlias('void')`
WHERE id = ${message.id}
`);
// Options: [{},{"tabWidth":4}]
// Message: Format the query
// Message: undefined
// Fixed code:
// await pool.query(sql.typeAlias('void')`
// UPDATE message
Expand All @@ -295,7 +245,7 @@ sql`
1
`
// Options: [{},{"tabWidth":4}]
// Message: Format the query
// Message: undefined
// Fixed code:
// sql`
// SELECT
Expand All @@ -307,7 +257,7 @@ sql.type({ id: z.number() })`
1
`
// Options: [{},{"tabWidth":4}]
// Message: Format the query
// Message: undefined
// Fixed code:
// sql.type({ id: z.number() })`
// SELECT
Expand All @@ -319,7 +269,7 @@ sql.typeAlias('void')`
1
`
// Options: [{},{"tabWidth":4}]
// Message: Format the query
// Message: undefined
// Fixed code:
// sql.typeAlias('void')`
// SELECT
Expand All @@ -328,7 +278,7 @@ sql.typeAlias('void')`

`SELECT 1`
// Options: [{"ignoreInline":false,"ignoreTagless":false},{}]
// Message: Format the query
// Message: undefined
// Fixed code:
// `
// SELECT
Expand All @@ -337,7 +287,7 @@ sql.typeAlias('void')`

`SELECT 2`
// Options: [{"ignoreInline":false,"ignoreTagless":false},{"tabWidth":2}]
// Message: Format the query
// Message: undefined
// Fixed code:
// `
// SELECT
Expand All @@ -346,7 +296,7 @@ sql.typeAlias('void')`

sql.unsafe`SELECT 3`
// Options: [{"ignoreInline":false},{}]
// Message: Format the query
// Message: undefined
// Fixed code:
// sql.unsafe`
// SELECT
Expand All @@ -355,7 +305,7 @@ sql.unsafe`SELECT 3`

sql.type()`SELECT 3`
// Options: [{"ignoreInline":false},{}]
// Message: Format the query
// Message: undefined
// Fixed code:
// sql.type()`
// SELECT
Expand All @@ -364,7 +314,7 @@ sql.type()`SELECT 3`

`SELECT ${'foo'} FROM ${'bar'}`
// Options: [{"ignoreInline":false,"ignoreTagless":false},{}]
// Message: Format the query
// Message: undefined
// Fixed code:
// `
// SELECT
Expand All @@ -380,7 +330,7 @@ const code = sql`
bar
`
// Options: [{},{}]
// Message: Format the query
// Message: undefined
// Fixed code:
// const code = sql`
// SELECT
Expand All @@ -391,7 +341,7 @@ const code = sql`

SQL`SELECT 1`
// Options: [{"ignoreInline":false,"sqlTag":"SQL"},{}]
// Message: Format the query
// Message: undefined
// Fixed code:
// SQL`
// SELECT
Expand Down Expand Up @@ -443,7 +393,7 @@ const code = sql`
// Options: [{},{}]
```

<!-- end-assertions -->


<a name="user-content-eslint-plugin-sql-rules-no-unsafe-query"></a>
<a name="eslint-plugin-sql-rules-no-unsafe-query"></a>
Expand All @@ -467,26 +417,24 @@ The first option is an object with the following configuration.
|`allowLiteral`|boolean|`false`|Controls whether `sql` tag is required for template literals containing literal queries, i.e. template literals without expressions.|
|`sqlTag`|string|`sql`|Template tag name for SQL.|

<!-- assertions noUnsafeQuery -->

The following patterns are considered problems:

```js
`SELECT 1`
// Message: Use "{{sqlTag}}" tag
// Message: undefined

`SELECT ${'foo'}`
// Message: Use "{{sqlTag}}" tag
// Message: undefined

foo`SELECT ${'bar'}`
// Message: Use "{{sqlTag}}" tag
// Message: undefined

`SELECT ?`
// Message: Use "{{sqlTag}}" tag
// Message: undefined

foo`SELECT ${'bar'}`
// Options: [{"sqlTag":"SQL"}]
// Message: Use "{{sqlTag}}" tag
// Message: undefined
```

The following patterns are not considered problems:
Expand All @@ -505,5 +453,5 @@ SQL`SELECT ${'bar'}`
// Options: [{"sqlTag":"SQL"}]
```

<!-- end-assertions -->


14 changes: 0 additions & 14 deletions eslint.config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import auto from 'eslint-config-canonical/auto';
import tseslint from 'typescript-eslint';
import globals from 'globals';

export default tseslint.config(
auto,
{
rules: {
'no-template-curly-in-string': 0,
'import/no-useless-path-segments': [
'error',
{
noUselessIndex: false,
},
],
},
},
{
files: ['**/index.[jt]s'],
rules: {
'canonical/filename-match-exported': 0,
},
},
{
Expand All @@ -36,7 +23,6 @@ export default tseslint.config(
},
{
languageOptions: {
globals: globals.mocha,
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
Expand Down
Loading
Loading