All files transform.ts

100% Statements 51/51
100% Branches 27/27
100% Functions 8/8
100% Lines 51/51
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  1x 1x 1x       1x 24x     1x   9x 1x     8x 1x         7x 7x 7x   7x 24x     7x   24x 24x 24x 24x 24x   24x   24x 24x     7x 162x     6x     6x 6x     18x   18x 18x   18x     6x     18x     6x 6x   18x 18x                   162x     7x 6x     7x 6x     7x     1x 1x 1x 1x     1x 1x  
import * as kt from "karma-typescript/src/api/transforms";
import * as log4js from "log4js";
import * as path from "path";
import * as ts from "typescript";
 
let log: log4js.Logger;
 
let fixWindowsPath = (value: string): string => {
    return value.replace(/\\/g, "/");
};
 
let transform: kt.Transform = (context: kt.TransformContext, callback: kt.TransformCallback) => {
 
    if (!context.ts) {
        return callback(undefined, false);
    }
 
    if (ts.version !== context.ts.version) {
        return callback(new Error("Typescript version of karma-typescript (" +
            context.ts.version + ") does not match karma-typescript-angular2-transform Typescript version (" +
            ts.version + ")"), false);
    }
 
    let dirty = false;
    let MagicString = require("magic-string");
    let magic = new MagicString(context.source);
 
    let isStringKind = (kind: ts.SyntaxKind): boolean => {
        return kind === ts.SyntaxKind.StringLiteral || kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral;
    };
 
    let rewriteUrl = (node: ts.StringLiteral): void => {
 
        let start = node.getStart() + 1;
        let end = start + node.text.length;
        let templateDir = path.dirname(context.filename);
        let relativeTemplateDir = path.relative(context.config.karma.basePath, templateDir);
        let styleUrl = path.join(context.config.karma.urlRoot, "base", relativeTemplateDir, node.text);
 
        log.debug("Rewriting %s to %s in %s", node.text, styleUrl, context.filename);
 
        magic.overwrite(start, end, fixWindowsPath(styleUrl));
        dirty = true;
    };
 
    let visitNode = (node: ts.Node) => {
        switch (node.kind) {
            case ts.SyntaxKind.ObjectLiteralExpression:
 
                let expression = (<ts.ObjectLiteralExpression> node);
 
                /* istanbul ignore else */
                if (expression.properties) {
                    expression.properties.forEach((p) => {
 
                        /* istanbul ignore else */
                        if (p.name && p.kind === ts.SyntaxKind.PropertyAssignment) {
 
                            let property = (<ts.PropertyAssignment> p);
                            let identifier = (<ts.Identifier> property.name);
 
                            if (identifier.text === "templateUrl" &&
                                    isStringKind(property.initializer.kind)) {
 
                                rewriteUrl((<ts.StringLiteral> property.initializer));
                            }
 
                            if (identifier.text === "styleUrls" &&
                                    property.initializer.kind === ts.SyntaxKind.ArrayLiteralExpression) {
 
                                let initializer = (<ts.ArrayLiteralExpression> property.initializer);
                                initializer.elements.forEach((element) => {
                                    /* istanbul ignore else */
                                    if (isStringKind(element.kind)) {
                                        rewriteUrl((<ts.StringLiteral> element));
                                    }
                                });
                            }
                        }
                    });
                }
            default:
        }
 
        ts.forEachChild(node, visitNode);
    };
 
    if (context.source.indexOf("templateUrl") > 0 || context.source.indexOf("styleUrls") > 0) {
        visitNode(context.ts.ast);
    }
 
    if (dirty) {
        context.source = magic.toString();
    }
 
    callback(undefined, dirty);
};
 
let initialize: kt.TransformInitialize = (logOptions: kt.TransformInitializeLogOptions) => {
    log4js.setGlobalLogLevel(logOptions.level);
    log4js.configure({ appenders: logOptions.appenders });
    log = log4js.getLogger("angular2-transform.karma-typescript");
};
 
let exp = Object.assign(transform, { initialize });
export = exp;