4 * Copyright 2018 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 Rule.prototype.applyWithProgram = function (sourceFile, program) {
29 return this.applyWithWalker(new NoInferredEmptyObjectTypeRule(sourceFile, this.ruleName, program.getTypeChecker()));
31 /* tslint:disable:object-literal-sort-keys */
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.",
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",
41 requiresTypeInfo: true,
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";
47 }(Lint.Rules.TypedRule));
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;
56 NoInferredEmptyObjectTypeRule.prototype.walk = function (sourceFile) {
58 var cb = function (node) {
59 if (node.kind === ts.SyntaxKind.CallExpression) {
60 _this.checkCallExpression(node);
62 else if (node.kind === ts.SyntaxKind.NewExpression) {
63 _this.checkNewExpression(node);
65 return ts.forEachChild(node, cb);
67 return ts.forEachChild(sourceFile, cb);
69 NoInferredEmptyObjectTypeRule.prototype.checkNewExpression = function (node) {
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);
80 NoInferredEmptyObjectTypeRule.prototype.checkCallExpression = function (node) {
81 if (node.typeArguments !== undefined) {
84 var callSig = this.checker.getResolvedSignature(node);
85 if (callSig === undefined) {
88 var retType = this.checker.getReturnTypeOfSignature(callSig);
89 if (tsutils_1.isObjectType(retType) && this.isEmptyObjectInterface(retType)) {
90 this.addFailureAtNode(node, Rule.EMPTY_INTERFACE_FUNCTION);
93 NoInferredEmptyObjectTypeRule.prototype.isEmptyObjectInterface = function (objType) {
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);
104 return NoInferredEmptyObjectTypeRule;
105 }(Lint.AbstractWalker));
106 var templateObject_1;