forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(compiler-cli): effective narrowing of signal reads in templates
This commit is an experiment to see how the template type-check block can be modified to support effective narrowing of signal reads. The prototype exposes one problem in particular: calling a type guard method no longer narrows according to the type-guard, as the method call is indirected though an additional variable. Other problems with this approach is that diagnostics will not be reported for all usages, given that identical call expressions are deduplicated into a single call expression. The implementation of this prototype is not as efficient as it could be. In particular, computing the identifier of a call expression does not leverage a potentially existing identifier for another call expression in its LHS.
- Loading branch information
Showing
10 changed files
with
469 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
packages/compiler-cli/src/ngtsc/typecheck/src/signal_calls.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {AST, AstVisitor, ASTWithSource, Binary, BindingPipe, Call, Chain, Conditional, ImplicitReceiver, Interpolation, KeyedRead, KeyedWrite, LiteralArray, LiteralMap, LiteralPrimitive, NonNullAssert, PrefixNot, PropertyRead, PropertyWrite, SafeCall, SafeKeyedRead, SafePropertyRead, ThisReceiver, Unary,} from '@angular/compiler'; | ||
|
||
export type SignalCallIdentity = string&{__brand: 'SignalCallIdentity'}; | ||
|
||
export type PotentialSignalCall = Call|SafeCall; | ||
|
||
export function computeSignalCallIdentity( | ||
call: PotentialSignalCall, | ||
recurse: (receiverCall: PotentialSignalCall) => string | null, | ||
identifyImplicitRead: (expr: AST) => string | null, | ||
): SignalCallIdentity { | ||
const identifier = | ||
call.receiver.visit(new SignalCallIdentification(recurse, identifyImplicitRead)); | ||
return identifier as SignalCallIdentity; | ||
} | ||
|
||
class SignalCallIdentification implements AstVisitor { | ||
constructor( | ||
private recurse: (receiverCall: PotentialSignalCall) => string | null, | ||
private identifyImplicitRead: (expr: AST) => string | null, | ||
) {} | ||
|
||
visitUnary(ast: Unary): string|null { | ||
const expr = this.forAst(ast.expr); | ||
if (expr === null) { | ||
return null; | ||
} | ||
return `${ast.operator}${expr}`; | ||
} | ||
|
||
visitBinary(ast: Binary): string|null { | ||
const left = this.forAst(ast.left); | ||
const right = this.forAst(ast.right); | ||
if (left === null || right === null) { | ||
return null; | ||
} | ||
return `${left}${ast.operation}${right}`; | ||
} | ||
|
||
visitChain(ast: Chain): string|null { | ||
return null; | ||
} | ||
|
||
visitConditional(ast: Conditional): string|null { | ||
return null; | ||
} | ||
|
||
visitThisReceiver(ast: ThisReceiver): string|null { | ||
return 'this'; | ||
} | ||
|
||
visitImplicitReceiver(ast: ImplicitReceiver): string|null { | ||
return 'this'; | ||
} | ||
|
||
visitInterpolation(ast: Interpolation): string|null { | ||
return null; | ||
} | ||
|
||
visitKeyedRead(ast: KeyedRead): string|null { | ||
const receiver = this.forAst(ast.receiver); | ||
const key = this.forAst(ast.key); | ||
if (receiver === null || key === null) { | ||
return null; | ||
} | ||
return `${receiver}[${key}]`; | ||
} | ||
|
||
visitKeyedWrite(ast: KeyedWrite): string|null { | ||
return null; | ||
} | ||
|
||
visitLiteralArray(ast: LiteralArray): string|null { | ||
return null; | ||
} | ||
|
||
visitLiteralMap(ast: LiteralMap): string|null { | ||
return null; | ||
} | ||
|
||
visitLiteralPrimitive(ast: LiteralPrimitive): string|null { | ||
return `${ast.value}`; | ||
} | ||
|
||
visitPipe(ast: BindingPipe): string|null { | ||
return null; | ||
} | ||
|
||
visitPrefixNot(ast: PrefixNot): string|null { | ||
const expression = this.forAst(ast.expression); | ||
if (expression === null) { | ||
return expression; | ||
} | ||
return `!${expression}`; | ||
} | ||
|
||
visitNonNullAssert(ast: NonNullAssert): string|null { | ||
return this.forAst(ast.expression); | ||
} | ||
|
||
visitPropertyRead(ast: PropertyRead): string|null { | ||
const receiver = this.identifyReceiver(ast); | ||
if (receiver === null) { | ||
return null; | ||
} | ||
return `${receiver}.${ast.name}`; | ||
} | ||
|
||
visitPropertyWrite(ast: PropertyWrite): string|null { | ||
return null; | ||
} | ||
|
||
visitSafePropertyRead(ast: SafePropertyRead): string|null { | ||
const receiver = this.identifyReceiver(ast); | ||
if (receiver === null) { | ||
return null; | ||
} | ||
return `${receiver}?.${ast.name}`; | ||
} | ||
|
||
visitSafeKeyedRead(ast: SafeKeyedRead): string|null { | ||
const receiver = this.forAst(ast.receiver) | ||
if (receiver === null) { | ||
return null; | ||
} | ||
return `${receiver}?.[${this.forAst(ast.key)}]`; | ||
} | ||
|
||
visitCall(ast: Call): string|null { | ||
if (ast.args.length > 0) { | ||
return null; | ||
} | ||
const receiver = this.forAst(ast.receiver); | ||
if (receiver === null) { | ||
return null; | ||
} | ||
return `${receiver}()`; | ||
} | ||
|
||
visitSafeCall(ast: SafeCall): string|null { | ||
if (ast.args.length > 0) { | ||
return null; | ||
} | ||
const receiver = this.forAst(ast.receiver); | ||
if (receiver === null) { | ||
return null; | ||
} | ||
return `${receiver}?.()`; | ||
} | ||
|
||
visitASTWithSource(ast: ASTWithSource): string|null { | ||
return this.forAst(ast.ast); | ||
} | ||
|
||
private identifyReceiver(ast: PropertyRead|SafePropertyRead): string|null { | ||
if (ast.receiver instanceof ImplicitReceiver && !(ast.receiver instanceof ThisReceiver)) { | ||
const implicitIdentity = this.identifyImplicitRead(ast); | ||
if (implicitIdentity !== null) { | ||
return implicitIdentity; | ||
} | ||
} | ||
return this.forAst(ast.receiver); | ||
} | ||
|
||
private forAst(ast: AST): string|null { | ||
if (ast instanceof Call || ast instanceof SafeCall) { | ||
const result = this.recurse(ast); | ||
if (result !== null) { | ||
return `ɵ${result}`; | ||
} | ||
} | ||
return ast.visit(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.