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

Class TcbExprTranslator

packages/compiler/src/typecheck/expression.ts:58–441  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56}
57
58class TcbExprTranslator implements AstVisitor {
59 constructor(
60 private maybeResolve: (ast: AST) => TcbExpr | null,
61 private config: TypeCheckingConfig,
62 ) {}
63
64 translate(ast: AST): TcbExpr {
65 if (ast instanceof ASTWithSource) {
66 ast = ast.ast;
67 }
68
69 // First attempt to let any custom resolution logic provide a translation for the given node.
70 const resolved = this.maybeResolve(ast);
71 if (resolved !== null) {
72 return resolved;
73 }
74
75 return ast.visit(this);
76 }
77
78 visitUnary(ast: Unary): TcbExpr {
79 // Note that we don't need to guard against sequences like `+ +a` collapsing into `++a`,
80 // because `wrapForTypeChecker` always parenthesizes nested unary expressions.
81 const expr = this.translate(ast.expr).print();
82 const node = new TcbExpr(ast.isPrefix ? `${ast.operator}${expr}` : `${expr}${ast.operator}`);
83 return node.wrapForTypeChecker().addParseSpanInfo(ast.sourceSpan);
84 }
85
86 visitBinary(ast: Binary): TcbExpr {
87 const lhs = this.translate(ast.left);
88 const rhs = this.translate(ast.right);
89 lhs.wrapForTypeChecker();
90 rhs.wrapForTypeChecker();
91 const expression = `${lhs.print()} ${ast.operation} ${rhs.print()}`;
92 const node = new TcbExpr(
93 ast.operation === '??' || ast.operation === '**' ? `(${expression})` : expression,
94 );
95 node.addParseSpanInfo(ast.sourceSpan);
96 return node;
97 }
98
99 visitChain(ast: Chain): TcbExpr {
100 const elements = ast.expressions.map((expr) => this.translate(expr).print());
101 const node = new TcbExpr(elements.join(', '));
102 node.wrapForTypeChecker();
103 node.addParseSpanInfo(ast.sourceSpan);
104 return node;
105 }
106
107 visitConditional(ast: Conditional): TcbExpr {
108 const condExpr = this.translate(ast.condition);
109 const trueExpr = this.translate(ast.trueExp);
110 // Wrap `falseExpr` in parens so that the trailing parse span info is not attributed to the
111 // whole conditional.
112 // In the following example, the last source span comment (5,6) could be seen as the
113 // trailing comment for _either_ the whole conditional expression _or_ just the `falseExpr` that
114 // is immediately before it:
115 // `conditional /*1,2*/ ? trueExpr /*3,4*/ : falseExpr /*5,6*/`

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected