.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noInferredEmptyObjectTypeRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 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     Rule.prototype.applyWithProgram = function (sourceFile, program) {
29         return this.applyWithWalker(new NoInferredEmptyObjectTypeRule(sourceFile, this.ruleName, program.getTypeChecker()));
30     };
31     /* tslint:disable:object-literal-sort-keys */
32     Rule.metadata = {
33         ruleName: "no-inferred-empty-object-type",
34         description: "Disallow type inference of {} (empty object type) at function and constructor call sites",
35         optionsDescription: "Not configurable.",
36         options: null,
37         optionExamples: [true],
38         rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Prior to TypeScript 3.4, generic type parameters for functions and constructors are inferred as\n            `{}` (the empty object type) by default when no type parameter is explicitly supplied or when\n            the compiler cannot infer a more specific type.\n            This is often undesirable as the call is meant to be of a more specific type.\n        "], ["\n            Prior to TypeScript 3.4, generic type parameters for functions and constructors are inferred as\n            \\`{}\\` (the empty object type) by default when no type parameter is explicitly supplied or when\n            the compiler cannot infer a more specific type.\n            This is often undesirable as the call is meant to be of a more specific type.\n        "]))),
39         type: "functionality",
40         typescriptOnly: true,
41         requiresTypeInfo: true,
42     };
43     /* tslint:enable:object-literal-sort-keys */
44     Rule.EMPTY_INTERFACE_INSTANCE = "Explicit type parameter needs to be provided to the constructor";
45     Rule.EMPTY_INTERFACE_FUNCTION = "Explicit type parameter needs to be provided to the function call";
46     return Rule;
47 }(Lint.Rules.TypedRule));
48 exports.Rule = Rule;
49 var NoInferredEmptyObjectTypeRule = /** @class */ (function (_super) {
50     tslib_1.__extends(NoInferredEmptyObjectTypeRule, _super);
51     function NoInferredEmptyObjectTypeRule(sourceFile, ruleName, checker) {
52         var _this = _super.call(this, sourceFile, ruleName, undefined) || this;
53         _this.checker = checker;
54         return _this;
55     }
56     NoInferredEmptyObjectTypeRule.prototype.walk = function (sourceFile) {
57         var _this = this;
58         var cb = function (node) {
59             if (node.kind === ts.SyntaxKind.CallExpression) {
60                 _this.checkCallExpression(node);
61             }
62             else if (node.kind === ts.SyntaxKind.NewExpression) {
63                 _this.checkNewExpression(node);
64             }
65             return ts.forEachChild(node, cb);
66         };
67         return ts.forEachChild(sourceFile, cb);
68     };
69     NoInferredEmptyObjectTypeRule.prototype.checkNewExpression = function (node) {
70         var _this = this;
71         if (node.typeArguments === undefined) {
72             var type = this.checker.getTypeAtLocation(node);
73             if (tsutils_1.isTypeReference(type) &&
74                 type.typeArguments !== undefined &&
75                 type.typeArguments.some(function (a) { return tsutils_1.isObjectType(a) && _this.isEmptyObjectInterface(a); })) {
76                 this.addFailureAtNode(node, Rule.EMPTY_INTERFACE_INSTANCE);
77             }
78         }
79     };
80     NoInferredEmptyObjectTypeRule.prototype.checkCallExpression = function (node) {
81         if (node.typeArguments !== undefined) {
82             return;
83         }
84         var callSig = this.checker.getResolvedSignature(node);
85         if (callSig === undefined) {
86             return;
87         }
88         var retType = this.checker.getReturnTypeOfSignature(callSig);
89         if (tsutils_1.isObjectType(retType) && this.isEmptyObjectInterface(retType)) {
90             this.addFailureAtNode(node, Rule.EMPTY_INTERFACE_FUNCTION);
91         }
92     };
93     NoInferredEmptyObjectTypeRule.prototype.isEmptyObjectInterface = function (objType) {
94         var _this = this;
95         return (tsutils_1.isObjectFlagSet(objType, ts.ObjectFlags.Anonymous) &&
96             objType.getProperties().length === 0 &&
97             objType.getNumberIndexType() === undefined &&
98             objType.getStringIndexType() === undefined &&
99             objType.getCallSignatures().every(function (signature) {
100                 var type = _this.checker.getReturnTypeOfSignature(signature);
101                 return tsutils_1.isObjectType(type) && _this.isEmptyObjectInterface(type);
102             }));
103     };
104     return NoInferredEmptyObjectTypeRule;
105 }(Lint.AbstractWalker));
106 var templateObject_1;