|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + AST, |
| 11 | + ASTWithSource, |
| 12 | + Call, |
| 13 | + Chain, |
| 14 | + Conditional, |
| 15 | + ParsedEventType, |
| 16 | + PropertyRead, |
| 17 | + SafeCall, |
| 18 | + SafePropertyRead, |
| 19 | + TmplAstBoundEvent, |
| 20 | + TmplAstNode, |
| 21 | +} from '@angular/compiler'; |
| 22 | +import ts from 'typescript'; |
| 23 | + |
| 24 | +import {ErrorCode, ExtendedTemplateDiagnosticName} from '../../../../diagnostics'; |
| 25 | +import {NgTemplateDiagnostic, SymbolKind} from '../../../api'; |
| 26 | +import {TemplateCheckFactory, TemplateCheckWithVisitor, TemplateContext} from '../../api'; |
| 27 | + |
| 28 | +/** |
| 29 | + * Ensures that function in event bindings are called. For example, `<button (click)="myFunc"></button>` |
| 30 | + * will not call `myFunc` when the button is clicked. Instead, it should be `<button (click)="myFunc()"></button>`. |
| 31 | + * This is likely not the intent of the developer. Instead, the intent is likely to call `myFunc`. |
| 32 | + */ |
| 33 | +class UninvokedFunctionInEventBindingSpec extends TemplateCheckWithVisitor<ErrorCode.UNINVOKED_FUNCTION_IN_EVENT_BINDING> { |
| 34 | + override code = ErrorCode.UNINVOKED_FUNCTION_IN_EVENT_BINDING as const; |
| 35 | + |
| 36 | + override visitNode( |
| 37 | + ctx: TemplateContext<ErrorCode.UNINVOKED_FUNCTION_IN_EVENT_BINDING>, |
| 38 | + component: ts.ClassDeclaration, |
| 39 | + node: TmplAstNode | AST, |
| 40 | + ): NgTemplateDiagnostic<ErrorCode.UNINVOKED_FUNCTION_IN_EVENT_BINDING>[] { |
| 41 | + // If the node is not a bound event, skip it. |
| 42 | + if (!(node instanceof TmplAstBoundEvent)) return []; |
| 43 | + |
| 44 | + // If the node is not a regular or animation event, skip it. |
| 45 | + if (node.type !== ParsedEventType.Regular && node.type !== ParsedEventType.Animation) return []; |
| 46 | + |
| 47 | + if (!(node.handler instanceof ASTWithSource)) return []; |
| 48 | + |
| 49 | + const sourceExpressionText = node.handler.source || ''; |
| 50 | + |
| 51 | + if (node.handler.ast instanceof Chain) { |
| 52 | + // (click)="increment; decrement" |
| 53 | + return node.handler.ast.expressions.flatMap((expression) => |
| 54 | + assertExpressionInvoked(expression, component, node, sourceExpressionText, ctx), |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + if (node.handler.ast instanceof Conditional) { |
| 59 | + // (click)="true ? increment : decrement" |
| 60 | + const {trueExp, falseExp} = node.handler.ast; |
| 61 | + return [trueExp, falseExp].flatMap((expression) => |
| 62 | + assertExpressionInvoked(expression, component, node, sourceExpressionText, ctx), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + // (click)="increment" |
| 67 | + return assertExpressionInvoked(node.handler.ast, component, node, sourceExpressionText, ctx); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Asserts that the expression is invoked. |
| 73 | + * If the expression is a property read, and it has a call signature, a diagnostic is generated. |
| 74 | + */ |
| 75 | +function assertExpressionInvoked( |
| 76 | + expression: AST, |
| 77 | + component: ts.ClassDeclaration, |
| 78 | + node: TmplAstBoundEvent, |
| 79 | + expressionText: string, |
| 80 | + ctx: TemplateContext<ErrorCode.UNINVOKED_FUNCTION_IN_EVENT_BINDING>, |
| 81 | +): NgTemplateDiagnostic<ErrorCode.UNINVOKED_FUNCTION_IN_EVENT_BINDING>[] { |
| 82 | + if (expression instanceof Call || expression instanceof SafeCall) { |
| 83 | + return []; // If the method is called, skip it. |
| 84 | + } |
| 85 | + |
| 86 | + if (!(expression instanceof PropertyRead) && !(expression instanceof SafePropertyRead)) { |
| 87 | + return []; // If the expression is not a property read, skip it. |
| 88 | + } |
| 89 | + |
| 90 | + const symbol = ctx.templateTypeChecker.getSymbolOfNode(expression, component); |
| 91 | + |
| 92 | + if (symbol !== null && symbol.kind === SymbolKind.Expression) { |
| 93 | + if (symbol.tsType.getCallSignatures()?.length > 0) { |
| 94 | + const fullExpressionText = generateStringFromExpression(expression, expressionText); |
| 95 | + const errorString = `Function in event binding should be invoked: ${fullExpressionText}()`; |
| 96 | + return [ctx.makeTemplateDiagnostic(node.sourceSpan, errorString)]; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return []; |
| 101 | +} |
| 102 | + |
| 103 | +function generateStringFromExpression(expression: AST, source: string): string { |
| 104 | + return source.substring(expression.span.start, expression.span.end); |
| 105 | +} |
| 106 | + |
| 107 | +export const factory: TemplateCheckFactory< |
| 108 | + ErrorCode.UNINVOKED_FUNCTION_IN_EVENT_BINDING, |
| 109 | + ExtendedTemplateDiagnosticName.UNINVOKED_FUNCTION_IN_EVENT_BINDING |
| 110 | +> = { |
| 111 | + code: ErrorCode.UNINVOKED_FUNCTION_IN_EVENT_BINDING, |
| 112 | + name: ExtendedTemplateDiagnosticName.UNINVOKED_FUNCTION_IN_EVENT_BINDING, |
| 113 | + create: () => new UninvokedFunctionInEventBindingSpec(), |
| 114 | +}; |
0 commit comments