Skip to content

Commit 8cae633

Browse files
author
Andy Hanson
committed
Add "unused" flag to diagnostics
1 parent 90313d2 commit 8cae633

File tree

11 files changed

+34
-9
lines changed

11 files changed

+34
-9
lines changed

src/compiler/core.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,7 @@ namespace ts {
16241624
messageText: text,
16251625
category: message.category,
16261626
code: message.code,
1627+
unused: message.unused,
16271628
};
16281629
}
16291630

@@ -1653,7 +1654,8 @@ namespace ts {
16531654

16541655
messageText: text,
16551656
category: message.category,
1656-
code: message.code
1657+
code: message.code,
1658+
unused: message.unused,
16571659
};
16581660
}
16591661

@@ -1665,7 +1667,7 @@ namespace ts {
16651667

16661668
code: chain.code,
16671669
category: chain.category,
1668-
messageText: chain.next ? chain : chain.messageText
1670+
messageText: chain.next ? chain : chain.messageText,
16691671
};
16701672
}
16711673

src/compiler/diagnosticMessages.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -3250,7 +3250,8 @@
32503250
},
32513251
"'{0}' is declared but its value is never read.": {
32523252
"category": "Error",
3253-
"code": 6133
3253+
"code": 6133,
3254+
"unused": true
32543255
},
32553256
"Report errors on unused locals.": {
32563257
"category": "Message",
@@ -3270,7 +3271,8 @@
32703271
},
32713272
"Property '{0}' is declared but its value is never read.": {
32723273
"category": "Error",
3273-
"code": 6138
3274+
"code": 6138,
3275+
"unused": true
32743276
},
32753277
"Import emit helpers from 'tslib'.": {
32763278
"category": "Message",
@@ -3482,7 +3484,8 @@
34823484
},
34833485
"All imports in import declaration are unused.": {
34843486
"category": "Error",
3485-
"code": 6192
3487+
"code": 6192,
3488+
"unused": true
34863489
},
34873490
"Variable '{0}' implicitly has an '{1}' type.": {
34883491
"category": "Error",
@@ -3562,7 +3565,8 @@
35623565
},
35633566
"Unused label.": {
35643567
"category": "Error",
3565-
"code": 7028
3568+
"code": 7028,
3569+
"unused": true
35663570
},
35673571
"Fallthrough case in switch.": {
35683572
"category": "Error",

src/compiler/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4009,6 +4009,7 @@ namespace ts {
40094009
category: DiagnosticCategory;
40104010
code: number;
40114011
message: string;
4012+
unused?: {};
40124013
}
40134014

40144015
/**
@@ -4030,6 +4031,8 @@ namespace ts {
40304031
length: number | undefined;
40314032
messageText: string | DiagnosticMessageChain;
40324033
category: DiagnosticCategory;
4034+
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
4035+
unused?: {};
40334036
code: number;
40344037
source?: string;
40354038
}

src/harness/unittests/matchFiles.ts

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ namespace ts {
130130
length: undefined,
131131
messageText: error.messageText,
132132
start: undefined,
133+
unused: undefined,
133134
}));
134135
assertParsed(actual, expected);
135136
}

src/server/client.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ namespace ts.server {
352352
return this.getDiagnostics(file, CommandNames.SuggestionDiagnosticsSync);
353353
}
354354

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

359-
return (<protocol.DiagnosticWithLinePosition[]>response.body).map(entry => {
359+
return (<protocol.DiagnosticWithLinePosition[]>response.body).map<Diagnostic>(entry => {
360360
const category = firstDefined(Object.keys(DiagnosticCategory), id =>
361361
isString(id) && entry.category === id.toLowerCase() ? (<any>DiagnosticCategory)[id] : undefined);
362362
return {
@@ -365,7 +365,8 @@ namespace ts.server {
365365
length: entry.length,
366366
messageText: entry.message,
367367
category: Debug.assertDefined(category, "convertDiagnostic: category should not be undefined"),
368-
code: entry.code
368+
code: entry.code,
369+
unused: entry.unused,
369370
};
370371
});
371372
}

src/server/protocol.ts

+2
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ namespace ts.server.protocol {
455455
endLocation: Location;
456456
category: string;
457457
code: number;
458+
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
459+
unused?: {};
458460
}
459461

460462
/**

src/services/shims.ts

+1
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ namespace ts {
588588
length: number;
589589
category: string;
590590
code: number;
591+
unused?: {};
591592
}
592593
export function realizeDiagnostics(diagnostics: ReadonlyArray<Diagnostic>, newLine: string): RealizedDiagnostic[] {
593594
return diagnostics.map(d => realizeDiagnostic(d, newLine));

tests/baselines/reference/api/tsserverlibrary.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,7 @@ declare namespace ts {
22542254
category: DiagnosticCategory;
22552255
code: number;
22562256
message: string;
2257+
unused?: {};
22572258
}
22582259
/**
22592260
* A linked list of formatted diagnostic messages to be used as part of a multiline message.
@@ -2273,6 +2274,8 @@ declare namespace ts {
22732274
length: number | undefined;
22742275
messageText: string | DiagnosticMessageChain;
22752276
category: DiagnosticCategory;
2277+
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
2278+
unused?: {};
22762279
code: number;
22772280
source?: string;
22782281
}
@@ -5345,6 +5348,8 @@ declare namespace ts.server.protocol {
53455348
endLocation: Location;
53465349
category: string;
53475350
code: number;
5351+
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
5352+
unused?: {};
53485353
}
53495354
/**
53505355
* Response message for "projectInfo" request

tests/baselines/reference/api/typescript.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,7 @@ declare namespace ts {
22542254
category: DiagnosticCategory;
22552255
code: number;
22562256
message: string;
2257+
unused?: {};
22572258
}
22582259
/**
22592260
* A linked list of formatted diagnostic messages to be used as part of a multiline message.
@@ -2273,6 +2274,8 @@ declare namespace ts {
22732274
length: number | undefined;
22742275
messageText: string | DiagnosticMessageChain;
22752276
category: DiagnosticCategory;
2277+
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
2278+
unused?: {};
22762279
code: number;
22772280
source?: string;
22782281
}

tests/cases/fourslash/codeFixUnusedIdentifier_suggestion.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ verify.getSuggestionDiagnostics([
1010
message: "'p' is declared but its value is never read.",
1111
range: r0,
1212
code: 6133,
13+
unused: true,
1314
},
1415
{
1516
message: "'x' is declared but its value is never read.",
1617
range: r1,
1718
code: 6133,
19+
unused: true,
1820
}
1921
]);
2022

tests/cases/fourslash/fourslash.ts

+1
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ declare namespace FourSlashInterface {
527527
/** @default `test.ranges()[0]` */
528528
range?: Range;
529529
code: number;
530+
unused?: true;
530531
}
531532
}
532533
declare function verifyOperationIsCancelled(f: any): void;

0 commit comments

Comments
 (0)