|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import * as t from '@babel/types'; |
| 9 | + |
| 10 | +import {AstFactory, BinaryOperator, LeadingComment, ObjectLiteralProperty, SourceMapRange, TemplateLiteral, VariableDeclarationType} from '../../../../src/ngtsc/translator'; |
| 11 | +import {assert} from '../utils'; |
| 12 | + |
| 13 | +export class BabelAstFactory implements AstFactory<t.Statement, t.Expression> { |
| 14 | + attachComments(statement: t.Statement, leadingComments: LeadingComment[]|undefined): t.Statement { |
| 15 | + if (leadingComments === undefined) { |
| 16 | + return statement; |
| 17 | + } |
| 18 | + // We must process the comments in reverse because `t.addComment()` will add new ones in front. |
| 19 | + for (let i = leadingComments.length - 1; i >= 0; i--) { |
| 20 | + const comment = leadingComments[i]; |
| 21 | + t.addComment(statement, 'leading', comment.toString(), !comment.multiline); |
| 22 | + } |
| 23 | + return statement; |
| 24 | + } |
| 25 | + |
| 26 | + createArrayLiteral = t.arrayExpression; |
| 27 | + |
| 28 | + createAssignment(target: t.Expression, value: t.Expression): t.Expression { |
| 29 | + assert(target, isLExpression, 'must be a left hand side expression'); |
| 30 | + return t.assignmentExpression('=', target, value); |
| 31 | + } |
| 32 | + |
| 33 | + createBinaryExpression( |
| 34 | + leftOperand: t.Expression, operator: BinaryOperator, |
| 35 | + rightOperand: t.Expression): t.Expression { |
| 36 | + switch (operator) { |
| 37 | + case '&&': |
| 38 | + case '||': |
| 39 | + return t.logicalExpression(operator, leftOperand, rightOperand); |
| 40 | + default: |
| 41 | + return t.binaryExpression(operator, leftOperand, rightOperand); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + createBlock = t.blockStatement; |
| 46 | + |
| 47 | + createCallExpression(callee: t.Expression, args: t.Expression[], pure: boolean): t.Expression { |
| 48 | + const call = t.callExpression(callee, args); |
| 49 | + if (pure) { |
| 50 | + t.addComment(call, 'leading', ' @__PURE__ ', /* line */ false); |
| 51 | + } |
| 52 | + return call; |
| 53 | + } |
| 54 | + |
| 55 | + createConditional = t.conditionalExpression; |
| 56 | + |
| 57 | + createElementAccess(expression: t.Expression, element: t.Expression): t.Expression { |
| 58 | + return t.memberExpression(expression, element, /* computed */ true); |
| 59 | + } |
| 60 | + |
| 61 | + createExpressionStatement = t.expressionStatement; |
| 62 | + |
| 63 | + createFunctionDeclaration(functionName: string, parameters: string[], body: t.Statement): |
| 64 | + t.Statement { |
| 65 | + assert(body, t.isBlockStatement, 'a block'); |
| 66 | + return t.functionDeclaration( |
| 67 | + t.identifier(functionName), parameters.map(param => t.identifier(param)), body); |
| 68 | + } |
| 69 | + |
| 70 | + createFunctionExpression(functionName: string|null, parameters: string[], body: t.Statement): |
| 71 | + t.Expression { |
| 72 | + assert(body, t.isBlockStatement, 'a block'); |
| 73 | + const name = functionName !== null ? t.identifier(functionName) : null; |
| 74 | + return t.functionExpression(name, parameters.map(param => t.identifier(param)), body); |
| 75 | + } |
| 76 | + |
| 77 | + createIdentifier = t.identifier; |
| 78 | + |
| 79 | + createIfStatement = t.ifStatement; |
| 80 | + |
| 81 | + createLiteral(value: string|number|boolean|null|undefined): t.Expression { |
| 82 | + if (typeof value === 'string') { |
| 83 | + return t.stringLiteral(value); |
| 84 | + } else if (typeof value === 'number') { |
| 85 | + return t.numericLiteral(value); |
| 86 | + } else if (typeof value === 'boolean') { |
| 87 | + return t.booleanLiteral(value); |
| 88 | + } else if (value === undefined) { |
| 89 | + return t.identifier('undefined'); |
| 90 | + } else if (value === null) { |
| 91 | + return t.nullLiteral(); |
| 92 | + } else { |
| 93 | + throw new Error(`Invalid literal: ${value} (${typeof value})`); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + createNewExpression = t.newExpression; |
| 98 | + |
| 99 | + createObjectLiteral(properties: ObjectLiteralProperty<t.Expression>[]): t.Expression { |
| 100 | + return t.objectExpression(properties.map(prop => { |
| 101 | + const key = |
| 102 | + prop.quoted ? t.stringLiteral(prop.propertyName) : t.identifier(prop.propertyName); |
| 103 | + return t.objectProperty(key, prop.value); |
| 104 | + })); |
| 105 | + } |
| 106 | + |
| 107 | + createParenthesizedExpression = t.parenthesizedExpression; |
| 108 | + |
| 109 | + createPropertyAccess(expression: t.Expression, propertyName: string): t.Expression { |
| 110 | + return t.memberExpression(expression, t.identifier(propertyName), /* computed */ false); |
| 111 | + } |
| 112 | + |
| 113 | + createReturnStatement = t.returnStatement; |
| 114 | + |
| 115 | + createTaggedTemplate(tag: t.Expression, template: TemplateLiteral<t.Expression>): t.Expression { |
| 116 | + const elements = template.elements.map( |
| 117 | + (element, i) => this.setSourceMapRange( |
| 118 | + t.templateElement(element, i === template.elements.length - 1), element.range)); |
| 119 | + return t.taggedTemplateExpression(tag, t.templateLiteral(elements, template.expressions)); |
| 120 | + } |
| 121 | + |
| 122 | + createThrowStatement = t.throwStatement; |
| 123 | + |
| 124 | + createTypeOfExpression(expression: t.Expression): t.Expression { |
| 125 | + return t.unaryExpression('typeof', expression); |
| 126 | + } |
| 127 | + |
| 128 | + createUnaryExpression = t.unaryExpression; |
| 129 | + |
| 130 | + createVariableDeclaration( |
| 131 | + variableName: string, initializer: t.Expression|null, |
| 132 | + type: VariableDeclarationType): t.Statement { |
| 133 | + return t.variableDeclaration( |
| 134 | + type, [t.variableDeclarator(t.identifier(variableName), initializer)]); |
| 135 | + } |
| 136 | + |
| 137 | + setSourceMapRange<T extends t.Statement|t.Expression|t.TemplateElement>( |
| 138 | + node: T, sourceMapRange: SourceMapRange|null): T { |
| 139 | + if (sourceMapRange === null) { |
| 140 | + return node; |
| 141 | + } |
| 142 | + // Note that the linker only works on a single file at a time, so there is no need to track the |
| 143 | + // filename. Babel will just use the current filename in the source-map. |
| 144 | + node.loc = { |
| 145 | + start: { |
| 146 | + line: sourceMapRange.start.line + 1, // lines are 1-based in Babel. |
| 147 | + column: sourceMapRange.start.column, |
| 148 | + }, |
| 149 | + end: { |
| 150 | + line: sourceMapRange.end.line + 1, // lines are 1-based in Babel. |
| 151 | + column: sourceMapRange.end.column, |
| 152 | + }, |
| 153 | + }; |
| 154 | + node.start = sourceMapRange.start.offset; |
| 155 | + node.end = sourceMapRange.end.offset; |
| 156 | + |
| 157 | + return node; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +function isLExpression(expr: t.Expression): expr is Extract<t.LVal, t.Expression> { |
| 162 | + // Some LVal types are not expressions, which prevents us from using `t.isLVal()` |
| 163 | + // directly with `assert()`. |
| 164 | + return t.isLVal(expr); |
| 165 | +} |
0 commit comments