* Convert a template AST expression into an output AST expression.
( ast: e.AST, job: CompilationJob, baseSourceSpan: ParseSourceSpan | null, )
| 1177 | * Convert a template AST expression into an output AST expression. |
| 1178 | */ |
| 1179 | function convertAst( |
| 1180 | ast: e.AST, |
| 1181 | job: CompilationJob, |
| 1182 | baseSourceSpan: ParseSourceSpan | null, |
| 1183 | ): o.Expression { |
| 1184 | if (ast instanceof e.ASTWithSource) { |
| 1185 | return convertAst(ast.ast, job, baseSourceSpan); |
| 1186 | } else if (ast instanceof e.PropertyRead) { |
| 1187 | if (ast.receiver instanceof e.ImplicitReceiver) { |
| 1188 | return new ir.LexicalReadExpr(ast.name); |
| 1189 | } else { |
| 1190 | return new o.ReadPropExpr( |
| 1191 | convertAst(ast.receiver, job, baseSourceSpan), |
| 1192 | ast.name, |
| 1193 | null, |
| 1194 | convertSourceSpan(ast.span, baseSourceSpan), |
| 1195 | ); |
| 1196 | } |
| 1197 | } else if (ast instanceof e.Call) { |
| 1198 | if (ast.receiver instanceof e.ImplicitReceiver) { |
| 1199 | throw new Error(`Unexpected ImplicitReceiver`); |
| 1200 | } else { |
| 1201 | return new o.InvokeFunctionExpr( |
| 1202 | convertAst(ast.receiver, job, baseSourceSpan), |
| 1203 | ast.args.map((arg) => convertAst(arg, job, baseSourceSpan)), |
| 1204 | undefined, |
| 1205 | convertSourceSpan(ast.span, baseSourceSpan), |
| 1206 | ); |
| 1207 | } |
| 1208 | } else if (ast instanceof e.LiteralPrimitive) { |
| 1209 | return o.literal(ast.value, undefined, convertSourceSpan(ast.span, baseSourceSpan)); |
| 1210 | } else if (ast instanceof e.Unary) { |
| 1211 | switch (ast.operator) { |
| 1212 | case '+': |
| 1213 | return new o.UnaryOperatorExpr( |
| 1214 | o.UnaryOperator.Plus, |
| 1215 | convertAst(ast.expr, job, baseSourceSpan), |
| 1216 | undefined, |
| 1217 | convertSourceSpan(ast.span, baseSourceSpan), |
| 1218 | ); |
| 1219 | case '-': |
| 1220 | return new o.UnaryOperatorExpr( |
| 1221 | o.UnaryOperator.Minus, |
| 1222 | convertAst(ast.expr, job, baseSourceSpan), |
| 1223 | undefined, |
| 1224 | convertSourceSpan(ast.span, baseSourceSpan), |
| 1225 | ); |
| 1226 | default: |
| 1227 | throw new Error(`AssertionError: unknown unary operator ${ast.operator}`); |
| 1228 | } |
| 1229 | } else if (ast instanceof e.Binary) { |
| 1230 | const operator = BINARY_OPERATORS.get(ast.operation); |
| 1231 | if (operator === undefined) { |
| 1232 | throw new Error(`AssertionError: unknown binary operator ${ast.operation}`); |
| 1233 | } |
| 1234 | return new o.BinaryOperatorExpr( |
| 1235 | operator, |
| 1236 | convertAst(ast.left, job, baseSourceSpan), |
no test coverage detected