wrap-enums.js 15.2 KB
Newer Older
Kriengkrai Yothee's avatar
Kriengkrai Yothee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */
const ts = require("typescript");
const ast_utils_1 = require("../helpers/ast-utils");
function isBlockLike(node) {
    return node.kind === ts.SyntaxKind.Block
        || node.kind === ts.SyntaxKind.ModuleBlock
        || node.kind === ts.SyntaxKind.CaseClause
        || node.kind === ts.SyntaxKind.DefaultClause
        || node.kind === ts.SyntaxKind.SourceFile;
}
function getWrapEnumsTransformer() {
    return (context) => {
        const transformer = sf => {
            const result = visitBlockStatements(sf.statements, context);
            return ts.updateSourceFileNode(sf, ts.setTextRange(result, sf.statements));
        };
        return transformer;
    };
}
exports.getWrapEnumsTransformer = getWrapEnumsTransformer;
function visitBlockStatements(statements, context) {
    // copy of statements to modify; lazy initialized
    let updatedStatements;
    const visitor = (node) => {
        if (isBlockLike(node)) {
            let result = visitBlockStatements(node.statements, context);
            if (result === node.statements) {
                return node;
            }
            result = ts.setTextRange(result, node.statements);
            switch (node.kind) {
                case ts.SyntaxKind.Block:
                    return ts.updateBlock(node, result);
                case ts.SyntaxKind.ModuleBlock:
                    return ts.updateModuleBlock(node, result);
                case ts.SyntaxKind.CaseClause:
                    return ts.updateCaseClause(node, node.expression, result);
                case ts.SyntaxKind.DefaultClause:
                    return ts.updateDefaultClause(node, result);
                default:
                    return node;
            }
        }
        else {
            return node;
        }
    };
    // 'oIndex' is the original statement index; 'uIndex' is the updated statement index
    for (let oIndex = 0, uIndex = 0; oIndex < statements.length - 1; oIndex++, uIndex++) {
        const currentStatement = statements[oIndex];
        let newStatement;
        let oldStatementsLength = 0;
        // these can't contain an enum declaration
        if (currentStatement.kind === ts.SyntaxKind.ImportDeclaration) {
            continue;
        }
        // enum declarations must:
        //   * not be last statement
        //   * be a variable statement
        //   * have only one declaration
        //   * have an identifer as a declaration name
        // ClassExpression declarations must:
        //   * not be last statement
        //   * be a variable statement
        //   * have only one declaration
        //   * have an ClassExpression or BinaryExpression and a right
        //     of kind ClassExpression as a initializer
        if (ts.isVariableStatement(currentStatement)
            && currentStatement.declarationList.declarations.length === 1) {
            const variableDeclaration = currentStatement.declarationList.declarations[0];
            const initializer = variableDeclaration.initializer;
            if (ts.isIdentifier(variableDeclaration.name)) {
                const name = variableDeclaration.name.text;
                if (!initializer) {
                    const iife = findTs2_3EnumIife(name, statements[oIndex + 1]);
                    if (iife) {
                        // update IIFE and replace variable statement and old IIFE
                        oldStatementsLength = 2;
                        newStatement = updateEnumIife(currentStatement, iife[0], iife[1]);
                        // skip IIFE statement
                        oIndex++;
                    }
                }
                else if (ts.isObjectLiteralExpression(initializer)) {
                    // tsickle es2015 enums first statement is an export declaration
                    const isPotentialEnumExport = ts.isExportDeclaration(statements[oIndex + 1]);
                    if (isPotentialEnumExport) {
                        // skip the export
                        oIndex++;
                    }
                    const enumStatements = findStatements(name, statements, oIndex, 1);
                    if (!enumStatements) {
                        continue;
                    }
                    // create wrapper and replace variable statement and enum member statements
                    oldStatementsLength = enumStatements.length + (isPotentialEnumExport ? 2 : 1);
                    newStatement = createWrappedEnum(name, currentStatement, enumStatements, initializer, isPotentialEnumExport);
                    // skip enum member declarations
                    oIndex += enumStatements.length;
                }
                else if (ts.isClassExpression(initializer)
                    || (ts.isBinaryExpression(initializer)
                        && ts.isClassExpression(initializer.right))) {
                    const classStatements = findStatements(name, statements, oIndex);
                    if (!classStatements) {
                        continue;
                    }
                    oldStatementsLength = classStatements.length;
                    newStatement = createWrappedClass(variableDeclaration, classStatements);
                    oIndex += classStatements.length - 1;
                }
            }
        }
        else if (ts.isClassDeclaration(currentStatement)) {
            const name = currentStatement.name.text;
            const classStatements = findStatements(name, statements, oIndex);
            if (!classStatements) {
                continue;
            }
            oldStatementsLength = classStatements.length;
            newStatement = createWrappedClass(currentStatement, classStatements);
            oIndex += classStatements.length - 1;
        }
        if (newStatement && newStatement.length > 0) {
            if (!updatedStatements) {
                updatedStatements = [...statements];
            }
            updatedStatements.splice(uIndex, oldStatementsLength, ...newStatement);
            // When having more than a single new statement
            // we need to update the update Index
            uIndex += (newStatement ? newStatement.length - 1 : 0);
        }
        const result = ts.visitNode(currentStatement, visitor);
        if (result !== currentStatement) {
            if (!updatedStatements) {
                updatedStatements = statements.slice();
            }
            updatedStatements[uIndex] = result;
        }
    }
    // if changes, return updated statements
    // otherwise, return original array instance
    return updatedStatements ? ts.createNodeArray(updatedStatements) : statements;
}
// TS 2.3 enums have statements that are inside a IIFE.
function findTs2_3EnumIife(name, statement) {
    if (!ts.isExpressionStatement(statement)) {
        return null;
    }
    const expression = statement.expression;
    if (!expression || !ts.isCallExpression(expression) || expression.arguments.length !== 1) {
        return null;
    }
    const callExpression = expression;
    let exportExpression;
    if (!ts.isParenthesizedExpression(callExpression.expression)) {
        return null;
    }
    const functionExpression = callExpression.expression.expression;
    if (!ts.isFunctionExpression(functionExpression)) {
        return null;
    }
    // The name of the parameter can be different than the name of the enum if it was renamed
    // due to scope hoisting.
    const parameter = functionExpression.parameters[0];
    if (!ts.isIdentifier(parameter.name)) {
        return null;
    }
    const parameterName = parameter.name.text;
    let argument = callExpression.arguments[0];
    if (!ts.isBinaryExpression(argument)
        || !ts.isIdentifier(argument.left)
        || argument.left.text !== name) {
        return null;
    }
    let potentialExport = false;
    if (argument.operatorToken.kind === ts.SyntaxKind.FirstAssignment) {
        if (ts.isBinaryExpression(argument.right) && argument.right.operatorToken.kind !== ts.SyntaxKind.BarBarToken) {
            return null;
        }
        potentialExport = true;
        argument = argument.right;
    }
    if (!ts.isBinaryExpression(argument)) {
        return null;
    }
    if (argument.operatorToken.kind !== ts.SyntaxKind.BarBarToken) {
        return null;
    }
    if (potentialExport && !ts.isIdentifier(argument.left)) {
        exportExpression = argument.left;
    }
    // Go through all the statements and check that all match the name
    for (const statement of functionExpression.body.statements) {
        if (!ts.isExpressionStatement(statement)
            || !ts.isBinaryExpression(statement.expression)
            || !ts.isElementAccessExpression(statement.expression.left)) {
            return null;
        }
        const leftExpression = statement.expression.left.expression;
        if (!ts.isIdentifier(leftExpression) || leftExpression.text !== parameterName) {
            return null;
        }
    }
    return [callExpression, exportExpression];
}
function updateHostNode(hostNode, expression) {
    // Update existing host node with the pure comment before the variable declaration initializer.
    const variableDeclaration = hostNode.declarationList.declarations[0];
    const outerVarStmt = ts.updateVariableStatement(hostNode, hostNode.modifiers, ts.updateVariableDeclarationList(hostNode.declarationList, [
        ts.updateVariableDeclaration(variableDeclaration, variableDeclaration.name, variableDeclaration.type, expression),
    ]));
    return outerVarStmt;
}
/**
 * Find enums, class expression or declaration statements.
 *
 * The classExpressions block to wrap in an iife must
 * - end with an ExpressionStatement
 * - it's expression must be a BinaryExpression
 * - have the same name
 *
 * ```
 let Foo = class Foo {};
 Foo = __decorate([]);
 ```
 */
