.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / invalidVoidRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2019 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 _a, _b, _c;
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);
27     function Rule() {
28         return _super !== null && _super.apply(this, arguments) || this;
29     }
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]),
34         });
35     };
36     Rule.prototype.getAllowGenerics = function (rawArgument) {
37         if (rawArgument == undefined) {
38             return true;
39         }
40         var allowGenerics = rawArgument[OPTION_ALLOW_GENERICS];
41         return allowGenerics instanceof Array ? new Set(allowGenerics) : Boolean(allowGenerics);
42     };
43     /* tslint:disable:object-literal-sort-keys */
44     Rule.metadata = {
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."]))),
48         hasFix: false,
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),
50         options: {
51             type: "object",
52             properties: (_a = {},
53                 _a[OPTION_ALLOW_GENERICS] = {
54                     oneOf: [
55                         { type: "boolean" },
56                         { type: "array", items: { type: "string" }, minLength: 1 },
57                     ],
58                 },
59                 _a),
60             additionalProperties: false,
61         },
62         optionExamples: [
63             true,
64             [true, (_b = {}, _b[OPTION_ALLOW_GENERICS] = false, _b)],
65             [true, (_c = {}, _c[OPTION_ALLOW_GENERICS] = ["Promise", "PromiseLike"], _c)],
66         ],
67         type: "maintainability",
68         typescriptOnly: true,
69     };
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";
75     };
76     return Rule;
77 }(Lint.Rules.AbstractRule));
78 exports.Rule = Rule;
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,
97 ]);
98 function walk(ctx) {
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);
105     };
106     var getTypeReferenceFailure = function (node) {
107         if (!(ctx.options.allowGenerics instanceof Set)) {
108             return ctx.options.allowGenerics ? undefined : defaultFailureString;
109         }
110         var genericName = getGenericReferenceName(node);
111         return ctx.options.allowGenerics.has(genericName)
112             ? undefined
113             : Rule.FAILURE_WRONG_GENERIC(genericName);
114     };
115     var checkTypeReference = function (parent, node) {
116         var failure = getTypeReferenceFailure(parent);
117         if (failure !== undefined) {
118             ctx.addFailureAtNode(node, failure);
119         }
120     };
121     var isParentGenericReference = function (parent, node) {
122         if (tsutils.isTypeReferenceNode(parent)) {
123             return true;
124         }
125         return (tsutils.isNewExpression(parent) &&
126             parent.typeArguments !== undefined &&
127             ts.isTypeNode(node) &&
128             parent.typeArguments.indexOf(node) !== -1);
129     };
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);
134             }
135             else {
136                 ctx.addFailureAtNode(node, defaultFailureString);
137             }
138         }
139         ts.forEachChild(node, cb);
140     });
141 }
142 var templateObject_1, templateObject_2, templateObject_3;