text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  /*
   * SonarTS
   * Copyright (C) 2017-2019 SonarSource SA
   * mailto:info AT sonarsource DOT com
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with this program; if not, write to the Free Software Foundation,
   * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   */
  const tslint = require("tslint");
  const ts = require("typescript");
  const areEquivalent_1 = require("../utils/areEquivalent");
  const sonarAnalysis_1 = require("../utils/sonarAnalysis");
  class Rule extends tslint.Rules.AbstractRule {
      static formatMessage(operator) {
          return `Correct one of the identical sub-expressions on both sides of operator "${operator}"`;
      }
      apply(sourceFile) {
          return new Visitor(this.getOptions().ruleName).visit(sourceFile).getIssues();
      }
  }
  Rule.metadata = {
      ruleName: "no-identical-expressions",
      description: "Identical expressions should not be used on both sides of a binary operator",
      rationale: tslint.Utils.dedent `
        Using the same value on either side of a binary operator is almost always a
        mistake. In the case of logical operators, it is either a copy/paste error and therefore a
        bug, or it is simply wasted code, and should be simplified. In the case of bitwise
        operators and most binary mathematical operators, having the same value on both sides of an
        operator yields predictable results, and should be simplified.
        This rule ignores *, +, and =.`,
      optionsDescription: "",
      options: null,
      rspecKey: "RSPEC-1764",
      type: "functionality",
      typescriptOnly: false,
  };
  exports.Rule = Rule;
  class Visitor extends sonarAnalysis_1.SonarRuleVisitor {
      visitBinaryExpression(node) {
          if (this.hasRelevantOperator(node) && !this.isOneOntoOneShifting(node) && areEquivalent_1.default(node.left, node.right)) {
              this.addIssue(node, Rule.formatMessage(node.operatorToken.getText()));
          }
          super.visitBinaryExpression(node);
      }
      hasRelevantOperator(node) {
          return (Visitor.RELEVANT_OPERATOR_TOKEN_KINDS.has(node.operatorToken.kind) ||
              (Visitor.EQUALITY_OPERATOR_TOKEN_KINDS.has(node.operatorToken.kind) && !this.hasIdentifierOperands(node)));
      }
      hasIdentifierOperands(node) {
          return node.left.kind === ts.SyntaxKind.Identifier;
      }
      isOneOntoOneShifting(node) {
          return (node.operatorToken.kind === ts.SyntaxKind.LessThanLessThanToken &&
              node.left.kind === ts.SyntaxKind.NumericLiteral &&
              node.left.getText() === "1");
      }
  }
  Visitor.EQUALITY_OPERATOR_TOKEN_KINDS = new Set([
      ts.SyntaxKind.EqualsEqualsToken,
      ts.SyntaxKind.EqualsEqualsEqualsToken,
      ts.SyntaxKind.ExclamationEqualsToken,
      ts.SyntaxKind.ExclamationEqualsEqualsToken,
  ]);
  // consider only binary expressions with these operators
  Visitor.RELEVANT_OPERATOR_TOKEN_KINDS = new Set([
      ts.SyntaxKind.AmpersandAmpersandToken,
      ts.SyntaxKind.BarBarToken,
      ts.SyntaxKind.SlashToken,
      ts.SyntaxKind.MinusToken,
      ts.SyntaxKind.LessThanLessThanToken,
      ts.SyntaxKind.GreaterThanGreaterThanToken,
      ts.SyntaxKind.LessThanToken,
      ts.SyntaxKind.LessThanEqualsToken,
      ts.SyntaxKind.GreaterThanToken,
      ts.SyntaxKind.GreaterThanEqualsToken,
  ]);
  //# sourceMappingURL=noIdenticalExpressionsRule.js.map