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

Handle unused directive problems #1588

Merged
merged 9 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions server/src/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export namespace RuleMetaData {
const handled: Set<string> = new Set();
const ruleId2Meta: Map<string, RuleMetaData> = new Map();

// For unused eslint-disable comments, ESLint does not include a rule ID
// nor any other metadata (although they do provide a fix). In order to
// provide code actions for these, we create a fake rule ID and metadata.
export const unusedDisableDirectiveId = 'unused-disable-directive';

export function capture(eslint: ESLintClass, reports: ESLintDocumentReport[]): void {
let rulesMetaData: Record<string, RuleMetaData> | undefined;
if (eslint.isCLIEngine) {
Expand Down Expand Up @@ -157,15 +162,27 @@ export namespace RuleMetaData {
}

export function getUrl(ruleId: string): string | undefined {
if (ruleId === unusedDisableDirectiveId) {
MariaSolOs marked this conversation as resolved.
Show resolved Hide resolved
return 'https://eslint.org/docs/latest/use/configure/rules#report-unused-eslint-disable-comments';
}

return ruleId2Meta.get(ruleId)?.docs?.url;
}

export function getType(ruleId: string): string | undefined {
if (ruleId === unusedDisableDirectiveId) {
return 'directive';
}

return ruleId2Meta.get(ruleId)?.type;
}

export function hasRuleId(ruleId: string): boolean {
return ruleId2Meta.has(ruleId);
return ruleId === unusedDisableDirectiveId || ruleId2Meta.has(ruleId);
}

export function isUnusedDisableDirectiveProblem(problem: ESLintProblem): boolean {
return problem.ruleId === null && problem.message.startsWith('Unused eslint-disable directive');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last question: I checked myself and I think the answer is no: but does ESLint support localization. If not right now then we are fine but this check might break in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. All I could find was this proposal but it looks stale, and for this specific diagnostic the message is hardcoded here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think we're good for now...

}
}

Expand Down Expand Up @@ -1071,7 +1088,7 @@ export namespace ESLint {
}
}

const validFixTypes = new Set<string>(['problem', 'suggestion', 'layout']);
const validFixTypes = new Set<string>(['problem', 'suggestion', 'layout', 'directive']);
export async function validate(document: TextDocument, settings: TextDocumentSettings & { library: ESLintModule }): Promise<Diagnostic[]> {
const newOptions: CLIOptions = Object.assign(Object.create(null), settings.options);
let fixTypes: Set<string> | undefined = undefined;
Expand Down Expand Up @@ -1111,6 +1128,10 @@ export namespace ESLint {
CodeActions.record(document, diagnostic, problem);
}
} else {
if (RuleMetaData.isUnusedDisableDirectiveProblem(problem)) {
problem.ruleId = RuleMetaData.unusedDisableDirectiveId;
}

CodeActions.record(document, diagnostic, problem);
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/eslintServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ messageQueue.registerRequest(CodeActionRequest.type, async (params) => {
});
}

if (settings.codeAction.disableRuleComment.enable) {
if (settings.codeAction.disableRuleComment.enable && ruleId !== RuleMetaData.unusedDisableDirectiveId) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since unused-directive problems are disabling comments themselves, we cannot disable them.

let workspaceChange = new WorkspaceChange();
if (settings.codeAction.disableRuleComment.location === 'sameLine') {
workspaceChange.getTextEditChange({ uri, version: documentVersion }).add(createDisableSameLineTextEdit(textDocument, editInfo));
Expand Down