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");
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.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
31 /* tslint:disable:object-literal-sort-keys */
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.",
38 optionExamples: [true],
39 type: "functionality",
41 requiresTypeInfo: true,
43 /* tslint:enable:object-literal-sort-keys */
44 Rule.FAILURE_STRING = "Union type cannot include both 'null' and 'undefined'.";
46 }(Lint.Rules.TypedRule));
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);
54 return ts.forEachChild(node, cb);
57 function getType(node, tc) {
58 if (tsutils_1.isUnionTypeNode(node)) {
59 return tc.getTypeAtLocation(node);
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();
70 function isNullUndefinedUnion(type) {
71 if (tsutils_1.isTypeReference(type) && type.typeArguments !== undefined) {
72 return type.typeArguments.some(isNullUndefinedUnion);
74 if (tsutils_1.isUnionType(type)) {
76 var hasUndefined = false;
77 for (var _i = 0, _a = type.types; _i < _a.length; _i++) {
79 hasNull = hasNull || subType.getFlags() === ts.TypeFlags.Null;
80 hasUndefined = hasUndefined || subType.getFlags() === ts.TypeFlags.Undefined;
81 if (hasNull && hasUndefined) {
88 var templateObject_1, templateObject_2;