Skip to content

Commit

Permalink
fix: inherits clean indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Jul 26, 2024
1 parent 57e1c6e commit 473c87d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/rules/fix-later/utils/fixer.ts
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
32 changes: 32 additions & 0 deletions tests/specs/basic-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 473c87d

Please sign in to comment.