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
5 changed files
with
204 additions
and
31 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
110 changes: 110 additions & 0 deletions
110
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,110 @@ | ||
/** | ||
* @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 {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 SignalCallIdentifier = string&{__brand: 'SignalCallIdentifier'}; | ||
|
||
export function computeSignalCallIdentifier(call: Call): SignalCallIdentifier { | ||
const identifier = call.receiver.visit(new SignalCallIdentification()); | ||
return identifier as SignalCallIdentifier; | ||
} | ||
|
||
class SignalCallIdentification implements AstVisitor { | ||
visitUnary(ast: Unary): string { | ||
return `${ast.operator}${ast.expr.visit(this)}`; | ||
} | ||
|
||
visitBinary(ast: Binary): string { | ||
return `${ast.left.visit(this)}${ast.operation}${ast.right.visit(this)}`; | ||
} | ||
|
||
visitChain(ast: Chain): string { | ||
return ast.expressions.map(expr => expr.visit(this)).join(','); | ||
} | ||
|
||
visitConditional(ast: Conditional): string{return `(${ast.condition.visit(this)} ? ${ | ||
ast.trueExp.visit(this)} : ${ast.falseExp.visit(this)})`} | ||
|
||
visitThisReceiver(ast: ThisReceiver): string { | ||
return 'this'; | ||
} | ||
|
||
visitImplicitReceiver(ast: ImplicitReceiver): string { | ||
return 'this'; | ||
} | ||
|
||
visitInterpolation(ast: Interpolation): string { | ||
return ast.expressions.map(expr => expr.visit(this)).join(','); | ||
} | ||
|
||
visitKeyedRead(ast: KeyedRead): string { | ||
return `${ast.receiver.visit(this)}[${ast.key.visit(this)}]`; | ||
} | ||
|
||
visitKeyedWrite(ast: KeyedWrite): string { | ||
return `${ast.receiver.visit(this)}[${ast.key.visit(this)}] = ${ast.value.visit(this)}`; | ||
} | ||
|
||
visitLiteralArray(ast: LiteralArray): string { | ||
const values = ast.expressions.map(expr => expr.visit(this)).join(','); | ||
return `[${values}]`; | ||
} | ||
|
||
visitLiteralMap(ast: LiteralMap): string { | ||
const values = ast.keys.map((key, i) => `${key}: ${ast.values[i].visit(this)}`).join(','); | ||
return `{${values}}`; | ||
} | ||
|
||
visitLiteralPrimitive(ast: LiteralPrimitive): string { | ||
return `${ast.value}`; | ||
} | ||
|
||
visitPipe(ast: BindingPipe): string { | ||
const args = ast.args.map(expr => expr.visit(this)).join(' : '); | ||
return `${ast.exp.visit(this)} | ${ast.name}: ${args}`; | ||
} | ||
|
||
visitPrefixNot(ast: PrefixNot): string { | ||
return `!${ast.expression.visit(this)}`; | ||
} | ||
|
||
visitNonNullAssert(ast: NonNullAssert): string { | ||
return `${ast.expression.visit(this)}!`; | ||
} | ||
|
||
visitPropertyRead(ast: PropertyRead): string { | ||
return `${ast.receiver.visit(this)}.${ast.name}`; | ||
} | ||
|
||
visitPropertyWrite(ast: PropertyWrite): string { | ||
return `${ast.receiver.visit(this)}.${ast.name} = ${ast.value.visit(this)}`; | ||
} | ||
|
||
visitSafePropertyRead(ast: SafePropertyRead): string { | ||
return `${ast.receiver.visit(this)}?.${ast.name}`; | ||
} | ||
|
||
visitSafeKeyedRead(ast: SafeKeyedRead): string { | ||
return `${ast.receiver.visit(this)}?.[${ast.key.visit(this)}]`; | ||
} | ||
|
||
visitCall(ast: Call): string { | ||
const args = ast.args.map(expr => expr.visit(this)).join(','); | ||
return `${ast.receiver.visit(this)}(${args})`; | ||
} | ||
|
||
visitSafeCall(ast: SafeCall): string { | ||
const args = ast.args.map(expr => expr.visit(this)).join(','); | ||
return `${ast.receiver.visit(this)}?.(${args})`; | ||
} | ||
|
||
visitASTWithSource(ast: ASTWithSource): string { | ||
return ast.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
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