Skip to content

Commit c60f3bb

Browse files
committed
fix(compiler-cli): display proper function in NG8117 message
The diagnostic was displaying `'Function in text interpolation should be invoked: ect Object]()` instead of `'Function in text interpolation should be invoked: firstName()'.
1 parent c66e9fe commit c60f3bb

File tree

2 files changed

+5
-21
lines changed

2 files changed

+5
-21
lines changed

packages/compiler-cli/src/ngtsc/typecheck/extended/checks/uninvoked_function_in_text_interpolation/index.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,7 @@ import ts from 'typescript';
1010
import {ErrorCode, ExtendedTemplateDiagnosticName} from '../../../../diagnostics';
1111
import {NgTemplateDiagnostic, SymbolKind} from '../../../api';
1212
import {TemplateCheckFactory, TemplateCheckWithVisitor, TemplateContext} from '../../api';
13-
import {
14-
AbsoluteSourceSpan,
15-
AST,
16-
Call,
17-
Interpolation,
18-
PropertyRead,
19-
SafeCall,
20-
SafePropertyRead,
21-
TmplAstNode,
22-
} from '@angular/compiler';
13+
import {AST, Interpolation, PropertyRead, SafePropertyRead, TmplAstNode} from '@angular/compiler';
2314

2415
class UninvokedFunctionInTextInterpolation extends TemplateCheckWithVisitor<ErrorCode.UNINVOKED_FUNCTION_IN_TEXT_INTERPOLATION> {
2516
override code = ErrorCode.UNINVOKED_FUNCTION_IN_TEXT_INTERPOLATION as const;
@@ -31,9 +22,7 @@ class UninvokedFunctionInTextInterpolation extends TemplateCheckWithVisitor<Erro
3122
): NgTemplateDiagnostic<ErrorCode.UNINVOKED_FUNCTION_IN_TEXT_INTERPOLATION>[] {
3223
// interpolations like `{{ myFunction }}`
3324
if (node instanceof Interpolation) {
34-
return node.expressions.flatMap((item) =>
35-
assertExpressionInvoked(item, component, node.sourceSpan, ctx),
36-
);
25+
return node.expressions.flatMap((item) => assertExpressionInvoked(item, component, ctx));
3726
}
3827
return [];
3928
}
@@ -42,7 +31,6 @@ class UninvokedFunctionInTextInterpolation extends TemplateCheckWithVisitor<Erro
4231
function assertExpressionInvoked(
4332
expression: AST,
4433
component: ts.ClassDeclaration,
45-
sourceSpan: AbsoluteSourceSpan,
4634
ctx: TemplateContext<ErrorCode.UNINVOKED_FUNCTION_IN_TEXT_INTERPOLATION>,
4735
): NgTemplateDiagnostic<ErrorCode.UNINVOKED_FUNCTION_IN_TEXT_INTERPOLATION>[] {
4836
if (!(expression instanceof PropertyRead) && !(expression instanceof SafePropertyRead)) {
@@ -53,9 +41,7 @@ function assertExpressionInvoked(
5341

5442
if (symbol !== null && symbol.kind === SymbolKind.Expression) {
5543
if (symbol.tsType.getCallSignatures()?.length > 0) {
56-
const fullExpressionText = generateStringFromExpression(expression, sourceSpan.toString());
57-
58-
const errorString = `Function in text interpolation should be invoked: ${fullExpressionText}()`;
44+
const errorString = `Function in text interpolation should be invoked: ${expression.name}()`;
5945
const templateMapping = ctx.templateTypeChecker.getSourceMappingAtTcbLocation(
6046
symbol.tcbLocation,
6147
)!;
@@ -66,10 +52,6 @@ function assertExpressionInvoked(
6652
return [];
6753
}
6854

69-
function generateStringFromExpression(expression: AST, source: string): string {
70-
return source.substring(expression.span.start, expression.span.end);
71-
}
72-
7355
export const factory: TemplateCheckFactory<
7456
ErrorCode.UNINVOKED_FUNCTION_IN_TEXT_INTERPOLATION,
7557
ExtendedTemplateDiagnosticName.UNINVOKED_FUNCTION_IN_TEXT_INTERPOLATION

packages/compiler-cli/src/ngtsc/typecheck/extended/test/checks/uninvoked_function_in_text_interpolation/uninvoked_function_in_text_interpolation_spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ runInEachFileSystem(() => {
8686
expect(diags.length).toBe(1);
8787
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
8888
expect(diags[0].code).toBe(ngErrorCode(ErrorCode.UNINVOKED_FUNCTION_IN_TEXT_INTERPOLATION));
89+
expect(diags[0].messageText).toContain('firstName()');
8990
expect(getSourceCodeForDiagnostic(diags[0])).toBe('firstName');
9091
});
9192

@@ -146,6 +147,7 @@ runInEachFileSystem(() => {
146147
expect(diags.length).toBe(1);
147148
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
148149
expect(diags[0].code).toBe(ngErrorCode(ErrorCode.UNINVOKED_FUNCTION_IN_TEXT_INTERPOLATION));
150+
expect(diags[0].messageText).toContain('firstName()');
149151
expect(getSourceCodeForDiagnostic(diags[0])).toBe('firstName');
150152
});
151153
});

0 commit comments

Comments
 (0)