All files readonly-shared.js

100% Statements 45/45
92.86% Branches 26/28
100% Functions 8/8
100% Lines 44/44
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            1x 1x 1x 1x   157x   1x   20x   20x 13x 13x 6x 6x     20x   1x   20x     1779x       17x 17x   17x   17x     1762x   1762x     1x   1779x   1x   17x   17x 12x 12x 12x 12x       17x 7x 7x 7x     17x   1x     260x 114x       105x 105x 41x       219x   1x  
/**
 * This file has code that is shared for all the readonly rules.
 * It supports the options for ignore-local and ignore-prefix which all readonly rules have.
 * The rules ony need to provide a checker function.
 */
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
var OPTION_IGNORE_LOCAL = "ignore-local";
var OPTION_IGNORE_PREFIX = "ignore-prefix";
function createInvalidNode(node, replacement) {
    return { node: node, replacement: replacement };
}
exports.createInvalidNode = createInvalidNode;
function parseOptions(options) {
    var ignoreLocal = options.indexOf(OPTION_IGNORE_LOCAL) !== -1;
    var ignorePrefix;
    for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
        var o = options_1[_i];
        if (typeof o === "object" && o[OPTION_IGNORE_PREFIX] !== null) {
            ignorePrefix = o[OPTION_IGNORE_PREFIX];
            break;
        }
    }
    return { ignoreLocal: ignoreLocal, ignorePrefix: ignorePrefix };
}
exports.parseOptions = parseOptions;
function walk(ctx, checkNode, failureString) {
    return ts.forEachChild(ctx.sourceFile, cb);
    function cb(node) {
        // Skip checking in functions if ignore-local is set
        if (ctx.options.ignoreLocal && (node.kind === ts.SyntaxKind.FunctionDeclaration
            || node.kind === ts.SyntaxKind.ArrowFunction || node.kind === ts.SyntaxKind.FunctionExpression
            || node.kind === ts.SyntaxKind.MethodDeclaration)) {
            // We still need to check the parameters and return type
            var functionNode = node; //tslint:disable-line
            var invalidNodes = checkIgnoreLocalFunctionNode(functionNode, ctx, checkNode);
            // invalidNodes.forEach((n) => reportInvalidNodes(n, ctx, failureString));
            reportInvalidNodes(invalidNodes, ctx, failureString);
            // Now skip this whole branch
            return;
        }
        // Check the node
        reportInvalidNodes(checkNode(node, ctx), ctx, failureString);
        // Use return becuase performance hints docs say it optimizes the function using tail-call recursion
        return ts.forEachChild(node, cb);
    }
}
exports.walk = walk;
function reportInvalidNodes(invalidNodes, ctx, failureString) {
    invalidNodes.forEach(function (invalidNode) { return ctx.addFailureAtNode(invalidNode.node, failureString, invalidNode.replacement); });
}
exports.reportInvalidNodes = reportInvalidNodes;
function checkIgnoreLocalFunctionNode(functionNode, ctx, checkNode) {
    var invalidNodes = [];
    // Check either the parameter's explicit type if it has one, or itself for implict type
    for (var _i = 0, _a = functionNode.parameters.map(function (p) { return p.type ? p.type : p; }); _i < _a.length; _i++) {
        var n = _a[_i];
        var invalidCheckNodes = checkNode(n, ctx);
        Eif (invalidCheckNodes) {
            invalidNodes = invalidNodes.concat.apply(invalidNodes, invalidCheckNodes);
        }
    }
    // Check the return type
    if (functionNode.type) {
        var invalidCheckNodes = checkNode(functionNode.type, ctx);
        Eif (invalidCheckNodes) {
            invalidNodes = invalidNodes.concat.apply(invalidNodes, invalidCheckNodes);
        }
    }
    return invalidNodes;
}
exports.checkIgnoreLocalFunctionNode = checkIgnoreLocalFunctionNode;
function shouldIgnorePrefix(node, options, sourceFile) {
    // Check ignore-prefix for VariableDeclaration, PropertySignature, TypeAliasDeclaration, Parameter
    if (options.ignorePrefix) {
        if (node && (node.kind === ts.SyntaxKind.VariableDeclaration
            || node.kind === ts.SyntaxKind.Parameter
            || node.kind === ts.SyntaxKind.PropertySignature
            || node.kind === ts.SyntaxKind.TypeAliasDeclaration)) {
            var variableDeclarationNode = node;
            if (variableDeclarationNode.name.getText(sourceFile).substr(0, options.ignorePrefix.length) === options.ignorePrefix) {
                return true;
            }
        }
    }
    return false;
}
exports.shouldIgnorePrefix = shouldIgnorePrefix;