Skip to content

Commit

Permalink
fix: correct no-unsafe-query
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 17, 2022
1 parent 68eee0d commit d968b18
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 98 deletions.
1 change: 0 additions & 1 deletion .README/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# eslint-plugin-sql

[![Travis build status](http://img.shields.io/travis/gajus/eslint-plugin-sql/master.svg?style=flat-square)](https://travis-ci.com/github/gajus/eslint-plugin-sql)
[![NPM version](http://img.shields.io/npm/v/eslint-plugin-sql.svg?style=flat-square)](https://www.npmjs.org/package/eslint-plugin-sql)
[![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
[![Twitter Follow](https://img.shields.io/twitter/follow/kuizinas.svg?style=social&label=Follow)](https://twitter.com/kuizinas)
Expand Down
109 changes: 18 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<a name="user-content-eslint-plugin-sql"></a>
<a name="eslint-plugin-sql"></a>
# eslint-plugin-sql

[![Travis build status](http://img.shields.io/travis/gajus/eslint-plugin-sql/master.svg?style=flat-square)](https://travis-ci.com/github/gajus/eslint-plugin-sql)
[![NPM version](http://img.shields.io/npm/v/eslint-plugin-sql.svg?style=flat-square)](https://www.npmjs.org/package/eslint-plugin-sql)
[![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
[![Twitter Follow](https://img.shields.io/twitter/follow/kuizinas.svg?style=social&label=Follow)](https://twitter.com/kuizinas)
Expand All @@ -10,16 +10,17 @@ SQL linting rules for ESLint.

> In its current form, the plugin has been designed and tested to work with Postgres codebase.
* [eslint-plugin-sql](#eslint-plugin-sql)
* [Installation](#eslint-plugin-sql-installation)
* [Configuration](#eslint-plugin-sql-configuration)
* [Settings](#eslint-plugin-sql-settings)
* [`placeholderRule`](#eslint-plugin-sql-settings-placeholderrule)
* [Rules](#eslint-plugin-sql-rules)
* [`format`](#eslint-plugin-sql-rules-format)
* [`no-unsafe-query`](#eslint-plugin-sql-rules-no-unsafe-query)
* [eslint-plugin-sql](#user-content-eslint-plugin-sql)
* [Installation](#user-content-eslint-plugin-sql-installation)
* [Configuration](#user-content-eslint-plugin-sql-configuration)
* [Settings](#user-content-eslint-plugin-sql-settings)
* [`placeholderRule`](#user-content-eslint-plugin-sql-settings-placeholderrule)
* [Rules](#user-content-eslint-plugin-sql-rules)
* [`format`](#user-content-eslint-plugin-sql-rules-format)
* [`no-unsafe-query`](#user-content-eslint-plugin-sql-rules-no-unsafe-query)


<a name="user-content-eslint-plugin-sql-installation"></a>
<a name="eslint-plugin-sql-installation"></a>
## Installation

Expand All @@ -33,6 +34,7 @@ npm install eslint --save-dev
npm install eslint-plugin-sql --save-dev
```

<a name="user-content-eslint-plugin-sql-configuration"></a>
<a name="eslint-plugin-sql-configuration"></a>
## Configuration

Expand Down Expand Up @@ -66,9 +68,11 @@ npm install eslint-plugin-sql --save-dev

```

<a name="user-content-eslint-plugin-sql-settings"></a>
<a name="eslint-plugin-sql-settings"></a>
## Settings

<a name="user-content-eslint-plugin-sql-settings-placeholderrule"></a>
<a name="eslint-plugin-sql-settings-placeholderrule"></a>
### <code>placeholderRule</code>

Expand All @@ -78,11 +82,13 @@ If you are using `?` placeholders in your queries, you must ignore `\?` pattern

This configuration is relevant for `sql/no-unsafe-query` to match queries containing placeholders as well as for `sql/format` when used with `{ignoreTagless: false}` configuration.

<a name="user-content-eslint-plugin-sql-rules"></a>
<a name="eslint-plugin-sql-rules"></a>
## Rules

<!-- Rules are sorted alphabetically. -->

<a name="user-content-eslint-plugin-sql-rules-format"></a>
<a name="eslint-plugin-sql-rules-format"></a>
### <code>format</code>

Expand All @@ -92,6 +98,7 @@ Matches queries in template literals. Warns when query formatting does not match

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

<a name="user-content-eslint-plugin-sql-rules-format-options"></a>
<a name="eslint-plugin-sql-rules-format-options"></a>
#### Options

Expand All @@ -106,63 +113,9 @@ The first option is an object with the following configuration.

The second option is an object with the [`pg-formatter` configuration](https://github.com/gajus/pg-formatter#configuration).

The following patterns are considered problems:

```js
`SELECT 1`
// Options: [{"ignoreInline":false,"ignoreTagless":false}]
// Message: Format the query
// Fixed code:
// `
// SELECT
// 1
// `

`SELECT 2`
// Options: [{"ignoreInline":false,"ignoreTagless":false},{"spaces":2}]
// Message: Format the query
// Fixed code:
// `
// SELECT
// 2
// `

sql`SELECT 3`
// Options: [{"ignoreInline":false}]
// Message: Format the query
// Fixed code:
// sql`
// SELECT
// 3
// `

`SELECT ${'foo'} FROM ${'bar'}`
// Options: [{"ignoreInline":false,"ignoreTagless":false}]
// Message: Format the query
// Fixed code:
// `
// SELECT
// ${'foo'}
// FROM
// ${'bar'}
// `
```

The following patterns are not considered problems:

```js
sql`SELECT 1`
// Options: [{"ignoreInline":true}]

`SELECT 2`
// Options: [{"ignoreTagless":true}]

`SELECT ${'foo'} FROM ${'bar'}`
// Options: [{"ignoreExpressions":true,"ignoreInline":false,"ignoreTagless":false}]
```



<a name="user-content-eslint-plugin-sql-rules-no-unsafe-query"></a>
<a name="eslint-plugin-sql-rules-no-unsafe-query"></a>
### <code>no-unsafe-query</code>

Expand All @@ -173,6 +126,7 @@ The `sql` tag can be anything, e.g.
* https://github.com/seegno/sql-tag
* https://github.com/gajus/mightyql#tagged-template-literals

<a name="user-content-eslint-plugin-sql-rules-no-unsafe-query-options-1"></a>
<a name="eslint-plugin-sql-rules-no-unsafe-query-options-1"></a>
#### Options

Expand All @@ -182,32 +136,5 @@ 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.|

The following patterns are considered problems:

```js
`SELECT 1`
// Message: Use "sql" tag

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

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

`SELECT ?`
// Message: Use "sql" tag
```

The following patterns are not considered problems:

```js
`SELECT 1`
// Options: [{"allowLiteral":true}]

sql`SELECT 1`

sql`SELECT ${'foo'}`
```



4 changes: 2 additions & 2 deletions bin/readmeAssertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ const formatCodeSnippet = (setup: Setup) => {
};

const getAssertions = () => {
const assertionFiles = glob.sync(path.resolve(__dirname, '../test/rules/assertions/*.js'));
const assertionFiles = glob.sync(path.resolve(__dirname, '../test/rules/assertions/*.ts'));

const assertionNames = _.map(assertionFiles, (filePath) => {
return path.basename(filePath, '.js');
return path.basename(filePath, '.ts');
});

const assertionCodes = _.map(assertionFiles, (filePath) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"url": "https://github.com/gajus/eslint-plugin-sql"
},
"scripts": {
"build": "tsc",
"build": "npm run documentation && tsc",
"documentation": "gitdown ./.README/README.md --output-file ./README.md && npm run documentation-add-assertions",
"documentation-add-assertions": "tsx ./bin/readmeAssertions",
"lint": "eslint ./src ./test && tsc --noEmit",
Expand Down
4 changes: 3 additions & 1 deletion src/rules/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const create = (context) => {

return {
TemplateLiteral (node) {
const sqlTagIsPresent = node.parent.tag?.name === 'sql' || node.parent.tag?.object?.name === 'sql' || node.parent.tag?.callee?.object?.name === 'sql';
const tagName = node.parent.tag?.name ?? node.parent.tag?.object?.name ?? node.parent.tag?.callee?.object?.name;

const sqlTagIsPresent = tagName === 'sql';

if (ignoreTagless && !sqlTagIsPresent) {
return;
Expand Down
3 changes: 2 additions & 1 deletion src/rules/noUnsafeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ const create = (context) => {
tag,
} = node.parent;

const tagName = node.parent.tag?.name ?? node.parent.tag?.object?.name ?? node.parent.tag?.callee?.object?.name;

const legacyTagName = tag?.name.toLowerCase();
const tagName = tag.property?.name.toLowerCase();

if (legacyTagName !== 'sql' && tagName !== 'sql') {
context.report({
Expand Down
2 changes: 1 addition & 1 deletion test/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ruleTester = new RuleTester({

const reportingRules = [
'format',
// 'no-unsafe-query',
'no-unsafe-query',
];

for (const ruleName of reportingRules) {
Expand Down

0 comments on commit d968b18

Please sign in to comment.