From 473c87d8d327ae3b67386db8257514e8ecb65515 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Fri, 26 Jul 2024 17:56:11 +0900 Subject: [PATCH] fix: inherits clean indentation --- src/rules/fix-later/utils/fixer.ts | 9 +++++++-- tests/specs/basic-usage.ts | 32 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/rules/fix-later/utils/fixer.ts b/src/rules/fix-later/utils/fixer.ts index 3be545d..2cd520c 100644 --- a/src/rules/fix-later/utils/fixer.ts +++ b/src/rules/fix-later/utils/fixer.ts @@ -1,12 +1,17 @@ import type { Rule } from 'eslint'; +/** + * Regex to only match a sequence of tabs or spaces (not both) + * \s is not used because it can include new lines which we don't want + */ +const tabOrSpaces = /^([\t ]?)\1*/; + export const insertCommentAboveLine = ( code: string, lineStart: number, comment: string, ): Rule.Fix => { - // \s can include new lines which we don't want - const indentation = code.slice(lineStart).match(/^[\t ]*/)![0]; + const indentation = code.slice(lineStart).match(tabOrSpaces)![0]; return { range: [lineStart, lineStart], diff --git a/tests/specs/basic-usage.ts b/tests/specs/basic-usage.ts index 7282e72..e530256 100644 --- a/tests/specs/basic-usage.ts +++ b/tests/specs/basic-usage.ts @@ -249,6 +249,38 @@ export default testSuite(({ describe }, eslintPath: string) => { }); }); + test('inherits indentation without mixing tabs + spaces', async () => { + const content = outdent` + if (true) { + console.log() + } + `; + expect(content).toMatch('\n\t console'); + const result = await eslint(eslintPath, { + config: { + rules: { + 'fix-later/fix-later': ['warn', { + insertDisableComment: 'above-line', + }], + 'no-mixed-spaces-and-tabs': 'error', + }, + }, + code: { + content, + }, + fix: true, + }); + + expect(result.output).toBe( + outdent` + if (true) { + // eslint-disable-next-line no-mixed-spaces-and-tabs -- Fix later + console.log() + } + `, + ); + }); + test('vue', async () => { const result = await eslint(eslintPath, { config: {