|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 |
|
| 9 | +import { strict as assert } from 'assert'; |
9 | 10 | import * as fs from 'fs';
|
10 | 11 | import * as path from 'path';
|
11 | 12 |
|
@@ -52,42 +53,43 @@ type NgtscLogger = Parameters<
|
52 | 53 | >[0]['logger'];
|
53 | 54 |
|
54 | 55 | type I18nDiagnostics = import('@angular/localize/src/tools/src/diagnostics').Diagnostics;
|
| 56 | +type I18nDiagnosticsHandlingStrategy = |
| 57 | + import('@angular/localize/src/tools/src/diagnostics').DiagnosticHandlingStrategy; |
55 | 58 | 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 | + } |
63 | 72 |
|
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); |
69 | 75 | }
|
70 |
| - }; |
71 | 76 |
|
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 | + } |
77 | 80 |
|
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 | + } |
83 | 86 |
|
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 | + ); |
89 | 91 | }
|
90 |
| - }; |
| 92 | + })(); |
91 | 93 |
|
92 | 94 | return diagnostics;
|
93 | 95 | }
|
|
0 commit comments