tripleEqualsRule.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. var tsutils_1 = require("tsutils");
  21. var ts = require("typescript");
  22. var Lint = require("../index");
  23. var OPTION_ALLOW_NULL_CHECK = "allow-null-check";
  24. var OPTION_ALLOW_UNDEFINED_CHECK = "allow-undefined-check";
  25. var Rule = /** @class */ (function (_super) {
  26. tslib_1.__extends(Rule, _super);
  27. function Rule() {
  28. return _super !== null && _super.apply(this, arguments) || this;
  29. }
  30. Rule.prototype.apply = function (sourceFile) {
  31. return this.applyWithFunction(sourceFile, walk, {
  32. allowNull: this.ruleArguments.indexOf(OPTION_ALLOW_NULL_CHECK) !== -1,
  33. allowUndefined: this.ruleArguments.indexOf(OPTION_ALLOW_UNDEFINED_CHECK) !== -1,
  34. });
  35. };
  36. /* tslint:disable:object-literal-sort-keys */
  37. Rule.metadata = {
  38. ruleName: "triple-equals",
  39. description: "Requires `===` and `!==` in place of `==` and `!=`.",
  40. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Two arguments may be optionally provided:\n\n * `\"allow-null-check\"` allows `==` and `!=` when comparing to `null`.\n * `\"allow-undefined-check\"` allows `==` and `!=` when comparing to `undefined`."], ["\n Two arguments may be optionally provided:\n\n * \\`\"allow-null-check\"\\` allows \\`==\\` and \\`!=\\` when comparing to \\`null\\`.\n * \\`\"allow-undefined-check\"\\` allows \\`==\\` and \\`!=\\` when comparing to \\`undefined\\`."]))),
  41. options: {
  42. type: "array",
  43. items: {
  44. type: "string",
  45. enum: [OPTION_ALLOW_NULL_CHECK, OPTION_ALLOW_UNDEFINED_CHECK],
  46. },
  47. minLength: 0,
  48. maxLength: 2,
  49. },
  50. optionExamples: [
  51. true,
  52. [true, "allow-null-check"],
  53. [true, "allow-undefined-check"],
  54. ],
  55. type: "functionality",
  56. typescriptOnly: false,
  57. };
  58. /* tslint:enable:object-literal-sort-keys */
  59. Rule.EQ_FAILURE_STRING = "== should be ===";
  60. Rule.NEQ_FAILURE_STRING = "!= should be !==";
  61. return Rule;
  62. }(Lint.Rules.AbstractRule));
  63. exports.Rule = Rule;
  64. function walk(ctx) {
  65. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  66. if (tsutils_1.isBinaryExpression(node)) {
  67. if ((node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken ||
  68. node.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsToken) &&
  69. !(isExpressionAllowed(node.right, ctx.options) || isExpressionAllowed(node.left, ctx.options))) {
  70. ctx.addFailureAtNode(node.operatorToken, node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken
  71. ? Rule.EQ_FAILURE_STRING
  72. : Rule.NEQ_FAILURE_STRING);
  73. }
  74. }
  75. return ts.forEachChild(node, cb);
  76. });
  77. }
  78. function isExpressionAllowed(node, options) {
  79. if (node.kind === ts.SyntaxKind.NullKeyword) {
  80. return options.allowNull;
  81. }
  82. return options.allowUndefined &&
  83. node.kind === ts.SyntaxKind.Identifier &&
  84. node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword;
  85. }
  86. var templateObject_1;