noNullKeywordRule.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Instead of having the dual concepts of \\`null\\` and\\`undefined\\` in a codebase,\n this rule ensures that only \\`undefined\\` is used."]))),
  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;