4 * Copyright 2019 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");
21 var tsutils = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var OPTION_ALLOW_GENERICS = "allow-generics";
25 var Rule = /** @class */ (function (_super) {
26 tslib_1.__extends(Rule, _super);
28 return _super !== null && _super.apply(this, arguments) || this;
30 Rule.prototype.apply = function (sourceFile) {
31 return this.applyWithFunction(sourceFile, walk, {
32 // tslint:disable-next-line:no-object-literal-type-assertion
33 allowGenerics: this.getAllowGenerics(this.ruleArguments[0]),
36 Rule.prototype.getAllowGenerics = function (rawArgument) {
37 if (rawArgument == undefined) {
40 var allowGenerics = rawArgument[OPTION_ALLOW_GENERICS];
41 return allowGenerics instanceof Array ? new Set(allowGenerics) : Boolean(allowGenerics);
43 /* tslint:disable:object-literal-sort-keys */
45 ruleName: "invalid-void",
46 description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows usage of `void` type outside of generic or return types.\n If `void` is used as return type, it shouldn't be a part of intersection/union type."], ["\n Disallows usage of \\`void\\` type outside of generic or return types.\n If \\`void\\` is used as return type, it shouldn't be a part of intersection/union type."]))),
47 rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n The `void` type means \"nothing\" or that a function does not return any value,\n in contra with implicit `undefined` type which means that a function returns a value `undefined`.\n So \"nothing\" cannot be mixed with any other types.\n If you need this - use `undefined` type instead."], ["\n The \\`void\\` type means \"nothing\" or that a function does not return any value,\n in contra with implicit \\`undefined\\` type which means that a function returns a value \\`undefined\\`.\n So \"nothing\" cannot be mixed with any other types.\n If you need this - use \\`undefined\\` type instead."]))),
49 optionsDescription: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n If `", "` is specified as `false`, then generic types will no longer be allowed to to be `void`.\n Alternately, provide an array of strings for `", "` to exclusively allow generic types by those names."], ["\n If \\`", "\\` is specified as \\`false\\`, then generic types will no longer be allowed to to be \\`void\\`.\n Alternately, provide an array of strings for \\`", "\\` to exclusively allow generic types by those names."])), OPTION_ALLOW_GENERICS, OPTION_ALLOW_GENERICS),
53 _a[OPTION_ALLOW_GENERICS] = {
56 { type: "array", items: { type: "string" }, minLength: 1 },
60 additionalProperties: false,
64 [true, (_b = {}, _b[OPTION_ALLOW_GENERICS] = false, _b)],
65 [true, (_c = {}, _c[OPTION_ALLOW_GENERICS] = ["Promise", "PromiseLike"], _c)],
67 type: "maintainability",
70 /* tslint:enable:object-literal-sort-keys */
71 Rule.FAILURE_STRING_ALLOW_GENERICS = "void is only valid as a return type or generic type variable";
72 Rule.FAILURE_STRING_NO_GENERICS = "void is only valid as a return type";
73 Rule.FAILURE_WRONG_GENERIC = function (genericName) {
74 return genericName + " may not have void as a type variable";
77 }(Lint.Rules.AbstractRule));
79 var failedKinds = new Set([
80 ts.SyntaxKind.PropertySignature,
81 ts.SyntaxKind.PropertyDeclaration,
82 ts.SyntaxKind.VariableDeclaration,
83 ts.SyntaxKind.TypeAliasDeclaration,
84 ts.SyntaxKind.IntersectionType,
85 ts.SyntaxKind.UnionType,
86 ts.SyntaxKind.Parameter,
87 ts.SyntaxKind.TypeParameter,
88 ts.SyntaxKind.AsExpression,
89 ts.SyntaxKind.TypeAssertionExpression,
90 ts.SyntaxKind.TypeOperator,
91 ts.SyntaxKind.ArrayType,
92 ts.SyntaxKind.MappedType,
93 ts.SyntaxKind.ConditionalType,
94 ts.SyntaxKind.TypeReference,
95 ts.SyntaxKind.NewExpression,
96 ts.SyntaxKind.CallExpression,
99 var defaultFailureString = ctx.options.allowGenerics
100 ? Rule.FAILURE_STRING_ALLOW_GENERICS
101 : Rule.FAILURE_STRING_NO_GENERICS;
102 var getGenericReferenceName = function (node) {
103 var rawName = tsutils.isNewExpression(node) ? node.expression : node.typeName;
104 return tsutils.isIdentifier(rawName) ? rawName.text : rawName.getText(ctx.sourceFile);
106 var getTypeReferenceFailure = function (node) {
107 if (!(ctx.options.allowGenerics instanceof Set)) {
108 return ctx.options.allowGenerics ? undefined : defaultFailureString;
110 var genericName = getGenericReferenceName(node);
111 return ctx.options.allowGenerics.has(genericName)
113 : Rule.FAILURE_WRONG_GENERIC(genericName);
115 var checkTypeReference = function (parent, node) {
116 var failure = getTypeReferenceFailure(parent);
117 if (failure !== undefined) {
118 ctx.addFailureAtNode(node, failure);
121 var isParentGenericReference = function (parent, node) {
122 if (tsutils.isTypeReferenceNode(parent)) {
125 return (tsutils.isNewExpression(parent) &&
126 parent.typeArguments !== undefined &&
127 ts.isTypeNode(node) &&
128 parent.typeArguments.indexOf(node) !== -1);
130 ts.forEachChild(ctx.sourceFile, function cb(node) {
131 if (node.kind === ts.SyntaxKind.VoidKeyword && failedKinds.has(node.parent.kind)) {
132 if (isParentGenericReference(node.parent, node)) {
133 checkTypeReference(node.parent, node);
136 ctx.addFailureAtNode(node, defaultFailureString);
139 ts.forEachChild(node, cb);
142 var templateObject_1, templateObject_2, templateObject_3;