All files readonlyArrayRule.js

92% Statements 46/50
80.95% Branches 34/42
83.33% Functions 10/12
95.74% Lines 45/47
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  1x 1x     1x 1x 1x 1x     1x 1x 1x 1x 1x 1x   13x   1x 13x   1x   1x   950x 950x 950x       950x   79x 15x   64x 64x 7x 7x   57x 57x 57x 57x     64x 64x   871x     950x     86x 86x 8x   78x 34x 16x 16x 16x                         16x       926x    
"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
var Lint = require("tslint");
var Shared = require("./readonly-shared");
var Rule = (function (_super) {
    __extends(Rule, _super);
    function Rule() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Rule.prototype.apply = function (sourceFile) {
        return this.applyWithFunction(sourceFile, function (ctx) { return Shared.walk(ctx, checkNode, "Only ReadonlyArray allowed."); }, Shared.parseOptions(this.ruleArguments));
    };
    return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
function checkNode(node, ctx) {
    var explicitTypeFailures = checkArrayTypeOrReference(node, ctx);
    var implicitTypeFailures = checkVariableOrParameterImplicitType(node, ctx);
    return explicitTypeFailures.concat(implicitTypeFailures);
}
function checkArrayTypeOrReference(node, ctx) {
    // We need to check both shorthand syntax "number[]" and type reference "Array<number>"
    if (node.kind === ts.SyntaxKind.ArrayType
        || (node.kind === ts.SyntaxKind.TypeReference && node.typeName.getText(ctx.sourceFile) === "Array")) {
        if (node.parent && Shared.shouldIgnorePrefix(node.parent, ctx.options, ctx.sourceFile)) {
            return [];
        }
        var typeArgument = "T";
        if (node.kind === ts.SyntaxKind.ArrayType) {
            var typeNode = node;
            typeArgument = typeNode.elementType.getFullText(ctx.sourceFile).trim();
        }
        else Eif (node.kind === ts.SyntaxKind.TypeReference) {
            var typeNode = node;
            Eif (typeNode.typeArguments) {
                typeArgument = typeNode.typeArguments[0].getFullText(ctx.sourceFile).trim();
            }
        }
        var length_1 = node.getWidth(ctx.sourceFile);
        return [Shared.createInvalidNode(node, new Lint.Replacement(node.end - length_1, length_1, "ReadonlyArray<" + typeArgument + ">"))];
    }
    return [];
}
function checkVariableOrParameterImplicitType(node, ctx) {
    if (node.kind === ts.SyntaxKind.VariableDeclaration || node.kind === ts.SyntaxKind.Parameter
        || node.kind === ts.SyntaxKind.PropertyDeclaration) {
        // The initializer is used to set and implicit type
        var varOrParamNode = node;
        if (Shared.shouldIgnorePrefix(node, ctx.options, ctx.sourceFile)) {
            return [];
        }
        if (!varOrParamNode.type) {
            if (varOrParamNode.initializer && varOrParamNode.initializer.kind === ts.SyntaxKind.ArrayLiteralExpression) {
                var length_2 = varOrParamNode.name.getWidth(ctx.sourceFile);
                var nameText = varOrParamNode.name.getText(ctx.sourceFile);
                var typeArgument = "any";
                // Not sure it is a good idea to guess what the element types are...
                // const arrayLiteralNode = varOrParamNode.initializer as ts.ArrayLiteralExpression;
                // if (arrayLiteralNode.elements.length > 0) {
                //   const element = arrayLiteralNode.elements[0];
                //   if (element.kind === ts.SyntaxKind.NumericLiteral) {
                //     typeArgument = "number";
                //   } else if (element.kind === ts.SyntaxKind.StringLiteral) {
                //     typeArgument = "string";
                //   } else if (element.kind === ts.SyntaxKind.TrueKeyword || element.kind === ts.SyntaxKind.FalseKeyword) {
                //     typeArgument = "boolean";
                //   }
                // }
                return [Shared.createInvalidNode(varOrParamNode.name, new Lint.Replacement(varOrParamNode.name.end - length_2, length_2, nameText + ": ReadonlyArray<" + typeArgument + ">"))];
            }
        }
    }
    return [];
}