unifiedSignaturesRule.js 13.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
"use strict";
/**
 * @license
 * Copyright 2017 Palantir Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Rule = void 0;
var tslib_1 = require("tslib");
var utils = require("tsutils");
var ts = require("typescript");
var Lint = require("../index");
var utils_1 = require("../utils");
var adjacentOverloadSignaturesRule_1 = require("./adjacentOverloadSignaturesRule");
var Rule = /** @class */ (function (_super) {
    tslib_1.__extends(Rule, _super);
    function Rule() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    /* tslint:enable:object-literal-sort-keys */
    Rule.FAILURE_STRING_OMITTING_SINGLE_PARAMETER = function (otherLine) {
        return Rule.FAILURE_STRING_START(otherLine) + " with an optional parameter.";
    };
    Rule.FAILURE_STRING_OMITTING_REST_PARAMETER = function (otherLine) {
        return Rule.FAILURE_STRING_START(otherLine) + " with a rest parameter.";
    };
    Rule.FAILURE_STRING_SINGLE_PARAMETER_DIFFERENCE = function (otherLine, type1, type2) {
        return Rule.FAILURE_STRING_START(otherLine) + " taking `" + type1 + " | " + type2 + "`.";
    };
    Rule.FAILURE_STRING_START = function (otherLine) {
        // For only 2 overloads we don't need to specify which is the other one.
        var overloads = otherLine === undefined
            ? "These overloads"
            : "This overload and the one on line " + otherLine;
        return overloads + " can be combined into one signature";
    };
    Rule.prototype.apply = function (sourceFile) {
        return this.applyWithFunction(sourceFile, walk);
    };
    /* tslint:disable:object-literal-sort-keys */
    Rule.metadata = {
        ruleName: "unified-signatures",
        description: "Warns for any two overloads that could be unified into one by using a union or an optional/rest parameter.",
        optionsDescription: "Not configurable.",
        options: null,
        optionExamples: [true],
        type: "typescript",
        typescriptOnly: true,
    };
    return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
function walk(ctx) {
    var sourceFile = ctx.sourceFile;
    checkStatements(sourceFile.statements);
    return ts.forEachChild(sourceFile, function cb(node) {
        switch (node.kind) {
            case ts.SyntaxKind.ModuleBlock:
                checkStatements(node.statements);
                break;
            case ts.SyntaxKind.InterfaceDeclaration:
            case ts.SyntaxKind.ClassDeclaration: {
                var _a = node, members = _a.members, typeParameters = _a.typeParameters;
                checkMembers(members, typeParameters);
                break;
            }
            case ts.SyntaxKind.TypeLiteral:
                checkMembers(node.members);
        }
        return ts.forEachChild(node, cb);
    });
    function checkStatements(statements) {
        addFailures(checkOverloads(statements, undefined, function (statement) {
            if (utils.isFunctionDeclaration(statement)) {
                var body = statement.body, name = statement.name;
                return body === undefined && name !== undefined
                    ? { signature: statement, key: name.text }
                    : undefined;
            }
            else {
                return undefined;
            }
        }));
    }
    function checkMembers(members, typeParameters) {
        addFailures(checkOverloads(members, typeParameters, function (member) {
            switch (member.kind) {
                case ts.SyntaxKind.CallSignature:
                case ts.SyntaxKind.ConstructSignature:
                case ts.SyntaxKind.MethodSignature:
                    break;
                case ts.SyntaxKind.MethodDeclaration:
                case ts.SyntaxKind.Constructor:
                    if (member.body !==
                        undefined) {
                        return undefined;
                    }
                    break;
                default:
                    return undefined;
            }
            var signature = member;
            var key = adjacentOverloadSignaturesRule_1.getOverloadKey(signature);
            return key === undefined ? undefined : { signature: signature, key: key };
        }));
    }
    function addFailures(failures) {
        for (var _i = 0, failures_1 = failures; _i < failures_1.length; _i++) {
            var failure = failures_1[_i];
            var unify = failure.unify, only2 = failure.only2;
            switch (unify.kind) {
                case "single-parameter-difference": {
                    var p0 = unify.p0, p1 = unify.p1;
                    var lineOfOtherOverload = only2 ? undefined : getLine(p0.getStart());
                    ctx.addFailureAtNode(p1, Rule.FAILURE_STRING_SINGLE_PARAMETER_DIFFERENCE(lineOfOtherOverload, typeText(p0), typeText(p1)));
                    break;
                }
                case "extra-parameter": {
                    var extraParameter = unify.extraParameter, otherSignature = unify.otherSignature;
                    var lineOfOtherOverload = only2 ? undefined : getLine(otherSignature.pos);
                    ctx.addFailureAtNode(extraParameter, extraParameter.dotDotDotToken !== undefined
                        ? Rule.FAILURE_STRING_OMITTING_REST_PARAMETER(lineOfOtherOverload)
                        : Rule.FAILURE_STRING_OMITTING_SINGLE_PARAMETER(lineOfOtherOverload));
                }
            }
        }
    }
    function getLine(pos) {
        return ts.getLineAndCharacterOfPosition(sourceFile, pos).line + 1;
    }
}
function checkOverloads(signatures, typeParameters, getOverload) {
    var result = [];
    var isTypeParameter = getIsTypeParameter(typeParameters);
    for (var _i = 0, _a = collectOverloads(signatures, getOverload); _i < _a.length; _i++) {
        var overloads = _a[_i];
        if (overloads.length === 2) {
            var unify = compareSignatures(overloads[0], overloads[1], isTypeParameter);
            if (unify !== undefined) {
                result.push({ unify: unify, only2: true });
            }
        }
        else {
            forEachPair(overloads, function (a, b) {
                var unify = compareSignatures(a, b, isTypeParameter);
                if (unify !== undefined) {
                    result.push({ unify: unify, only2: false });
                }
            });
        }
    }
    return result;
}
function compareSignatures(a, b, isTypeParameter) {
    if (!signaturesCanBeUnified(a, b, isTypeParameter)) {
        return undefined;
    }
    return a.parameters.length === b.parameters.length
        ? signaturesDifferBySingleParameter(a.parameters, b.parameters)
        : signaturesDifferByOptionalOrRestParameter(a.parameters, b.parameters);
}
function signaturesCanBeUnified(a, b, isTypeParameter) {
    // Must return the same type.
    return (typesAreEqual(a.type, b.type) &&
        // Must take the same type parameters.
        utils_1.arraysAreEqual(a.typeParameters, b.typeParameters, typeParametersAreEqual) &&
        // If one uses a type parameter (from outside) and the other doesn't, they shouldn't be joined.
        signatureUsesTypeParameter(a, isTypeParameter) ===
            signatureUsesTypeParameter(b, isTypeParameter));
}
/** Detect `a(x: number, y: number, z: number)` and `a(x: number, y: string, z: number)`. */
function signaturesDifferBySingleParameter(types1, types2) {
    var index = getIndexOfFirstDifference(types1, types2, parametersAreEqual);
    if (index === undefined) {
        return undefined;
    }
    // If remaining arrays are equal, the signatures differ by just one parameter type
    if (!utils_1.arraysAreEqual(types1.slice(index + 1), types2.slice(index + 1), parametersAreEqual)) {
        return undefined;
    }
    var a = types1[index];
    var b = types2[index];
    // Can unify `a?: string` and `b?: number`. Can't unify `...args: string[]` and `...args: number[]`.
    // See https://github.com/Microsoft/TypeScript/issues/5077
    return parametersHaveEqualSigils(a, b) && a.dotDotDotToken === undefined
        ? { kind: "single-parameter-difference", p0: a, p1: b }
        : undefined;
}
/**
 * Detect `a(): void` and `a(x: number): void`.
 * Returns the parameter declaration (`x: number` in this example) that should be optional/rest, and overload it's a part of.
 */
function signaturesDifferByOptionalOrRestParameter(sig1, sig2) {
    var minLength = Math.min(sig1.length, sig2.length);
    var longer = sig1.length < sig2.length ? sig2 : sig1;
    var shorter = sig1.length < sig2.length ? sig1 : sig2;
    // If one is has 2+ parameters more than the other, they must all be optional/rest.
    // Differ by optional parameters: f() and f(x), f() and f(x, ?y, ...z)
    // Not allowed: f() and f(x, y)
    for (var i = minLength + 1; i < longer.length; i++) {
        if (!parameterMayBeMissing(longer[i])) {
            return undefined;
        }
    }
    for (var i = 0; i < minLength; i++) {
        if (!typesAreEqual(sig1[i].type, sig2[i].type)) {
            return undefined;
        }
    }
    if (minLength > 0 && shorter[minLength - 1].dotDotDotToken !== undefined) {
        return undefined;
    }
    return {
        extraParameter: longer[longer.length - 1],
        kind: "extra-parameter",
        otherSignature: shorter,
    };
}
/** Given type parameters, returns a function to test whether a type is one of those parameters. */
function getIsTypeParameter(typeParameters) {
    if (typeParameters === undefined) {
        return function () { return false; };
    }
    var set = new Set();
    for (var _i = 0, typeParameters_1 = typeParameters; _i < typeParameters_1.length; _i++) {
        var t = typeParameters_1[_i];
        set.add(t.getText());
    }
    return function (typeName) { return set.has(typeName); };
}
/** True if any of the outer type parameters are used in a signature. */
function signatureUsesTypeParameter(sig, isTypeParameter) {
    return sig.parameters.some(function (p) { return p.type !== undefined && typeContainsTypeParameter(p.type) === true; });
    function typeContainsTypeParameter(type) {
        if (utils.isTypeReferenceNode(type)) {
            var typeName = type.typeName;
            if (typeName.kind === ts.SyntaxKind.Identifier && isTypeParameter(typeName.text)) {
                return true;
            }
        }
        return ts.forEachChild(type, typeContainsTypeParameter);
    }
}
/**
 * Given all signatures, collects an array of arrays of signatures which are all overloads.
 * Does not rely on overloads being adjacent. This is similar to code in adjacentOverloadSignaturesRule.ts, but not the same.
 */
function collectOverloads(nodes, getOverload) {
    var map = new Map();
    for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
        var sig = nodes_1[_i];
        var overload = getOverload(sig);
        if (overload === undefined) {
            continue;
        }
        var signature = overload.signature, key = overload.key;
        var overloads = map.get(key);
        if (overloads !== undefined) {
            overloads.push(signature);
        }
        else {
            map.set(key, [signature]);
        }
    }
    return Array.from(map.values());
}
function parametersAreEqual(a, b) {
    return parametersHaveEqualSigils(a, b) && typesAreEqual(a.type, b.type);
}
/** True for optional/rest parameters. */
function parameterMayBeMissing(p) {
    return p.dotDotDotToken !== undefined || p.questionToken !== undefined;
}
/** False if one is optional and the other isn't, or one is a rest parameter and the other isn't. */
function parametersHaveEqualSigils(a, b) {
    return ((a.dotDotDotToken !== undefined) === (b.dotDotDotToken !== undefined) &&
        (a.questionToken !== undefined) === (b.questionToken !== undefined));
}
function typeParametersAreEqual(a, b) {
    return a.name.text === b.name.text && typesAreEqual(a.constraint, b.constraint);
}
function typesAreEqual(a, b) {
    // TODO: Could traverse AST so that formatting differences don't affect this.
    return a === b || (a !== undefined && b !== undefined && a.getText() === b.getText());
}
/** Returns the first index where `a` and `b` differ. */
function getIndexOfFirstDifference(a, b, equal) {
    for (var i = 0; i < a.length && i < b.length; i++) {
        if (!equal(a[i], b[i])) {
            return i;
        }
    }
    return undefined;
}
/** Calls `action` for every pair of values in `values`. */
function forEachPair(values, action) {
    for (var i = 0; i < values.length; i++) {
        for (var j = i + 1; j < values.length; j++) {
            var result = action(values[i], values[j]);
            if (result !== undefined) {
                return result;
            }
        }
    }
    return undefined;
}
function typeText(_a) {
    var type = _a.type;
    return type === undefined ? "any" : type.getText();
}