MCPcopy Create free account
hub / github.com/angular/angular / TcbExpressionTranslator

Class TcbExpressionTranslator

packages/compiler/src/typecheck/ops/expression.ts:105–325  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

103}
104
105export class TcbExpressionTranslator {
106 constructor(
107 protected tcb: Context,
108 protected scope: Scope,
109 ) {}
110
111 translate(ast: AST): TcbExpr {
112 // `astToTcbExpr` actually does the conversion. A special resolver `tcbResolve` is passed
113 // which interprets specific expression nodes that interact with the `ImplicitReceiver`. These
114 // nodes actually refer to identifiers within the current scope.
115 return astToTcbExpr(ast, (ast: AST) => this.resolve(ast), this.tcb.env.config);
116 }
117
118 /**
119 * Resolve an `AST` expression within the given scope.
120 *
121 * Some `AST` expressions refer to top-level concepts (references, variables, the component
122 * context). This method assists in resolving those.
123 */
124 protected resolve(ast: AST): TcbExpr | null {
125 if (ast instanceof PropertyRead && ast.receiver instanceof ImplicitReceiver) {
126 // Try to resolve a bound target for this expression. If no such target is available, then
127 // the expression is referencing the top-level component context. In that case, `null` is
128 // returned here to let it fall through resolution so it will be caught when the
129 // `ImplicitReceiver` is resolved in the branch below.
130 const target = this.tcb.boundTarget.getExpressionTarget(ast);
131 const targetExpression = target === null ? null : this.getTargetNodeExpression(target, ast);
132 if (target instanceof LetDeclaration && !this.isValidLetDeclarationAccess(target, ast)) {
133 this.tcb.oobRecorder.letUsedBeforeDefinition(this.tcb.id, ast, target);
134 // Cast the expression to `any` so we don't produce additional diagnostics.
135 // We don't use `markIgnoreForDiagnostics` here, because it won't prevent duplicate
136 // diagnostics for nested accesses in cases like `@let value = value.foo.bar.baz`.
137 if (targetExpression !== null) {
138 return new TcbExpr(`${targetExpression.print()} as any`);
139 }
140 }
141
142 return targetExpression;
143 } else if (
144 (ast instanceof Binary && Binary.isAssignmentOperation(ast.operation)) ||
145 (ast instanceof Unary && Unary.isUpdateOperation(ast.operator))
146 ) {
147 const update = ast instanceof Unary ? ast : null;
148
149 const read = unwrapWriteTarget(update !== null ? update.expr : ast.left);
150
151 if (
152 !(read instanceof PropertyRead) ||
153 !(read.receiver instanceof ImplicitReceiver || read.receiver instanceof ThisReceiver)
154 ) {
155 return null;
156 }
157
158 const target = this.tcb.boundTarget.getExpressionTarget(read);
159 if (target === null || target instanceof ArrowFunctionIdentifierParameter) {
160 return null;
161 }
162

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected