|
| 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 | + AstVisitor, |
| 12 | + ASTWithSource, |
| 13 | + Binary, |
| 14 | + BindingPipe, |
| 15 | + Call, |
| 16 | + Chain, |
| 17 | + Conditional, |
| 18 | + ImplicitReceiver, |
| 19 | + Interpolation, |
| 20 | + KeyedRead, |
| 21 | + KeyedWrite, |
| 22 | + LiteralArray, |
| 23 | + LiteralMap, |
| 24 | + LiteralPrimitive, |
| 25 | + NonNullAssert, |
| 26 | + PrefixNot, |
| 27 | + PropertyRead, |
| 28 | + PropertyWrite, |
| 29 | + SafeCall, |
| 30 | + SafeKeyedRead, |
| 31 | + SafePropertyRead, |
| 32 | + ThisReceiver, |
| 33 | + Unary, |
| 34 | +} from '@angular/compiler'; |
| 35 | + |
| 36 | +/** |
| 37 | + * The types of nodes potentially corresponding with a signal call in a template. Any call |
| 38 | + * expression without arguments is potentially a signal call, as signal calls cannot be |
| 39 | + * differentiated from regular method calls syntactically. |
| 40 | + */ |
| 41 | +export type PotentialSignalCall = (Call | SafeCall) & {args: []}; |
| 42 | + |
| 43 | +/** |
| 44 | + * Tests whether the given call expression may correspond with a signal call. |
| 45 | + */ |
| 46 | +export function isPotentialSignalCall(ast: Call | SafeCall): ast is PotentialSignalCall { |
| 47 | + return ast.args.length === 0; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Represents a string that identifies a call expression that may represent a signal call. |
| 52 | + */ |
| 53 | +export type SignalCallIdentity = string & {__brand: 'SignalCallIdentity'}; |
| 54 | + |
| 55 | +/** |
| 56 | + * Computes the string identity of the provided call expression. Any expression that is witnessed |
| 57 | + * when evaluating the call expression is included in the identity. For implicit reads (e.g. |
| 58 | + * property accesses with `ImplicitReceiver` as receiver) an external identity is requested, to |
| 59 | + * ensure that each local template variable is assigned its own unique identifier. This is important |
| 60 | + * for scenarios like |
| 61 | + * |
| 62 | + * ``` |
| 63 | + * @Component({ |
| 64 | + * template: ` |
| 65 | + * @if (persons[selectedPerson](); as selectedPerson) { |
| 66 | + * {{ persons[selectedPerson]() }} |
| 67 | + * }` |
| 68 | + * }) |
| 69 | + * export class ShadowCmp { |
| 70 | + * persons = { |
| 71 | + * admin: signal('admin'); |
| 72 | + * guest: signal('guest'); |
| 73 | + * }; |
| 74 | + * selectedPerson: keyof ShadowCmp['persons']; |
| 75 | + * } |
| 76 | + * ``` |
| 77 | + * |
| 78 | + * Here, the local alias `selectedPerson` shadows the `ShadowCmp.selectedPerson` field, such that |
| 79 | + * the call expression in the @if's condition expression is not equivalent to the syntactically |
| 80 | + * identical expression within its block. Consequently, different identities have to be computed for |
| 81 | + * both call expressions, which is achieved by letting `identifyImplicitRead` resolve any implicit |
| 82 | + * accesses to a unique identifier for local template variables. |
| 83 | + * |
| 84 | + * @param call The call expression to compute the identity for. |
| 85 | + * @param recurse Callback function to compute the identity of nested signal calls. |
| 86 | + * @param identifyImplicitRead Callback function to determine the identity of implicit reads. |
| 87 | + */ |
| 88 | +export function computeSignalCallIdentity( |
| 89 | + call: PotentialSignalCall, |
| 90 | + recurse: (receiverCall: PotentialSignalCall) => string | null, |
| 91 | + identifyImplicitRead: (expr: AST) => string | null, |
| 92 | +): SignalCallIdentity | null { |
| 93 | + return call.receiver.visit(new SignalCallIdentification(recurse, identifyImplicitRead)); |
| 94 | +} |
| 95 | + |
| 96 | +class SignalCallIdentification implements AstVisitor { |
| 97 | + constructor( |
| 98 | + private recurse: (receiverCall: PotentialSignalCall) => string | null, |
| 99 | + private identifyImplicitRead: (expr: AST) => string | null, |
| 100 | + ) {} |
| 101 | + |
| 102 | + visitUnary(ast: Unary): string | null { |
| 103 | + const expr = this.forAst(ast.expr); |
| 104 | + if (expr === null) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + return `${ast.operator}${expr}`; |
| 108 | + } |
| 109 | + |
| 110 | + visitBinary(ast: Binary): string | null { |
| 111 | + const left = this.forAst(ast.left); |
| 112 | + const right = this.forAst(ast.right); |
| 113 | + if (left === null || right === null) { |
| 114 | + return null; |
| 115 | + } |
| 116 | + return `${left}${ast.operation}${right}`; |
| 117 | + } |
| 118 | + |
| 119 | + visitChain(ast: Chain): string | null { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + visitConditional(ast: Conditional): string | null { |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + visitThisReceiver(ast: ThisReceiver): string | null { |
| 128 | + return 'this'; |
| 129 | + } |
| 130 | + |
| 131 | + visitImplicitReceiver(ast: ImplicitReceiver): string | null { |
| 132 | + return 'this'; |
| 133 | + } |
| 134 | + |
| 135 | + visitInterpolation(ast: Interpolation): string | null { |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + visitKeyedRead(ast: KeyedRead): string | null { |
| 140 | + const receiver = this.forAst(ast.receiver); |
| 141 | + const key = this.forAst(ast.key); |
| 142 | + if (receiver === null || key === null) { |
| 143 | + return null; |
| 144 | + } |
| 145 | + return `${receiver}[${key}]`; |
| 146 | + } |
| 147 | + |
| 148 | + visitKeyedWrite(ast: KeyedWrite): string | null { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + visitLiteralArray(ast: LiteralArray): string | null { |
| 153 | + return null; |
| 154 | + } |
| 155 | + |
| 156 | + visitLiteralMap(ast: LiteralMap): string | null { |
| 157 | + return null; |
| 158 | + } |
| 159 | + |
| 160 | + visitLiteralPrimitive(ast: LiteralPrimitive): string | null { |
| 161 | + return `${ast.value}`; |
| 162 | + } |
| 163 | + |
| 164 | + visitPipe(ast: BindingPipe): string | null { |
| 165 | + // Don't enable narrowing when pipes are being evaluated. |
| 166 | + return null; |
| 167 | + } |
| 168 | + |
| 169 | + visitPrefixNot(ast: PrefixNot): string | null { |
| 170 | + const expression = this.forAst(ast.expression); |
| 171 | + if (expression === null) { |
| 172 | + return expression; |
| 173 | + } |
| 174 | + return `!${expression}`; |
| 175 | + } |
| 176 | + |
| 177 | + visitNonNullAssert(ast: NonNullAssert): string | null { |
| 178 | + return this.forAst(ast.expression); |
| 179 | + } |
| 180 | + |
| 181 | + visitPropertyRead(ast: PropertyRead): string | null { |
| 182 | + const receiver = this.identifyReceiver(ast); |
| 183 | + if (receiver === null) { |
| 184 | + return null; |
| 185 | + } |
| 186 | + return `${receiver}.${ast.name}`; |
| 187 | + } |
| 188 | + |
| 189 | + visitPropertyWrite(ast: PropertyWrite): string | null { |
| 190 | + return null; |
| 191 | + } |
| 192 | + |
| 193 | + visitSafePropertyRead(ast: SafePropertyRead): string | null { |
| 194 | + const receiver = this.identifyReceiver(ast); |
| 195 | + if (receiver === null) { |
| 196 | + return null; |
| 197 | + } |
| 198 | + return `${receiver}?.${ast.name}`; |
| 199 | + } |
| 200 | + |
| 201 | + visitSafeKeyedRead(ast: SafeKeyedRead): string | null { |
| 202 | + const receiver = this.forAst(ast.receiver); |
| 203 | + if (receiver === null) { |
| 204 | + return null; |
| 205 | + } |
| 206 | + return `${receiver}?.[${this.forAst(ast.key)}]`; |
| 207 | + } |
| 208 | + |
| 209 | + visitCall(ast: Call): string | null { |
| 210 | + if (ast.args.length > 0) { |
| 211 | + // Don't enable narrowing for complex calls. |
| 212 | + return null; |
| 213 | + } |
| 214 | + const receiver = this.forAst(ast.receiver); |
| 215 | + if (receiver === null) { |
| 216 | + return null; |
| 217 | + } |
| 218 | + return `${receiver}()`; |
| 219 | + } |
| 220 | + |
| 221 | + visitSafeCall(ast: SafeCall): string | null { |
| 222 | + if (ast.args.length > 0) { |
| 223 | + // Don't enable narrowing for complex calls. |
| 224 | + return null; |
| 225 | + } |
| 226 | + const receiver = this.forAst(ast.receiver); |
| 227 | + if (receiver === null) { |
| 228 | + return null; |
| 229 | + } |
| 230 | + return `${receiver}?.()`; |
| 231 | + } |
| 232 | + |
| 233 | + visitASTWithSource(ast: ASTWithSource): string | null { |
| 234 | + return this.forAst(ast.ast); |
| 235 | + } |
| 236 | + |
| 237 | + private identifyReceiver(ast: PropertyRead | SafePropertyRead): string | null { |
| 238 | + const receiver = ast.receiver; |
| 239 | + if (receiver instanceof ImplicitReceiver && !(receiver instanceof ThisReceiver)) { |
| 240 | + const implicitIdentity = this.identifyImplicitRead(ast); |
| 241 | + if (implicitIdentity !== null) { |
| 242 | + return implicitIdentity; |
| 243 | + } |
| 244 | + } |
| 245 | + return this.forAst(receiver); |
| 246 | + } |
| 247 | + |
| 248 | + private forAst(ast: AST): string | null { |
| 249 | + if ((ast instanceof Call || ast instanceof SafeCall) && isPotentialSignalCall(ast)) { |
| 250 | + const result = this.recurse(ast); |
| 251 | + if (result !== null) { |
| 252 | + return `ɵ${result}`; |
| 253 | + } |
| 254 | + } |
| 255 | + return ast.visit(this); |
| 256 | + } |
| 257 | +} |
0 commit comments