Skip to content

feat: ability to ignore diagnostics by code #272

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

Merged
merged 2 commits into from
Nov 13, 2021
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 @@ -159,6 +159,10 @@ Some of the preferences can be controlled through the `workspace/didChangeConfig
[language].inlayHints.includeInlayParameterNameHintsWhenArgumentMatchesName: boolean;
[language].inlayHints.includeInlayPropertyDeclarationTypeHints: boolean;
[language].inlayHints.includeInlayVariableTypeHints: boolean;
// Diagnostics code to be omitted when reporting diagnostics.
// See https://github.com/microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json for a full list of valid codes.
diagnostics.ignoredCodes: number[];

```

## Code actions on save
Expand Down
20 changes: 17 additions & 3 deletions src/diagnostic-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class FileDiagnostics {

export class DiagnosticEventQueue {
protected readonly diagnostics = new Map<string, FileDiagnostics>();
private ignoredDiagnosticCodes: Set<number> = new Set();

constructor(
protected readonly publishDiagnostics: (params: lsp.PublishDiagnosticsParams) => void,
Expand All @@ -59,10 +60,23 @@ export class DiagnosticEventQueue {
return;
}
const { file } = event.body;
let { diagnostics } = event.body;

if (this.ignoredDiagnosticCodes.size) {
diagnostics = diagnostics.filter(diagnostic => !this.isDiagnosticIgnored(diagnostic));
}
const uri = pathToUri(file, this.documents);
const diagnostics = this.diagnostics.get(uri) || new FileDiagnostics(
const diagnosticsForFile = this.diagnostics.get(uri) || new FileDiagnostics(
uri, this.publishDiagnostics, this.documents, this.publishDiagnosticsCapabilities);
diagnostics.update(kind, event.body.diagnostics);
this.diagnostics.set(uri, diagnostics);
diagnosticsForFile.update(kind, diagnostics);
this.diagnostics.set(uri, diagnosticsForFile);
}

updateIgnoredDiagnosticCodes(ignoredCodes: readonly number[]): void {
this.ignoredDiagnosticCodes = new Set(ignoredCodes);
}

private isDiagnosticIgnored(diagnostic: tsp.Diagnostic) : boolean {
return diagnostic.code !== undefined && this.ignoredDiagnosticCodes.has(diagnostic.code);
}
}
32 changes: 32 additions & 0 deletions src/lsp-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,38 @@ describe('diagnostics', () => {
assert.equal(diagnosticsForDoc!.diagnostics.length, 1, JSON.stringify(diagnostics));
assert.equal(diagnosticsForDoc2!.diagnostics.length, 1, JSON.stringify(diagnostics));
}).timeout(10000);

it('code 6133 (ununsed variable) is ignored', async () => {
server.didChangeConfiguration({
settings: {
diagnostics: {
ignoredCodes: [6133]
}
}
});

const doc = {
uri: uri('diagnosticsBar2.ts'),
languageId: 'typescript',
version: 1,
text: `
export function foo() {
const x = 42;
return 1;
}
`
};
server.didOpenTextDocument({
textDocument: doc
});

await server.requestDiagnostics();
await new Promise(resolve => setTimeout(resolve, 200));
const diagnosticsForThisFile = diagnostics.get(doc.uri);
assert.isDefined(diagnosticsForThisFile);
const fileDiagnostics = diagnosticsForThisFile!.diagnostics;
assert.equal(fileDiagnostics.length, 0, JSON.stringify(fileDiagnostics));
}).timeout(10000);
});

describe('document symbol', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/lsp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ export class LspServer {

didChangeConfiguration(params: lsp.DidChangeConfigurationParams): void {
this.workspaceConfiguration = params.settings || {};
const ignoredDiagnosticCodes = this.workspaceConfiguration.diagnostics?.ignoredCodes || [];
this.diagnosticQueue?.updateIgnoredDiagnosticCodes(ignoredDiagnosticCodes);
}

getWorkspacePreferencesForDocument(file: string): TypeScriptWorkspaceSettingsLanguageSettings {
Expand Down
5 changes: 5 additions & 0 deletions src/ts-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export interface TypeScriptWorkspaceSettingsLanguageSettings {
inlayHints?: InlayHintsOptions;
}

interface TypeScriptWorkspaceSettingsDiagnostics {
ignoredCodes?: number[];
}

export interface TypeScriptWorkspaceSettings {
javascript?: TypeScriptWorkspaceSettingsLanguageSettings;
typescript?: TypeScriptWorkspaceSettingsLanguageSettings;
diagnostics?: TypeScriptWorkspaceSettingsDiagnostics;
}