From 9da5b349ba78ae55c9a4ef6227a742c3102c3e6d Mon Sep 17 00:00:00 2001 From: Nikita Pavlov Date: Fri, 28 Jul 2023 19:06:33 +0000 Subject: [PATCH 1/3] fix: doubling new lines after format --- src/rules/format.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/format.ts b/src/rules/format.ts index 28850a9..ae6db33 100644 --- a/src/rules/format.ts +++ b/src/rules/format.ts @@ -77,7 +77,7 @@ const create = (context) => { node.quasis[0].range[0], node.quasis[node.quasis.length - 1].range[1], ], - '`\n' + final + '`', + '`' + (final.startsWith('\n') ? final : '\n' + final) + '`', ); }, message: 'Format the query', From 5a9358f97f855e2aa7457d547548245c9b2ec82c Mon Sep 17 00:00:00 2001 From: Nikita Pavlov Date: Fri, 28 Jul 2023 19:19:50 +0000 Subject: [PATCH 2/3] feat: ignore indent before linting --- package.json | 3 ++- src/rules/format.ts | 12 +++++++++++- test/rules/assertions/format.ts | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5ab32af..a8ea07f 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "debug": "^4.3.4", "lodash": "^4.17.21", "pg-formatter": "^2.0.2", - "sql-parse": "^0.1.5" + "sql-parse": "^0.1.5", + "strip-indent": "^3.0.0" }, "description": "SQL linting rules for ESLint.", "devDependencies": { diff --git a/src/rules/format.ts b/src/rules/format.ts index ae6db33..d8c7a30 100644 --- a/src/rules/format.ts +++ b/src/rules/format.ts @@ -1,6 +1,7 @@ import isSqlQuery from '../utilities/isSqlQuery'; import { generate } from 'astring'; import { format } from 'pg-formatter'; +import stripIndent from 'strip-indent'; const create = (context) => { const placeholderRule = context.settings?.sql?.placeholderRule; @@ -11,6 +12,7 @@ const create = (context) => { const ignoreInline = pluginOptions.ignoreInline !== false; const ignoreTagless = pluginOptions.ignoreTagless !== false; const ignoreStartWithNewLine = pluginOptions.ignoreStartWithNewLine !== false; + const ignoreBaseIndent = pluginOptions.ignoreBaseIndent === true; return { TemplateLiteral(node) { @@ -31,7 +33,7 @@ const create = (context) => { const magic = '"gajus-eslint-plugin-sql"'; - const literal = node.quasis + let literal = node.quasis .map((quasi) => { return quasi.value.raw; }) @@ -45,6 +47,10 @@ const create = (context) => { return; } + if (ignoreBaseIndent) { + literal = stripIndent(literal); + } + let formatted = format(literal, context.options[1]); if ( @@ -101,6 +107,10 @@ export = { { additionalProperties: false, properties: { + ignoreBaseIndent: { + default: false, + type: 'boolean', + }, ignoreExpressions: { default: false, type: 'boolean', diff --git a/test/rules/assertions/format.ts b/test/rules/assertions/format.ts index 82c9fb2..714fc3b 100644 --- a/test/rules/assertions/format.ts +++ b/test/rules/assertions/format.ts @@ -76,6 +76,21 @@ export default { ], output: "`\nSELECT\n ${'foo'}\nFROM\n ${'bar'}\n`", }, + { + code: " const code = sql`\n SELECT\n ${'foo'}\n FROM\n ${'bar'}\n`", + errors: [ + { + message: 'Format the query', + }, + ], + options: [ + { + ignoreBaseIndent: false, + }, + ], + output: + " const code = sql`\nSELECT\n ${'foo'}\nFROM\n ${'bar'}\n`", + }, ], valid: [ { @@ -104,5 +119,13 @@ export default { }, ], }, + { + code: " const code = sql`\n SELECT\n ${'foo'}\n FROM\n ${'bar'}\n`", + options: [ + { + ignoreBaseIndent: true, + }, + ], + }, ], }; From 7f1065a13af80c0cbf551d17331c6ac47eda78ed Mon Sep 17 00:00:00 2001 From: Nikita Pavlov Date: Fri, 28 Jul 2023 20:52:20 +0000 Subject: [PATCH 3/3] docs: ignoreBaseIndent --- .README/rules/format.md | 1 + README.md | 1 + 2 files changed, 2 insertions(+) diff --git a/.README/rules/format.md b/.README/rules/format.md index 3ce62e2..4d3beb0 100644 --- a/.README/rules/format.md +++ b/.README/rules/format.md @@ -12,6 +12,7 @@ The first option is an object with the following configuration. |configuration|format|default|description| |---|---|---|---| +|`ignoreBaseIndent`|boolean|`false`|Does not leave base indent before linting.| |`ignoreExpressions`|boolean|`false`|Does not format template literals that contain expressions.| |`ignoreInline`|boolean|`true`|Does not format queries that are written on a single line.| |`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.| diff --git a/README.md b/README.md index fd37db0..31854c5 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ The first option is an object with the following configuration. |configuration|format|default|description| |---|---|---|---| +|`ignoreBaseIndent`|boolean|`false`|Does not leave base indent before linting.| |`ignoreExpressions`|boolean|`false`|Does not format template literals that contain expressions.| |`ignoreInline`|boolean|`true`|Does not format queries that are written on a single line.| |`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|