Skip to content

Commit

Permalink
feat: support only applying fix-later with --fix-type=directive (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Aug 16, 2024
1 parent e9c8b7c commit 81ff17c
Show file tree
Hide file tree
Showing 8 changed files with 553 additions and 327 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ In your ESLint config:

This approach prevents errors from slipping through while accommodating "fix later" notes.

### Suppressing auto-fixable errors (ESLint v8+)

Pass in the [`--fix-type=directive`](https://eslint.org/docs/latest/use/command-line-interface#--fix-type) flag to ESLint to only apply the fix-later auto-fix.

## Options

### includeWarnings
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"find-up-simple": "^1.0.0",
"fs-fixture": "^2.4.0",
"ignore": "^5.3.1",
"lintroll": "^1.7.1",
"lintroll": "^1.8.1",
"manten": "^1.3.0",
"outdent": "^0.8.0",
"pkgroll": "^2.4.1",
Expand Down
798 changes: 484 additions & 314 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

33 changes: 23 additions & 10 deletions src/rules/fix-later/fix-later.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ const suppressFileErrors = (

// If applying fix, only suppress errors that can't be fixed
if (fix) {
processMessages = processMessages.filter(message => !message.fix);
processMessages = processMessages.filter(message => (
!message.fix

// Filter that applies `--fix-type`
|| (typeof fix === 'function' && !fix(message))
));
} else {
const suppressableMessages = processMessages.filter(message => !message.fix);

Expand Down Expand Up @@ -179,24 +184,32 @@ const suppressFileErrors = (
comments.push(`<!-- eslint-enable ${rulesToDisable} -->`);
}

const line = Number(key);
const lineStartIndex = sourceCode.getIndexFromLoc({
line: Number(key),
line,
column: 0,
});

const comment = comments.join('\n');
const insertCommentAbove = (
ruleOptions.insertDisableComment === 'above-line'
|| groupedMessages.start.length > 0
|| groupedMessages.end.length > 0
);
messages.push({
ruleId,
/**
* Not specifiying a ruleId allows us to only apply this fix
* when --fix-type=directive is passed in
*
* https://github.com/eslint/eslint/blob/v8.0.0/lib/cli-engine/cli-engine.js#L342-L344
*/
ruleId: null,
severity: ruleSeverity,
message: 'Suppressing errors',
line: 0,
message: `fix-later: insert eslint comment on L${line + (insertCommentAbove ? 1 : 0)}`,
line,
column: 0,
fix: (
(
ruleOptions.insertDisableComment === 'above-line'
|| groupedMessages.start.length > 0
|| groupedMessages.end.length > 0
)
insertCommentAbove
? insertCommentAboveLine(
code,
lineStartIndex,
Expand Down
1 change: 0 additions & 1 deletion src/rules/fix-later/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const fixLater = {
additionalProperties: false,
},
],
type: 'problem',
},
create: (context) => {
const options = normalizeOptions(context.options[0]);
Expand Down
2 changes: 1 addition & 1 deletion src/rules/fix-later/utils/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AST } from 'vue-eslint-parser';

const safeRequire = <Type>(id: string) => {
try {
// eslint-disable-next-line import-x/no-dynamic-require, @typescript-eslint/no-var-requires
// eslint-disable-next-line import-x/no-dynamic-require, @typescript-eslint/no-require-imports
return require(id) as Type;
} catch {
return null;
Expand Down
35 changes: 35 additions & 0 deletions tests/specs/basic-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,41 @@ export default testSuite(({ describe }, eslintPath: string) => {
});
});

if (eslintPath.includes('eslint8')) {
test('apply fix-only with --fix-type=directive', async () => {
const content = outdent`
var foo = () => 0
`;
const result = await eslint(eslintPath, {
config: {
parserOptions: {
ecmaVersion: 2021,
},
rules: {
'fix-later/fix-later': ['warn', {
insertDisableComment: 'above-line',
}],
'arrow-body-style': ['error', 'always'],
},
},
code: {
content,
},
fix: true,

// Only applies fix-later and doesn't auto-fix the arrow-body-style
fixType: 'directive',
});

expect(result.output).toBe(
outdent`
// eslint-disable-next-line arrow-body-style -- Fix later
var foo = () => 0
`,
);
});
}

test('inherits indentation without mixing tabs + spaces', async () => {
const content = outdent`
if (true) {
Expand Down
5 changes: 5 additions & 0 deletions tests/utils/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Options = {
code: string | StdIn;
cwd?: string;
fix?: boolean;
fixType?: 'directive';
};

export const eslint = async (
Expand All @@ -24,6 +25,7 @@ export const eslint = async (
config: configRaw,
code,
fix,
fixType,
}: Options,
) => {
await installSelfPackage(cwd);
Expand All @@ -45,6 +47,9 @@ export const eslint = async (

if (fix) {
eslintArgs.push('--fix-dry-run');
if (fixType) {
eslintArgs.push(`--fix-type=${fixType}`);
}
}

if (typeof code === 'object') {
Expand Down

0 comments on commit 81ff17c

Please sign in to comment.