4 * Copyright 2013 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var Rule = /** @class */ (function (_super) {
24 tslib_1.__extends(Rule, _super);
26 return _super !== null && _super.apply(this, arguments) || this;
28 /* tslint:enable:object-literal-sort-keys */
29 Rule.FAILURE_STRING_FACTORY = function (type, sigSuggestion) {
30 return type + " has only a call signature \u2014 use `" + sigSuggestion + "` instead.";
32 Rule.prototype.apply = function (sourceFile) {
33 return this.applyWithFunction(sourceFile, walk);
35 /* tslint:disable:object-literal-sort-keys */
37 ruleName: "callable-types",
38 description: "An interface or literal type with just a call signature can be written as a function type.",
40 optionsDescription: "Not configurable.",
47 }(Lint.Rules.AbstractRule));
50 return ts.forEachChild(ctx.sourceFile, function cb(node) {
51 if (((tsutils_1.isInterfaceDeclaration(node) && noSupertype(node)) || tsutils_1.isTypeLiteralNode(node)) &&
52 node.members.length === 1) {
53 var member = node.members[0];
54 if ((tsutils_1.isConstructSignatureDeclaration(member) || tsutils_1.isCallSignatureDeclaration(member)) &&
56 member.type !== undefined) {
57 var suggestion = renderSuggestion(member, node, ctx.sourceFile);
58 var fixStart = node.kind === ts.SyntaxKind.TypeLiteral
59 ? node.getStart(ctx.sourceFile)
60 : tsutils_1.getChildOfKind(node, ts.SyntaxKind.InterfaceKeyword).getStart(ctx.sourceFile);
61 ctx.addFailureAtNode(member, Rule.FAILURE_STRING_FACTORY(node.kind === ts.SyntaxKind.TypeLiteral ? "Type literal" : "Interface", suggestion), Lint.Replacement.replaceFromTo(fixStart, node.end, suggestion));
64 return ts.forEachChild(node, cb);
67 /** True if there is no supertype or if the supertype is `Function`. */
68 function noSupertype(node) {
69 if (node.heritageClauses === undefined) {
72 if (node.heritageClauses.length !== 1) {
75 var expr = node.heritageClauses[0].types[0].expression;
76 return tsutils_1.isIdentifier(expr) && expr.text === "Function";
78 function renderSuggestion(call, parent, sourceFile) {
79 var start = call.getStart(sourceFile);
80 var colonPos = call.type.pos - 1 - start;
81 var text = sourceFile.text.substring(start, call.end);
82 var suggestion = text.substr(0, colonPos) + " =>" + text.substr(colonPos + 1);
83 if (shouldWrapSuggestion(parent.parent)) {
84 suggestion = "(" + suggestion + ")";
86 if (parent.kind === ts.SyntaxKind.InterfaceDeclaration) {
87 if (parent.typeParameters !== undefined) {
88 return "type" + sourceFile.text.substring(parent.name.pos, parent.typeParameters.end + 1) + " = " + suggestion;
91 return "type " + parent.name.text + " = " + suggestion;
94 return suggestion.endsWith(";") ? suggestion.slice(0, -1) : suggestion;
96 function shouldWrapSuggestion(parent) {
97 switch (parent.kind) {
98 case ts.SyntaxKind.UnionType:
99 case ts.SyntaxKind.IntersectionType:
100 case ts.SyntaxKind.ArrayType: