noNullKeywordRule.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 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. // 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);
  26. function Rule() {
  27. return _super !== null && _super.apply(this, arguments) || this;
  28. }
  29. Rule.prototype.apply = function (sourceFile) {
  30. return this.applyWithFunction(sourceFile, walk);
  31. };
  32. /* tslint:disable:object-literal-sort-keys */
  33. Rule.metadata = {
  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.",
  38. options: null,
  39. optionExamples: [true],
  40. type: "functionality",
  41. typescriptOnly: false,
  42. hasFix: true,
  43. };
  44. /* tslint:enable:object-literal-sort-keys */
  45. Rule.FAILURE_STRING = "Use 'undefined' instead of 'null'";
  46. return Rule;
  47. }(Lint.Rules.AbstractRule));
  48. exports.Rule = Rule;
  49. function walk(ctx) {
  50. return ts.forEachChild(ctx.sourceFile, cb);
  51. function cb(node) {
  52. if (tsutils_1.isTypeNodeKind(node.kind)) {
  53. return; // skip type nodes
  54. }
  55. if (node.kind !== ts.SyntaxKind.NullKeyword) {
  56. return ts.forEachChild(node, cb);
  57. }
  58. var parent = node.parent;
  59. var eq;
  60. if (tsutils_1.isBinaryExpression(parent)) {
  61. eq = Lint.getEqualsKind(parent.operatorToken);
  62. }
  63. if (eq === undefined) {
  64. ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
  65. }
  66. else if (!eq.isStrict) {
  67. ctx.addFailureAtNode(node, Rule.FAILURE_STRING, Lint.Replacement.replaceNode(node, "undefined", ctx.sourceFile));
  68. }
  69. }
  70. }
  71. var templateObject_1;