Skip to content
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

feat: support only applying fix-later with --fix-type=directive #5

Merged
merged 5 commits into from
Aug 16, 2024
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
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 @@ -26,7 +26,7 @@
return ruleIds;
};

const suppressFileErrors = (

Check warning on line 29 in src/rules/fix-later/fix-later.ts

View workflow job for this annotation

GitHub Actions / Test

Arrow function has a complexity of 21. Maximum allowed is 10
code: string,
sourceCode: SourceCode,
extractedConfig: Linter.Config,
Expand Down Expand Up @@ -68,7 +68,12 @@

// 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 @@
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 All @@ -223,7 +236,7 @@
_verifyWithProcessor,
} = eslint.Linter.prototype;

eslint.Linter.prototype._verifyWithoutProcessors = function (

Check warning on line 239 in src/rules/fix-later/fix-later.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected unnamed function
textOrSourceCode,
config,
options,
Expand Down Expand Up @@ -254,7 +267,7 @@
* So a plugin's postprocess may remove suppressed errors
* We want to filter out after that
*/
eslint.Linter.prototype._verifyWithProcessor = function (

Check warning on line 270 in src/rules/fix-later/fix-later.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected unnamed function
textOrSourceCode,
config,
options,
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
Loading