function findStatements(name, statements, statementIndex, offset = 0) {
    let count = 1;
    for (let index = statementIndex + 1; index < statements.length; ++index) {
        const statement = statements[index];
        if (!ts.isExpressionStatement(statement)) {
            break;
        }
        const expression = statement.expression;
        if (ts.isCallExpression(expression)) {
            // Ex:
            // setClassMetadata(FooClass, [{}], void 0);
            // __decorate([propDecorator()], FooClass.prototype, "propertyName", void 0);
            // __decorate([propDecorator()], FooClass, "propertyName", void 0);
            // __decorate$1([propDecorator()], FooClass, "propertyName", void 0);
            const args = expression.arguments;
            if (args.length > 2) {
                const isReferenced = args.some(arg => {
                    const potentialIdentifier = ts.isPropertyAccessExpression(arg) ? arg.expression : arg;
                    return ts.isIdentifier(potentialIdentifier) && potentialIdentifier.text === name;
                });
                if (isReferenced) {
                    count++;
                    continue;
                }
            }
        }
        else if (ts.isBinaryExpression(expression)) {
            const node = ts.isBinaryExpression(expression.left)
                ? expression.left.left
                : expression.left;
            const leftExpression = ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)
                // Static Properties // Ex: Foo.bar = 'value';
                // ENUM Property // Ex:  ChangeDetectionStrategy[ChangeDetectionStrategy.Default] = "Default";
                ? node.expression
                // Ex: FooClass = __decorate([Component()], FooClass);
                : node;
            if (ts.isIdentifier(leftExpression) && leftExpression.text === name) {
                count++;
                continue;
            }
        }
        break;
    }
    if (count > 1) {
        return statements.slice(statementIndex + offset, statementIndex + count);
    }
    return undefined;
}
function updateEnumIife(hostNode, iife, exportAssignment) {
    if (!ts.isParenthesizedExpression(iife.expression)
        || !ts.isFunctionExpression(iife.expression.expression)) {
        throw new Error('Invalid IIFE Structure');
    }
    // Ignore export assignment if variable is directly exported
    if (hostNode.modifiers
        && hostNode.modifiers.findIndex(m => m.kind == ts.SyntaxKind.ExportKeyword) != -1) {
        exportAssignment = undefined;
    }
    const expression = iife.expression.expression;
    const updatedFunction = ts.updateFunctionExpression(expression, expression.modifiers, expression.asteriskToken, expression.name, expression.typeParameters, expression.parameters, expression.type, ts.updateBlock(expression.body, [
        ...expression.body.statements,
        ts.createReturn(expression.parameters[0].name),
    ]));
    let arg = ts.createObjectLiteral();
    if (exportAssignment) {
        arg = ts.createBinary(exportAssignment, ts.SyntaxKind.BarBarToken, arg);
    }
    const updatedIife = ts.updateCall(iife, ts.updateParen(iife.expression, updatedFunction), iife.typeArguments, [arg]);
    let value = ast_utils_1.addPureComment(updatedIife);
    if (exportAssignment) {
        value = ts.createBinary(exportAssignment, ts.SyntaxKind.FirstAssignment, updatedIife);
    }
    return [updateHostNode(hostNode, value)];
}
function createWrappedEnum(name, hostNode, statements, literalInitializer = ts.createObjectLiteral(), addExportModifier = false) {
    const node = addExportModifier
        ? ts.updateVariableStatement(hostNode, [ts.createToken(ts.SyntaxKind.ExportKeyword)], hostNode.declarationList)
        : hostNode;
    const innerVarStmt = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([
        ts.createVariableDeclaration(name, undefined, literalInitializer),
    ]));
    const innerReturn = ts.createReturn(ts.createIdentifier(name));
    const iife = ts.createImmediatelyInvokedFunctionExpression([
        innerVarStmt,
        ...statements,
        innerReturn,
    ]);
    return [updateHostNode(node, ast_utils_1.addPureComment(ts.createParen(iife)))];
}
function createWrappedClass(hostNode, statements) {
    const name = hostNode.name.text;
    const updatedStatements = [...statements];
    if (ts.isClassDeclaration(hostNode)) {
        updatedStatements[0] = ts.createClassDeclaration(hostNode.decorators, undefined, hostNode.name, hostNode.typeParameters, hostNode.heritageClauses, hostNode.members);
    }
    const pureIife = ast_utils_1.addPureComment(ts.createImmediatelyInvokedArrowFunction([
        ...updatedStatements,
        ts.createReturn(ts.createIdentifier(name)),
    ]));
    const modifiers = hostNode.modifiers;
    const isDefault = !!modifiers
        && modifiers.some(x => x.kind === ts.SyntaxKind.DefaultKeyword);
    const newStatement = [];
    newStatement.push(ts.createVariableStatement(isDefault ? undefined : modifiers, ts.createVariableDeclarationList([
        ts.createVariableDeclaration(name, undefined, pureIife),
    ], ts.NodeFlags.Let)));
    if (isDefault) {
        newStatement.push(ts.createExportAssignment(undefined, undefined, false, ts.createIdentifier(name)));
    }
    return newStatement;
}