Skip to content

Commit 9a4288c

Browse files
committed
refactor: use chainable asError/asWarning/asSuggestion for Reporter severity
1 parent f720fdc commit 9a4288c

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

packages/core/index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ export function createLinter(
139139
rule2Mode.set(currentRuleId, true);
140140
shouldRetry = true;
141141
} else if (err instanceof Error) {
142-
report(err.stack ?? err.message, 0, 0, ts.DiagnosticCategory.Message, [err, 0]);
142+
report(err.stack ?? err.message, 0, 0, [err, 0]);
143143
} else {
144-
report(String(err), 0, 0, ts.DiagnosticCategory.Message, [new Error(), Number.MAX_VALUE]);
144+
report(String(err), 0, 0, [new Error(), Number.MAX_VALUE]);
145145
}
146146
}
147147

@@ -201,9 +201,9 @@ export function createLinter(
201201

202202
return diagnostics;
203203

204-
function report(message: string, start: number, end: number, category: ts.DiagnosticCategory = ts.DiagnosticCategory.Message, reportAt: [Error, number] = [new Error(), 1]): Reporter {
204+
function report(message: string, start: number, end: number, reportAt: [Error, number] = [new Error(), 1]): Reporter {
205205
const error: ts.DiagnosticWithLocation = {
206-
category,
206+
category: ts.DiagnosticCategory.Message,
207207
code: currentRuleId as any,
208208
messageText: message,
209209
file: rulesContext.file,
@@ -237,6 +237,18 @@ export function createLinter(
237237
const fixes = diagnostic2Fixes.get(error)!;
238238

239239
return {
240+
asWarning() {
241+
error.category = ts.DiagnosticCategory.Warning;
242+
return this;
243+
},
244+
asError() {
245+
error.category = ts.DiagnosticCategory.Error;
246+
return this;
247+
},
248+
asSuggestion() {
249+
error.category = ts.DiagnosticCategory.Suggestion;
250+
return this;
251+
},
240252
withDeprecated() {
241253
error.reportsDeprecated = true;
242254
return this;

packages/eslint/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,16 @@ export function convertRule(
249249
}
250250
}
251251
} catch { }
252-
const reporter = report(message, start, end, category, [new Error(), 1]);
252+
const reporter = report(message, start, end, [new Error(), 1]);
253+
if (category === 0 satisfies ts.DiagnosticCategory.Warning) {
254+
reporter.asWarning();
255+
}
256+
else if (category === 1 satisfies ts.DiagnosticCategory.Error) {
257+
reporter.asError();
258+
}
259+
else if (category === 2 satisfies ts.DiagnosticCategory.Suggestion) {
260+
reporter.asSuggestion();
261+
}
253262
if (descriptor.fix) {
254263
// @ts-expect-error
255264
const textChanges = getTextChanges(descriptor.fix);

packages/tslint/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ export function convertRule(
2525
failure.getFailure(),
2626
failure.getStartPosition().getPosition(),
2727
failure.getEndPosition().getPosition(),
28-
category,
2928
[new Error(), Number.MAX_VALUE]
3029
);
30+
if (category === 0 satisfies ts.DiagnosticCategory.Warning) {
31+
reporter.asWarning();
32+
}
33+
else if (category === 1 satisfies ts.DiagnosticCategory.Error) {
34+
reporter.asError();
35+
}
36+
else if (category === 2 satisfies ts.DiagnosticCategory.Suggestion) {
37+
reporter.asSuggestion();
38+
}
3139
if (failure.hasFix()) {
3240
const ruleName = Rule.metadata?.ruleName;
3341
reporter.withFix(

packages/types/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type {
22
CodeFixAction,
33
Diagnostic,
4-
DiagnosticCategory,
54
DiagnosticWithLocation,
65
FileTextChanges,
76
LanguageService,
@@ -45,10 +44,13 @@ export interface RuleContext {
4544
typescript: typeof import('typescript');
4645
program: Program;
4746
file: SourceFile;
48-
report(message: string, start: number, end: number, category?: DiagnosticCategory, reportAt?: [Error, number]): Reporter;
47+
report(message: string, start: number, end: number, reportAt?: [Error, number]): Reporter;
4948
}
5049

5150
export interface Reporter {
51+
asWarning(): Reporter;
52+
asError(): Reporter;
53+
asSuggestion(): Reporter;
5254
withDeprecated(): Reporter;
5355
withUnnecessary(): Reporter;
5456
withFix(title: string, getChanges: () => FileTextChanges[]): Reporter;

0 commit comments

Comments
 (0)