.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noNullUndefinedUnionRule.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 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.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
30     };
31     /* tslint:disable:object-literal-sort-keys */
32     Rule.metadata = {
33         ruleName: "no-null-undefined-union",
34         description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Disallows explicitly declared or implicitly returned union types with both `null` and\n            `undefined` as members.\n        "], ["\n            Disallows explicitly declared or implicitly returned union types with both \\`null\\` and\n            \\`undefined\\` as members.\n        "]))),
35         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            A union type that includes both `null` and `undefined` is either redundant or fragile.\n            Enforcing the choice between the two allows the `triple-equals` rule to exist without\n            exceptions, and is essentially a more flexible version of the `no-null-keyword` rule.\n            Optional parameters are not considered to have the type `undefined`.\n        "], ["\n            A union type that includes both \\`null\\` and \\`undefined\\` is either redundant or fragile.\n            Enforcing the choice between the two allows the \\`triple-equals\\` rule to exist without\n            exceptions, and is essentially a more flexible version of the \\`no-null-keyword\\` rule.\n            Optional parameters are not considered to have the type \\`undefined\\`.\n        "]))),
36         optionsDescription: "Not configurable.",
37         options: null,
38         optionExamples: [true],
39         type: "functionality",
40         typescriptOnly: true,
41         requiresTypeInfo: true,
42     };
43     /* tslint:enable:object-literal-sort-keys */
44     Rule.FAILURE_STRING = "Union type cannot include both 'null' and 'undefined'.";
45     return Rule;
46 }(Lint.Rules.TypedRule));
47 exports.Rule = Rule;
48 function walk(ctx, tc) {
49     return ts.forEachChild(ctx.sourceFile, function cb(node) {
50         var type = getType(node, tc);
51         if (type !== undefined && isNullUndefinedUnion(type)) {
52             ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
53         }
54         return ts.forEachChild(node, cb);
55     });
56 }
57 function getType(node, tc) {
58     if (tsutils_1.isUnionTypeNode(node)) {
59         return tc.getTypeAtLocation(node);
60     }
61     else if (tsutils_1.isSignatureDeclaration(node) && node.type === undefined) {
62         // Explicit types should be handled by the first case.
63         var signature = tc.getSignatureFromDeclaration(node);
64         return signature === undefined ? undefined : signature.getReturnType();
65     }
66     else {
67         return undefined;
68     }
69 }
70 function isNullUndefinedUnion(type) {
71     if (tsutils_1.isTypeReference(type) && type.typeArguments !== undefined) {
72         return type.typeArguments.some(isNullUndefinedUnion);
73     }
74     if (tsutils_1.isUnionType(type)) {
75         var hasNull = false;
76         var hasUndefined = false;
77         for (var _i = 0, _a = type.types; _i < _a.length; _i++) {
78             var subType = _a[_i];
79             hasNull = hasNull || subType.getFlags() === ts.TypeFlags.Null;
80             hasUndefined = hasUndefined || subType.getFlags() === ts.TypeFlags.Undefined;
81             if (hasNull && hasUndefined) {
82                 return true;
83             }
84         }
85     }
86     return false;
87 }
88 var templateObject_1, templateObject_2;