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 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
24 changes: 22 additions & 2 deletions server/src/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,19 @@ export type RuleMetaData = {
};

export namespace RuleMetaData {
// 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';
const unusedDisableDirectiveMeta: RuleMetaData = {
docs: {
url: 'https://eslint.org/docs/latest/use/configure/rules#report-unused-eslint-disable-comments'
},
type: 'directive'
};

const handled: Set<string> = new Set();
const ruleId2Meta: Map<string, RuleMetaData> = new Map();
const ruleId2Meta: Map<string, RuleMetaData> = new Map([[unusedDisableDirectiveId, unusedDisableDirectiveMeta]]);

export function capture(eslint: ESLintClass, reports: ESLintDocumentReport[]): void {
let rulesMetaData: Record<string, RuleMetaData> | undefined;
Expand Down Expand Up @@ -154,6 +165,7 @@ export namespace RuleMetaData {
export function clear(): void {
handled.clear();
ruleId2Meta.clear();
ruleId2Meta.set(unusedDisableDirectiveId, unusedDisableDirectiveMeta);
}

export function getUrl(ruleId: string): string | undefined {
Expand All @@ -167,6 +179,10 @@ export namespace RuleMetaData {
export function hasRuleId(ruleId: string): boolean {
return 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
Contributor 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
Contributor 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...

}
}

export type ParserOptions = {
Expand Down Expand Up @@ -1081,7 +1097,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 @@ -1121,6 +1137,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
Contributor 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