From 1a820a0a1bbb3259171f6ff871456a8d9a67bbf6 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Tue, 8 May 2018 16:18:43 -0700 Subject: [PATCH] Add setting to control show unused variables --- .../typescript-language-features/package.json | 12 +++++++++ .../package.nls.json | 3 ++- .../src/features/diagnostics.ts | 27 ++++++++++++------- .../src/languageProvider.ts | 8 +++--- .../src/typeScriptServiceClientHost.ts | 15 +++++++---- 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/extensions/typescript-language-features/package.json b/extensions/typescript-language-features/package.json index dbb5650f514d5..ab8007e448c30 100644 --- a/extensions/typescript-language-features/package.json +++ b/extensions/typescript-language-features/package.json @@ -470,6 +470,18 @@ "default": "auto", "description": "%typescript.preferences.importModuleSpecifier%", "scope": "resource" + }, + "javascript.showUnused.enabled": { + "type": "boolean", + "default": true, + "description": "%typescript.showUnused.enabled%", + "scope": "resource" + }, + "typescript.showUnused.enabled": { + "type": "boolean", + "default": true, + "description": "%typescript.showUnused.enabled%", + "scope": "resource" } } }, diff --git a/extensions/typescript-language-features/package.nls.json b/extensions/typescript-language-features/package.nls.json index fba3a419edb49..66b199464c2f3 100644 --- a/extensions/typescript-language-features/package.nls.json +++ b/extensions/typescript-language-features/package.nls.json @@ -56,5 +56,6 @@ "javascript.suggestionActions.enabled": "Enable/disable suggestion diagnostics for JavaScript files in the editor. Requires TypeScript >= 2.8", "typescript.suggestionActions.enabled": "Enable/disable suggestion diagnostics for TypeScript files in the editor. Requires TypeScript >= 2.8", "typescript.preferences.quoteStyle": "Preferred quote style to use for quick fixes: 'single' quotes, 'double' quotes, or 'auto' infer quote type from existing imports. Requires TS >= 2.9", - "typescript.preferences.importModuleSpecifier": "Preferred path style for auto imports: 'relative' paths, 'non-relative' paths, or 'auto' infer the shortest path type. Requires TS >= 2.9" + "typescript.preferences.importModuleSpecifier": "Preferred path style for auto imports: 'relative' paths, 'non-relative' paths, or 'auto' infer the shortest path type. Requires TS >= 2.9", + "typescript.showUnused.enabled": "Enable/disable highlighting of unused variables in code. Requires TypeScript >= 2.9" } \ No newline at end of file diff --git a/extensions/typescript-language-features/src/features/diagnostics.ts b/extensions/typescript-language-features/src/features/diagnostics.ts index 9893eeb690bd5..fc42e0518b79d 100644 --- a/extensions/typescript-language-features/src/features/diagnostics.ts +++ b/extensions/typescript-language-features/src/features/diagnostics.ts @@ -90,11 +90,11 @@ export class DiagnosticsManager { public diagnosticsReceived( kind: DiagnosticKind, file: vscode.Uri, - syntaxDiagnostics: vscode.Diagnostic[] + diagnostics: vscode.Diagnostic[] ): void { - const diagnostics = this._diagnostics.get(kind); - if (diagnostics) { - diagnostics.set(file, syntaxDiagnostics); + const collection = this._diagnostics.get(kind); + if (collection) { + collection.set(file, diagnostics); this.updateCurrentDiagnostics(file); } } @@ -112,15 +112,22 @@ export class DiagnosticsManager { return; } - const allDiagnostics: vscode.Diagnostic[] = []; - allDiagnostics.push(...this._diagnostics.get(DiagnosticKind.Syntax)!.get(file)); - allDiagnostics.push(...this._diagnostics.get(DiagnosticKind.Semantic)!.get(file)); - if (this._enableSuggestions) { - allDiagnostics.push(...this._diagnostics.get(DiagnosticKind.Suggestion)!.get(file)); - } + const allDiagnostics = [ + ...this._diagnostics.get(DiagnosticKind.Syntax)!.get(file), + ...this._diagnostics.get(DiagnosticKind.Semantic)!.get(file), + ...this.getSuggestionDiagnostics(file), + ]; this._currentDiagnostics.set(file, allDiagnostics); } + private getSuggestionDiagnostics(file: vscode.Uri) { + if (!this._enableSuggestions) { + return []; + } + + return this._diagnostics.get(DiagnosticKind.Suggestion)!.get(file); + } + public getDiagnostics(file: vscode.Uri): vscode.Diagnostic[] { return this._currentDiagnostics.get(file) || []; } diff --git a/extensions/typescript-language-features/src/languageProvider.ts b/extensions/typescript-language-features/src/languageProvider.ts index 08a509b83ae0a..2c0679e13a169 100644 --- a/extensions/typescript-language-features/src/languageProvider.ts +++ b/extensions/typescript-language-features/src/languageProvider.ts @@ -170,7 +170,7 @@ export default class LanguageProvider { } private configurationChanged(): void { - const config = workspace.getConfiguration(this.id); + const config = workspace.getConfiguration(this.id, null); this.updateValidate(config.get(validateSetting, true)); this.updateSuggestionDiagnostics(config.get(suggestionSetting, true)); @@ -258,8 +258,10 @@ export default class LanguageProvider { this.bufferSyncSupport.requestAllDiagnostics(); } - public diagnosticsReceived(diagnosticsKind: DiagnosticKind, file: Uri, syntaxDiagnostics: Diagnostic[]): void { - this.diagnosticsManager.diagnosticsReceived(diagnosticsKind, file, syntaxDiagnostics); + public diagnosticsReceived(diagnosticsKind: DiagnosticKind, file: Uri, diagnostics: (Diagnostic & { reportUnnecessary: any })[]): void { + const config = workspace.getConfiguration(this.id); + const reportUnnecessary = config.get('showUnused.enabled', true); + this.diagnosticsManager.diagnosticsReceived(diagnosticsKind, file, diagnostics.filter(diag => diag.reportUnnecessary ? reportUnnecessary : true)); } public configFileDiagnosticsReceived(file: Uri, diagnostics: Diagnostic[]): void { diff --git a/extensions/typescript-language-features/src/typeScriptServiceClientHost.ts b/extensions/typescript-language-features/src/typeScriptServiceClientHost.ts index ed859fc3b5be8..eb04c9ed633c1 100644 --- a/extensions/typescript-language-features/src/typeScriptServiceClientHost.ts +++ b/extensions/typescript-language-features/src/typeScriptServiceClientHost.ts @@ -146,8 +146,9 @@ export default class TypeScriptServiceClientHost { } private configurationChanged(): void { - const config = workspace.getConfiguration('typescript'); - this.reportStyleCheckAsWarnings = config.get('reportStyleChecksAsWarnings', true); + const typescriptConfig = workspace.getConfiguration('typescript'); + + this.reportStyleCheckAsWarnings = typescriptConfig.get('reportStyleChecksAsWarnings', true); } private async findLanguage(resource: Uri): Promise { @@ -239,11 +240,14 @@ export default class TypeScriptServiceClientHost { }); } - private createMarkerDatas(diagnostics: Proto.Diagnostic[], source: string): Diagnostic[] { + private createMarkerDatas( + diagnostics: Proto.Diagnostic[], + source: string + ): (Diagnostic & { reportUnnecessary: any })[] { return diagnostics.map(tsDiag => this.tsDiagnosticToVsDiagnostic(tsDiag, source)); } - private tsDiagnosticToVsDiagnostic(diagnostic: Proto.Diagnostic, source: string) { + private tsDiagnosticToVsDiagnostic(diagnostic: Proto.Diagnostic, source: string): Diagnostic & { reportUnnecessary: any } { const { start, end, text } = diagnostic; const range = new Range(typeConverters.Position.fromLocation(start), typeConverters.Position.fromLocation(end)); const converted = new Diagnostic(range, text); @@ -252,7 +256,8 @@ export default class TypeScriptServiceClientHost { if (diagnostic.code) { converted.code = diagnostic.code; } - return converted; + (converted as Diagnostic & { reportUnnecessary: any }).reportUnnecessary = diagnostic.reportsUnnecessary; + return converted as Diagnostic & { reportUnnecessary: any }; } private getDiagnosticSeverity(diagnostic: Proto.Diagnostic): DiagnosticSeverity {