Skip to content

Commit 067362e

Browse files
filipesilvadgp1130
authored andcommitted
fix(@ngtools/webpack): report diagnostics the same way in type checker
(cherry picked from commit b9667b3)
1 parent 7338735 commit 067362e

File tree

4 files changed

+78
-73
lines changed

4 files changed

+78
-73
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
createCompilerHost,
2929
createProgram,
3030
formatDiagnostics,
31-
isNgDiagnostic,
3231
readConfiguration,
3332
} from '@angular/compiler-cli';
3433
import { ChildProcess, ForkOptions, fork } from 'child_process';
@@ -38,8 +37,8 @@ import * as ts from 'typescript';
3837
import { Compiler, compilation } from 'webpack';
3938
import { time, timeEnd } from './benchmark';
4039
import { WebpackCompilerHost } from './compiler_host';
40+
import { DiagnosticMode, gatherDiagnostics, hasErrors, reportDiagnostics } from './diagnostics';
4141
import { resolveEntryModuleFromMain } from './entry_resolver';
42-
import { DiagnosticMode, gatherDiagnostics, hasErrors } from './gather_diagnostics';
4342
import {
4443
AngularCompilerPluginOptions,
4544
ContextElementDependencyConstructor,
@@ -1097,59 +1096,13 @@ export class AngularCompilerPlugin {
10971096
const { emitResult, diagnostics } = this._emit();
10981097
timeEnd('AngularCompilerPlugin._update._emit');
10991098

1100-
// Report Diagnostics
1101-
const tsErrors = [];
1102-
const tsWarnings = [];
1103-
const ngErrors = [];
1104-
const ngWarnings = [];
1105-
1106-
for (const diagnostic of diagnostics) {
1107-
switch (diagnostic.category) {
1108-
case ts.DiagnosticCategory.Error:
1109-
if (isNgDiagnostic(diagnostic)) {
1110-
ngErrors.push(diagnostic);
1111-
} else {
1112-
tsErrors.push(diagnostic);
1113-
}
1114-
break;
1115-
case ts.DiagnosticCategory.Message:
1116-
case ts.DiagnosticCategory.Suggestion:
1117-
// Warnings?
1118-
case ts.DiagnosticCategory.Warning:
1119-
if (isNgDiagnostic(diagnostic)) {
1120-
ngWarnings.push(diagnostic);
1121-
} else {
1122-
tsWarnings.push(diagnostic);
1123-
}
1124-
break;
1125-
}
1126-
}
1127-
1128-
if (tsErrors.length > 0) {
1129-
const message = ts.formatDiagnosticsWithColorAndContext(
1130-
tsErrors,
1131-
this._compilerHost,
1132-
);
1133-
this._errors.push(new Error(message));
1134-
}
1135-
1136-
if (tsWarnings.length > 0) {
1137-
const message = ts.formatDiagnosticsWithColorAndContext(
1138-
tsWarnings,
1139-
this._compilerHost,
1140-
);
1141-
this._warnings.push(message);
1142-
}
1143-
1144-
if (ngErrors.length > 0) {
1145-
const message = formatDiagnostics(ngErrors);
1146-
this._errors.push(new Error(message));
1147-
}
1148-
1149-
if (ngWarnings.length > 0) {
1150-
const message = formatDiagnostics(ngWarnings);
1151-
this._warnings.push(message);
1152-
}
1099+
// Report any diagnostics.
1100+
reportDiagnostics(
1101+
diagnostics,
1102+
this._compilerHost,
1103+
msg => this._errors.push(new Error(msg)),
1104+
msg => this._warnings.push(msg),
1105+
);
11531106

11541107
this._emitSkipped = !emitResult || emitResult.emitSkipped;
11551108

packages/ngtools/webpack/src/gather_diagnostics.ts renamed to packages/ngtools/webpack/src/diagnostics.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { Diagnostic, Diagnostics, Program } from '@angular/compiler-cli';
8+
import {
9+
CompilerHost, Diagnostic, Diagnostics,
10+
Program, formatDiagnostics, isNgDiagnostic,
11+
} from '@angular/compiler-cli';
912
import * as ts from 'typescript';
1013
import { time, timeEnd } from './benchmark';
14+
import { WebpackCompilerHost } from './compiler_host';
1115

1216
export enum DiagnosticMode {
1317
Syntactic = 1 << 0,
@@ -102,3 +106,57 @@ export function gatherDiagnostics(
102106

103107
return allDiagnostics;
104108
}
109+
110+
export function reportDiagnostics(
111+
diagnostics: Diagnostics,
112+
compilerHost: WebpackCompilerHost & CompilerHost,
113+
reportError: (msg: string) => void,
114+
reportWarning: (msg: string) => void,
115+
) {
116+
const tsErrors = [];
117+
const tsWarnings = [];
118+
const ngErrors = [];
119+
const ngWarnings = [];
120+
121+
for (const diagnostic of diagnostics) {
122+
switch (diagnostic.category) {
123+
case ts.DiagnosticCategory.Error:
124+
if (isNgDiagnostic(diagnostic)) {
125+
ngErrors.push(diagnostic);
126+
} else {
127+
tsErrors.push(diagnostic);
128+
}
129+
break;
130+
case ts.DiagnosticCategory.Message:
131+
case ts.DiagnosticCategory.Suggestion:
132+
// Warnings?
133+
case ts.DiagnosticCategory.Warning:
134+
if (isNgDiagnostic(diagnostic)) {
135+
ngWarnings.push(diagnostic);
136+
} else {
137+
tsWarnings.push(diagnostic);
138+
}
139+
break;
140+
}
141+
}
142+
143+
if (tsErrors.length > 0) {
144+
const message = ts.formatDiagnosticsWithColorAndContext(tsErrors, compilerHost);
145+
reportError(message);
146+
}
147+
148+
if (tsWarnings.length > 0) {
149+
const message = ts.formatDiagnosticsWithColorAndContext(tsWarnings, compilerHost);
150+
reportWarning(message);
151+
}
152+
153+
if (ngErrors.length > 0) {
154+
const message = formatDiagnostics(ngErrors);
155+
reportError(message);
156+
}
157+
158+
if (ngWarnings.length > 0) {
159+
const message = formatDiagnostics(ngWarnings);
160+
reportWarning(message);
161+
}
162+
}

packages/ngtools/webpack/src/type_checker.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import {
1818
import * as ts from 'typescript';
1919
import { time, timeEnd } from './benchmark';
2020
import { WebpackCompilerHost } from './compiler_host';
21-
import { CancellationToken, DiagnosticMode, gatherDiagnostics } from './gather_diagnostics';
21+
import {
22+
CancellationToken, DiagnosticMode,
23+
gatherDiagnostics, reportDiagnostics,
24+
} from './diagnostics';
2225
import { LogMessage, TypeCheckerMessage } from './type_checker_messages';
2326

2427

@@ -106,21 +109,12 @@ export class TypeChecker {
106109

107110
// Report diagnostics.
108111
if (!cancellationToken.isCancellationRequested()) {
109-
const errors = allDiagnostics.filter((d) => d.category === ts.DiagnosticCategory.Error);
110-
const warnings = allDiagnostics.filter((d) => d.category === ts.DiagnosticCategory.Warning);
111-
112-
if (errors.length > 0) {
113-
const message = formatDiagnostics(errors);
114-
this.sendMessage(new LogMessage('error', 'ERROR in ' + message));
115-
} else {
116-
// Reset the changed file tracker only if there are no errors.
117-
this._compilerHost.resetChangedFileTracker();
118-
}
119-
120-
if (warnings.length > 0) {
121-
const message = formatDiagnostics(warnings);
122-
this.sendMessage(new LogMessage('warn', 'WARNING in ' + message));
123-
}
112+
reportDiagnostics(
113+
allDiagnostics,
114+
this._compilerHost,
115+
msg => this.sendMessage(new LogMessage('error', 'ERROR in ' + msg)),
116+
msg => this.sendMessage(new LogMessage('warn', 'WARNING in ' + msg)),
117+
);
124118
}
125119
}
126120

packages/ngtools/webpack/src/type_checker_worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import * as process from 'process';
99
import { time, timeEnd } from './benchmark';
10-
import { CancellationToken } from './gather_diagnostics';
10+
import { CancellationToken } from './diagnostics';
1111
import {
1212
AUTO_START_ARG,
1313
TypeChecker,

0 commit comments

Comments
 (0)