.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / callableTypesRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
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);
25     function Rule() {
26         return _super !== null && _super.apply(this, arguments) || this;
27     }
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.";
31     };
32     Rule.prototype.apply = function (sourceFile) {
33         return this.applyWithFunction(sourceFile, walk);
34     };
35     /* tslint:disable:object-literal-sort-keys */
36     Rule.metadata = {
37         ruleName: "callable-types",
38         description: "An interface or literal type with just a call signature can be written as a function type.",
39         rationale: "style",
40         optionsDescription: "Not configurable.",
41         options: null,
42         type: "style",
43         typescriptOnly: true,
44         hasFix: true,
45     };
46     return Rule;
47 }(Lint.Rules.AbstractRule));
48 exports.Rule = Rule;
49 function walk(ctx) {
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)) &&
55                 // avoid bad parse
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));
62             }
63         }
64         return ts.forEachChild(node, cb);
65     });
66 }
67 /** True if there is no supertype or if the supertype is `Function`. */
68 function noSupertype(node) {
69     if (node.heritageClauses === undefined) {
70         return true;
71     }
72     if (node.heritageClauses.length !== 1) {
73         return false;
74     }
75     var expr = node.heritageClauses[0].types[0].expression;
76     return tsutils_1.isIdentifier(expr) && expr.text === "Function";
77 }
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 + ")";
85     }
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;
89         }
90         else {
91             return "type " + parent.name.text + " = " + suggestion;
92         }
93     }
94     return suggestion.endsWith(";") ? suggestion.slice(0, -1) : suggestion;
95 }
96 function shouldWrapSuggestion(parent) {
97     switch (parent.kind) {
98         case ts.SyntaxKind.UnionType:
99         case ts.SyntaxKind.IntersectionType:
100         case ts.SyntaxKind.ArrayType:
101             return true;
102         default:
103             return false;
104     }
105 }