Skip to content

Commit 952e862

Browse files
committed
feat(cli): add support for suggestion diagnostics
1 parent 3454b67 commit 952e862

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

packages/cli/index.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import languagePlugins = require('./lib/languagePlugins.js');
1111
process.env.TSSLINT_CLI = '1';
1212

1313
const _reset = '\x1b[0m';
14-
const purple = (s: string) => '\x1b[35m' + s + _reset;
15-
const cyan = (s: string) => '\x1b[36m' + s + _reset;
1614
const darkGray = (s: string) => '\x1b[90m' + s + _reset;
1715
const lightRed = (s: string) => '\x1b[91m' + s + _reset;
1816
const lightGreen = (s: string) => '\x1b[92m' + s + _reset;
1917
const lightYellow = (s: string) => '\x1b[93m' + s + _reset;
2018
const lightBlue = (s: string) => '\x1b[94m' + s + _reset;
19+
const purple = (s: string) => '\x1b[95m' + s + _reset;
20+
const cyan = (s: string) => '\x1b[96m' + s + _reset;
2121

2222
// https://talyian.github.io/ansicolors/
2323
const tsColor = (s: string) => '\x1b[34m' + s + _reset;
@@ -126,6 +126,7 @@ class Project {
126126
let errors = 0;
127127
let warnings = 0;
128128
let messages = 0;
129+
let suggestions = 0;
129130
let cached = 0;
130131

131132
if (isTTY) {
@@ -353,6 +354,7 @@ class Project {
353354
[errors, 'errors', lightRed] as const,
354355
[warnings, 'warnings', lightYellow] as const,
355356
[messages, 'messages', lightBlue] as const,
357+
[suggestions, 'suggestions', darkGray] as const,
356358
[excluded, 'excluded', darkGray] as const,
357359
];
358360

@@ -425,25 +427,36 @@ class Project {
425427
project.cache[fileName] = fileCache = [fileStat.mtimeMs, {}, {}];
426428
}
427429

428-
let diagnostics = await linterWorker.lint(
430+
const diagnostics = await linterWorker.lint(
429431
fileName,
430432
process.argv.includes('--fix'),
431433
fileCache
432434
);
433-
434-
diagnostics = diagnostics.filter(diagnostic => diagnostic.category !== ts.DiagnosticCategory.Suggestion);
435+
const formatHost: ts.FormatDiagnosticsHost = {
436+
getCurrentDirectory: ts.sys.getCurrentDirectory,
437+
getCanonicalFileName: ts.sys.useCaseSensitiveFileNames ? x => x : x => x.toLowerCase(),
438+
getNewLine: () => ts.sys.newLine,
439+
};
435440

436441
if (diagnostics.length) {
437442
hasFix ||= await linterWorker.hasCodeFixes(fileName);
438443

439444
for (const diagnostic of diagnostics) {
440445
hasFix ||= !!fileCache[1][diagnostic.code]?.[0];
441446

442-
let output = ts.formatDiagnosticsWithColorAndContext([diagnostic], {
443-
getCurrentDirectory: ts.sys.getCurrentDirectory,
444-
getCanonicalFileName: ts.sys.useCaseSensitiveFileNames ? x => x : x => x.toLowerCase(),
445-
getNewLine: () => ts.sys.newLine,
446-
});
447+
let output: string;
448+
449+
if (diagnostic.category === ts.DiagnosticCategory.Suggestion) {
450+
output = ts.formatDiagnosticsWithColorAndContext([{
451+
...diagnostic,
452+
category: ts.DiagnosticCategory.Message,
453+
}], formatHost);
454+
output = output.replace(/\[94mmessage/, '[90msuggestion');
455+
output = output.replace(/\[94m/g, '[90m');
456+
} else {
457+
output = ts.formatDiagnosticsWithColorAndContext([diagnostic], formatHost);
458+
}
459+
447460
output = output.trimEnd();
448461

449462
if (typeof diagnostic.code === 'string') {
@@ -463,6 +476,7 @@ class Project {
463476
log(output);
464477
}
465478
else {
479+
suggestions++;
466480
log(output);
467481
}
468482
}

0 commit comments

Comments
 (0)