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 4 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
38 changes: 37 additions & 1 deletion server/src/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,23 @@ 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) {
const toHandle = reports.filter(report => !handled.has(report.filePath));
if (toHandle.length === 0) {
return;
}
addUnusedDisableDirectivesMeta(toHandle);
rulesMetaData = typeof eslint.getRulesMetaForResults === 'function' ? eslint.getRulesMetaForResults(toHandle) : undefined;
toHandle.forEach(report => handled.add(report.filePath));
} else {
addUnusedDisableDirectivesMeta(reports);
rulesMetaData = typeof eslint.getRulesMetaForResults === 'function' ? eslint.getRulesMetaForResults(reports) : undefined;
}
if (rulesMetaData === undefined) {
Expand Down Expand Up @@ -167,6 +174,31 @@ 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
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...

}

function addUnusedDisableDirectivesMeta(reports: ESLintDocumentReport[]): void {
if (hasRuleId(unusedDisableDirectiveId)) {
return;
}

for (const report of reports) {
for (const message of report.messages) {
if (isUnusedDisableDirectiveProblem(message)) {
ruleId2Meta.set(unusedDisableDirectiveId, {
docs: {
url: 'https://eslint.org/docs/latest/use/configure/rules#report-unused-eslint-disable-comments'
},
type: 'directive'
});

return;
}
}
}
}
}

export type ParserOptions = {
Expand Down Expand Up @@ -1071,7 +1103,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 +1143,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