Skip to content

Commit 043c410

Browse files
committed
refactor(@angular-devkit/build-angular): remove patching of @angular/localize Diagnostics object
A Babel webpack loader specific `@angular/localize` package `Diagnostics` object is now created that is optimized for the diagnostics reporting case. This removes the need to manually create and patch an instance of a Diagnostics object. Future work will potentially allow a reporter to be passed directly to the translation plugins to remove the need to create a new object entirely.
1 parent 47bf88e commit 043c410

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

packages/angular_devkit/build_angular/src/babel/presets/application.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import { strict as assert } from 'assert';
910
import * as fs from 'fs';
1011
import * as path from 'path';
1112

@@ -52,42 +53,43 @@ type NgtscLogger = Parameters<
5253
>[0]['logger'];
5354

5455
type I18nDiagnostics = import('@angular/localize/src/tools/src/diagnostics').Diagnostics;
56+
type I18nDiagnosticsHandlingStrategy =
57+
import('@angular/localize/src/tools/src/diagnostics').DiagnosticHandlingStrategy;
5558
function createI18nDiagnostics(reporter: DiagnosticReporter | undefined): I18nDiagnostics {
56-
// Babel currently is synchronous so import cannot be used
57-
const diagnostics: I18nDiagnostics =
58-
new (require('@angular/localize/src/tools/src/diagnostics').Diagnostics)();
59-
60-
if (!reporter) {
61-
return diagnostics;
62-
}
59+
const diagnostics: I18nDiagnostics = new (class {
60+
readonly messages: I18nDiagnostics['messages'] = [];
61+
hasErrors = false;
62+
63+
add(type: I18nDiagnosticsHandlingStrategy, message: string): void {
64+
if (type === 'ignore') {
65+
return;
66+
}
67+
68+
this.messages.push({ type, message });
69+
this.hasErrors ||= type === 'error';
70+
reporter?.(type, message);
71+
}
6372

64-
const baseAdd = diagnostics.add;
65-
diagnostics.add = function (type, message, ...args) {
66-
if (type !== 'ignore') {
67-
baseAdd.call(diagnostics, type, message, ...args);
68-
reporter(type, message);
73+
error(message: string): void {
74+
this.add('error', message);
6975
}
70-
};
7176

72-
const baseError = diagnostics.error;
73-
diagnostics.error = function (message, ...args) {
74-
baseError.call(diagnostics, message, ...args);
75-
reporter('error', message);
76-
};
77+
warn(message: string): void {
78+
this.add('warning', message);
79+
}
7780

78-
const baseWarn = diagnostics.warn;
79-
diagnostics.warn = function (message, ...args) {
80-
baseWarn.call(diagnostics, message, ...args);
81-
reporter('warning', message);
82-
};
81+
merge(other: I18nDiagnostics): void {
82+
for (const diagnostic of other.messages) {
83+
this.add(diagnostic.type, diagnostic.message);
84+
}
85+
}
8386

84-
const baseMerge = diagnostics.merge;
85-
diagnostics.merge = function (other, ...args) {
86-
baseMerge.call(diagnostics, other, ...args);
87-
for (const diagnostic of other.messages) {
88-
reporter(diagnostic.type, diagnostic.message);
87+
formatDiagnostics(): never {
88+
assert.fail(
89+
'@angular/localize Diagnostics formatDiagnostics should not be called from within babel.',
90+
);
8991
}
90-
};
92+
})();
9193

9294
return diagnostics;
9395
}

0 commit comments

Comments
 (0)