4 * Copyright 2013 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 // with due reference to https://github.com/Microsoft/TypeScript/blob/7813121c4d77e50aad0eed3152ef1f1156c7b574/scripts/tslint/noNullRule.ts
21 var tsutils_1 = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var Rule = /** @class */ (function (_super) {
25 tslib_1.__extends(Rule, _super);
27 return _super !== null && _super.apply(this, arguments) || this;
29 Rule.prototype.apply = function (sourceFile) {
30 return this.applyWithFunction(sourceFile, walk);
32 /* tslint:disable:object-literal-sort-keys */
34 ruleName: "no-null-keyword",
35 description: "Disallows use of the `null` keyword literal.",
36 rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Instead of having the dual concepts of `null` and`undefined` in a codebase,\n this rule ensures that only `undefined` is used.\n\n JavaScript originally intended `undefined` to refer to a value that doesn't yet exist,\n while `null` was meant to refer to a value that does exist but points to nothing.\n That's confusing.\n `undefined` is the default value when object members don't exist, and is the return value\n for newer native collection APIs such as `Map.get` when collection values don't exist.\n\n ```\n const myObject = {};\n myObject.doesNotExist; // undefined\n ```\n\n ```\n const myMap = new Map<string, number>();\n myMap.get(\"doesNotExist\"); // undefined\n ```\n\n To remove confusion over the two similar values, it's better to stick with just `undefined`.\n "], ["\n Instead of having the dual concepts of \\`null\\` and\\`undefined\\` in a codebase,\n this rule ensures that only \\`undefined\\` is used.\n\n JavaScript originally intended \\`undefined\\` to refer to a value that doesn't yet exist,\n while \\`null\\` was meant to refer to a value that does exist but points to nothing.\n That's confusing.\n \\`undefined\\` is the default value when object members don't exist, and is the return value\n for newer native collection APIs such as \\`Map.get\\` when collection values don't exist.\n\n \\`\\`\\`\n const myObject = {};\n myObject.doesNotExist; // undefined\n \\`\\`\\`\n\n \\`\\`\\`\n const myMap = new Map<string, number>();\n myMap.get(\"doesNotExist\"); // undefined\n \\`\\`\\`\n\n To remove confusion over the two similar values, it's better to stick with just \\`undefined\\`.\n "]))),
37 optionsDescription: "Not configurable.",
39 optionExamples: [true],
40 type: "functionality",
41 typescriptOnly: false,
44 /* tslint:enable:object-literal-sort-keys */
45 Rule.FAILURE_STRING = "Use 'undefined' instead of 'null'";
47 }(Lint.Rules.AbstractRule));
50 return ts.forEachChild(ctx.sourceFile, cb);
52 if (tsutils_1.isTypeNodeKind(node.kind)) {
53 return; // skip type nodes
55 if (node.kind !== ts.SyntaxKind.NullKeyword) {
56 return ts.forEachChild(node, cb);
58 var parent = node.parent;
60 if (tsutils_1.isBinaryExpression(parent)) {
61 eq = Lint.getEqualsKind(parent.operatorToken);
63 if (eq === undefined) {
64 ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
66 else if (!eq.isStrict) {
67 ctx.addFailureAtNode(node, Rule.FAILURE_STRING, Lint.Replacement.replaceNode(node, "undefined", ctx.sourceFile));