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

Class Unparser

packages/compiler/test/expression_parser/utils/unparser.ts:44–281  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42} from '../../../src/expression_parser/ast';
43
44class Unparser implements AstVisitor {
45 private static _quoteRegExp = /"/g;
46 // using non-null assertion because they're both re(set) by unparse()
47 private _expression!: string;
48
49 unparse(ast: AST) {
50 this._expression = '';
51 this._visit(ast);
52 return this._expression;
53 }
54
55 visitPropertyRead(ast: PropertyRead, context: any) {
56 this._visit(ast.receiver);
57 this._expression +=
58 ast.receiver instanceof ImplicitReceiver || ast.receiver instanceof ThisReceiver
59 ? `${ast.name}`
60 : `.${ast.name}`;
61 }
62
63 visitUnary(ast: Unary, context: any) {
64 const previous = this._expression;
65 this._expression = '';
66 this._visit(ast.expr);
67 const inner = this._expression;
68 this._expression = previous;
69
70 if (ast.isPrefix) {
71 const space = inner.startsWith(ast.operator[0]) ? ' ' : '';
72 this._expression += `${ast.operator}${space}${inner}`;
73 } else {
74 this._expression += `${inner}${ast.operator}`;
75 }
76 }
77
78 visitBinary(ast: Binary, context: any) {
79 this._visit(ast.left);
80 this._expression += ` ${ast.operation} `;
81 this._visit(ast.right);
82 }
83
84 visitChain(ast: Chain, context: any) {
85 const len = ast.expressions.length;
86 for (let i = 0; i < len; i++) {
87 this._visit(ast.expressions[i]);
88 this._expression += i == len - 1 ? ';' : '; ';
89 }
90 }
91
92 visitConditional(ast: Conditional, context: any) {
93 this._visit(ast.condition);
94 this._expression += ' ? ';
95 this._visit(ast.trueExp);
96 this._expression += ' : ';
97 this._visit(ast.falseExp);
98 }
99
100 visitPipe(ast: BindingPipe, context: any) {
101 this._expression += '(';

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected