Skip to content

Fix doubling of new lines and adding indent ignore feature #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2023
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
1 change: 1 addition & 0 deletions .README/rules/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.|
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.|
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 12 additions & 2 deletions src/rules/format.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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;
})
Expand All @@ -45,6 +47,10 @@ const create = (context) => {
return;
}

if (ignoreBaseIndent) {
literal = stripIndent(literal);
}

let formatted = format(literal, context.options[1]);

if (
Expand Down Expand Up @@ -77,7 +83,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',
Expand All @@ -101,6 +107,10 @@ export = {
{
additionalProperties: false,
properties: {
ignoreBaseIndent: {
default: false,
type: 'boolean',
},
ignoreExpressions: {
default: false,
type: 'boolean',
Expand Down
23 changes: 23 additions & 0 deletions test/rules/assertions/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -104,5 +119,13 @@ export default {
},
],
},
{
code: " const code = sql`\n SELECT\n ${'foo'}\n FROM\n ${'bar'}\n`",
options: [
{
ignoreBaseIndent: true,
},
],
},
],
};