Skip to content

Commit ed5062a

Browse files
committed
fix(compiler-cli): add suggestion when pipe is missing (#62146)
Adds some suggestions for potential pipes to be imported when raising the missing pipe diagnostic. Fixes #62136. PR Close #62146
1 parent 02da44c commit ed5062a

2 files changed

Lines changed: 42 additions & 8 deletions

File tree

‎packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts‎

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ export interface OutOfBandDiagnosticRecorder {
6565
*
6666
* @param id the type-checking ID of the template which contains the unknown pipe.
6767
* @param ast the `BindingPipe` invocation of the pipe which could not be found.
68+
* @param isStandalone whether the host component is standalone.
6869
*/
69-
missingPipe(id: TypeCheckId, ast: BindingPipe): void;
70+
missingPipe(id: TypeCheckId, ast: BindingPipe, isStandalone: boolean): void;
7071

7172
/**
7273
* Reports usage of a pipe imported via `@Component.deferredImports` outside
@@ -238,13 +239,30 @@ export interface OutOfBandDiagnosticRecorder {
238239
}
239240

240241
export class OutOfBandDiagnosticRecorderImpl implements OutOfBandDiagnosticRecorder {
241-
private _diagnostics: TemplateDiagnostic[] = [];
242+
private readonly _diagnostics: TemplateDiagnostic[] = [];
242243

243244
/**
244245
* Tracks which `BindingPipe` nodes have already been recorded as invalid, so only one diagnostic
245246
* is ever produced per node.
246247
*/
247-
private recordedPipes = new Set<BindingPipe>();
248+
private readonly recordedPipes = new Set<BindingPipe>();
249+
250+
/** Common pipes that can be suggested to users. */
251+
private readonly pipeSuggestions = new Map<string, string>([
252+
['async', 'AsyncPipe'],
253+
['uppercase', 'UpperCasePipe'],
254+
['lowercase', 'LowerCasePipe'],
255+
['json', 'JsonPipe'],
256+
['slice', 'SlicePipe'],
257+
['number', 'DecimalPipe'],
258+
['percent', 'PercentPipe'],
259+
['titlecase', 'TitleCasePipe'],
260+
['currency', 'CurrencyPipe'],
261+
['date', 'DatePipe'],
262+
['i18nPlural', 'I18nPluralPipe'],
263+
['i18nSelect', 'I18nSelectPipe'],
264+
['keyvalue', 'KeyValuePipe'],
265+
]);
248266

249267
constructor(private resolver: TypeCheckSourceResolver) {}
250268

@@ -269,20 +287,36 @@ export class OutOfBandDiagnosticRecorderImpl implements OutOfBandDiagnosticRecor
269287
);
270288
}
271289

272-
missingPipe(id: TypeCheckId, ast: BindingPipe): void {
290+
missingPipe(id: TypeCheckId, ast: BindingPipe, isStandalone: boolean): void {
273291
if (this.recordedPipes.has(ast)) {
274292
return;
275293
}
276294

277-
const mapping = this.resolver.getTemplateSourceMapping(id);
278-
const errorMsg = `No pipe found with name '${ast.name}'.`;
279-
280295
const sourceSpan = this.resolver.toTemplateParseSourceSpan(id, ast.nameSpan);
281296
if (sourceSpan === null) {
282297
throw new Error(
283298
`Assertion failure: no SourceLocation found for usage of pipe '${ast.name}'.`,
284299
);
285300
}
301+
302+
const mapping = this.resolver.getTemplateSourceMapping(id);
303+
let errorMsg = `No pipe found with name '${ast.name}'.`;
304+
305+
if (this.pipeSuggestions.has(ast.name)) {
306+
const suggestedClassName = this.pipeSuggestions.get(ast.name)!;
307+
const suggestedImport = '@angular/common';
308+
309+
if (isStandalone) {
310+
errorMsg +=
311+
`\nTo fix this, import the "${suggestedClassName}" class from "${suggestedImport}"` +
312+
` and add it to the "imports" array of the component.`;
313+
} else {
314+
errorMsg +=
315+
`\nTo fix this, import the "${suggestedClassName}" class from "${suggestedImport}"` +
316+
` and add it to the "imports" array of the module declaring the component.`;
317+
}
318+
}
319+
286320
this._diagnostics.push(
287321
makeTemplateDiagnostic(
288322
id,

‎packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3163,7 +3163,7 @@ class TcbExpressionTranslator {
31633163
let pipe: ts.Expression | null;
31643164
if (pipeMeta === null) {
31653165
// No pipe by that name exists in scope. Record this as an error.
3166-
this.tcb.oobRecorder.missingPipe(this.tcb.id, ast);
3166+
this.tcb.oobRecorder.missingPipe(this.tcb.id, ast, this.tcb.hostIsStandalone);
31673167

31683168
// Use an 'any' value to at least allow the rest of the expression to be checked.
31693169
pipe = ANY_EXPRESSION;

0 commit comments

Comments
 (0)