-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathdiagnostics.ts
36 lines (31 loc) · 1.13 KB
/
diagnostics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { Diagnostic, DiagnosticCategory } from 'typescript';
import type { Compilation } from 'webpack';
export type DiagnosticsReporter = (diagnostics: readonly Diagnostic[]) => void;
export function createDiagnosticsReporter(
compilation: Compilation,
formatter: (diagnostic: Diagnostic) => string,
): DiagnosticsReporter {
return (diagnostics) => {
for (const diagnostic of diagnostics) {
const text = formatter(diagnostic);
if (diagnostic.category === DiagnosticCategory.Error) {
addError(compilation, text);
} else {
addWarning(compilation, text);
}
}
};
}
export function addWarning(compilation: Compilation, message: string): void {
compilation.warnings.push(new compilation.compiler.webpack.WebpackError(message));
}
export function addError(compilation: Compilation, message: string): void {
compilation.errors.push(new compilation.compiler.webpack.WebpackError(message));
}