Skip to content

Commit

Permalink
Add "unused" flag to diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hanson committed Mar 12, 2018
1 parent 90313d2 commit 013840f
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,7 @@ namespace ts {
messageText: text,
category: message.category,
code: message.code,
unused: message.unused,
};
}

Expand Down Expand Up @@ -1653,7 +1654,8 @@ namespace ts {

messageText: text,
category: message.category,
code: message.code
code: message.code,
unused: message.unused,
};
}

Expand All @@ -1665,7 +1667,7 @@ namespace ts {

code: chain.code,
category: chain.category,
messageText: chain.next ? chain : chain.messageText
messageText: chain.next ? chain : chain.messageText,
};
}

Expand Down
12 changes: 8 additions & 4 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3250,7 +3250,8 @@
},
"'{0}' is declared but its value is never read.": {
"category": "Error",
"code": 6133
"code": 6133,
"unused": true
},
"Report errors on unused locals.": {
"category": "Message",
Expand All @@ -3270,7 +3271,8 @@
},
"Property '{0}' is declared but its value is never read.": {
"category": "Error",
"code": 6138
"code": 6138,
"unused": true
},
"Import emit helpers from 'tslib'.": {
"category": "Message",
Expand Down Expand Up @@ -3482,7 +3484,8 @@
},
"All imports in import declaration are unused.": {
"category": "Error",
"code": 6192
"code": 6192,
"unused": true
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
Expand Down Expand Up @@ -3562,7 +3565,8 @@
},
"Unused label.": {
"category": "Error",
"code": 7028
"code": 7028,
"unused": true
},
"Fallthrough case in switch.": {
"category": "Error",
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4009,6 +4009,7 @@ namespace ts {
category: DiagnosticCategory;
code: number;
message: string;
unused?: {};
}

/**
Expand All @@ -4030,6 +4031,8 @@ namespace ts {
length: number | undefined;
messageText: string | DiagnosticMessageChain;
category: DiagnosticCategory;
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
unused?: {};
code: number;
source?: string;
}
Expand Down
7 changes: 4 additions & 3 deletions src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,11 @@ namespace ts.server {
return this.getDiagnostics(file, CommandNames.SuggestionDiagnosticsSync);
}

private getDiagnostics(file: string, command: CommandNames) {
private getDiagnostics(file: string, command: CommandNames): Diagnostic[] {
const request = this.processRequest<protocol.SyntacticDiagnosticsSyncRequest | protocol.SemanticDiagnosticsSyncRequest | protocol.SuggestionDiagnosticsSyncRequest>(command, { file, includeLinePosition: true });
const response = this.processResponse<protocol.SyntacticDiagnosticsSyncResponse | protocol.SemanticDiagnosticsSyncResponse | protocol.SuggestionDiagnosticsSyncResponse>(request);

return (<protocol.DiagnosticWithLinePosition[]>response.body).map(entry => {
return (<protocol.DiagnosticWithLinePosition[]>response.body).map<Diagnostic>(entry => {
const category = firstDefined(Object.keys(DiagnosticCategory), id =>
isString(id) && entry.category === id.toLowerCase() ? (<any>DiagnosticCategory)[id] : undefined);
return {
Expand All @@ -365,7 +365,8 @@ namespace ts.server {
length: entry.length,
messageText: entry.message,
category: Debug.assertDefined(category, "convertDiagnostic: category should not be undefined"),
code: entry.code
code: entry.code,
unused: entry.unused,
};
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ namespace ts.server.protocol {
endLocation: Location;
category: string;
code: number;
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
unused?: {};
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/services/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ namespace ts {
length: number;
category: string;
code: number;
unused?: {};
}
export function realizeDiagnostics(diagnostics: ReadonlyArray<Diagnostic>, newLine: string): RealizedDiagnostic[] {
return diagnostics.map(d => realizeDiagnostic(d, newLine));
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/fourslash/codeFixUnusedIdentifier_suggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ verify.getSuggestionDiagnostics([
message: "'p' is declared but its value is never read.",
range: r0,
code: 6133,
unused: true,
},
{
message: "'x' is declared but its value is never read.",
range: r1,
code: 6133,
unused: true,
}
]);

Expand Down
1 change: 1 addition & 0 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ declare namespace FourSlashInterface {
/** @default `test.ranges()[0]` */
range?: Range;
code: number;
unused?: true;
}
}
declare function verifyOperationIsCancelled(f: any): void;
Expand Down

0 comments on commit 013840f

Please sign in to comment.