Skip to content

Commit

Permalink
fix: multiquery deindent
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Aug 3, 2023
2 parents 3471a1c + a41fd2d commit eac47b9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 30 deletions.
3 changes: 1 addition & 2 deletions cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ words:
- tagless
- gitdown
- astring
- quasis
- deindent
- quasis
24 changes: 0 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"dependencies": {
"astring": "^1.8.3",
"debug": "^4.3.4",
"deindent": "^0.1.0",
"lodash": "^4.17.21",
"pg-formatter": "^2.0.2",
"sql-parse": "^0.1.5"
Expand All @@ -17,7 +16,6 @@
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/github": "^8.0.7",
"@semantic-release/npm": "^10.0.3",
"@types/deindent": "^0.1.0",
"@types/mocha": "^10.0.0",
"@types/node": "^18.11.9",
"cspell": "^6.31.1",
Expand Down
8 changes: 6 additions & 2 deletions src/rules/format.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dropBaseIndent from '../utilities/dropBaseIndent';
import isSqlQuery from '../utilities/isSqlQuery';
import { generate } from 'astring';
import deindent from 'deindent';
import { format } from 'pg-formatter';

const create = (context) => {
Expand Down Expand Up @@ -48,7 +48,7 @@ const create = (context) => {
}

if (ignoreBaseIndent) {
literal = deindent(literal);
literal = dropBaseIndent(literal);
}

let formatted = format(literal, context.options[1]);
Expand All @@ -61,6 +61,10 @@ const create = (context) => {
formatted = '\n' + formatted;
}

if (formatted.endsWith('\n\n')) {
formatted = formatted.replace(/\n\n$/u, '\n');
}

if (formatted !== literal) {
context.report({
fix: (fixer) => {
Expand Down
12 changes: 12 additions & 0 deletions src/utilities/dropBaseIndent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// based on https://github.com/sindresorhus/strip-indent
export default (raw: string) => {
const trimmedString = raw.replace(/ *$/u, '');

const matches = trimmedString.match(/^[\t ]*(?=\S)/gmu);
if (!matches) return trimmedString;

const indent = Math.min(...matches.map((match) => match.length));
if (!indent) return trimmedString;

return trimmedString.replaceAll(new RegExp(`^[ \\t]{${indent}}`, 'gmu'), '');
};
8 changes: 8 additions & 0 deletions test/rules/assertions/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,13 @@ export default {
},
],
},
{
code: ' const code = sql`\n DROP TABLE foo;\n\n DROP TABLE foo;\n `;',
options: [
{
ignoreBaseIndent: true,
},
],
},
],
};

0 comments on commit eac47b9

Please sign in to comment.