All files / src/rules many_parentheses.ts

94.66% Statements 195/206
88.73% Branches 63/71
100% Functions 9/9
94.66% Lines 195/206

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 2071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21101x 21101x 21101x 21101x 21101x 83283x 83283x 83283x 83283x 83283x 83283x 83283x 83283x 83283x 83283x 83283x 83283x 83283x 83283x 83283x 21101x 21101x 20205x 20205x 21101x 21101x 374x 374x 21101x 21101x 431x 431x 431x 431x 13x 13x 418x 431x 38x 38x 418x 431x 29x 29x 26x 29x 3x 3x 29x     29x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 29x 418x 431x 83x 83x 431x 6x 6x 418x 418x 418x 21101x 21101x 21101x 21101x 6x 6x 5x 5x 1x 1x 6x     1x 1x 6x 1x 1x 1x       21101x 21101x 83x 83x 83x 204x 204x 204x 204x 204x 204x 4x 4x 4x 4x 4x 4x 4x 204x 83x 83x 83x 21101x 21101x 38x 38x 38x 38x 38x 81x 81x 22x 81x 59x 28x 5x 5x 23x 28x 3x 3x 28x     23x 28x 16x 16x 59x 31x 31x 31x 21x 21x 1x 1x 1x 1x 21x 76x 81x 49x 81x 5x 5x 81x 28x 38x 10x 10x 10x 10x 28x 28x 28x 21101x 21101x 23x 23x 23x 23x 63x 20x 20x 16x 20x     20x 63x 23x 23x 23x 21101x 21101x  
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ExpressionNode, StatementNode, TokenNode} from "../abap/nodes";
import {ABAPFile} from "../abap/abap_file";
import {EditHelper} from "../edit_helper";
import {countLeadingNot} from "../abap/5_syntax/_key_compare";
 
export class ManyParenthesesConf extends BasicRuleConfig {
}
 
export class ManyParentheses extends ABAPRule {
 
  private conf = new ManyParenthesesConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "many_parentheses",
      title: "Too many parentheses",
      shortDescription: `Searches for expressions where extra parentheses can safely be removed`,
      tags: [RuleTag.SingleFile, RuleTag.Quickfix],
      badExample: `IF ( destination IS INITIAL ).
ENDIF.
IF foo = boo AND ( bar = lar AND moo = loo ).
ENDIF.`,
      goodExample: `IF destination IS INITIAL.
ENDIF.
IF foo = boo AND bar = lar AND moo = loo.
ENDIF.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: ManyParenthesesConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const structure = file.getStructure();
    if (structure === undefined) {
      return [];
    }
 
    for (const cond of structure.findAllExpressionsMulti([Expressions.Cond, Expressions.ComponentCond])) {
      issues.push(...this.analyzeCondition(file, cond));
    }
 
    for (const sub of structure.findAllExpressionsMulti([Expressions.CondSub, Expressions.ComponentCondSub])) {
      let cond: readonly ExpressionNode[];
      if (sub.get() instanceof Expressions.CondSub) {
        cond = sub.findDirectExpressions(Expressions.Cond);
      } else {
        cond = sub.findDirectExpressions(Expressions.ComponentCond);
      }
      if (cond.length !== 1) {
        continue;
      }
      if (cond[0].getChildren().length === 1) {
        const message = "Too many parentheses, simple";
        const children = sub.getChildren();
        const nots = countLeadingNot(sub);
 
        const startToken = children[nots].getFirstToken();
        const fixText = children[nots + 1].concatTokens();
 
        const fix = EditHelper.replaceRange(file, startToken.getStart(), sub.getLastToken().getEnd(), fixText);
 
        const issue = Issue.atToken(file, startToken, message, this.getMetadata().key, this.conf.severity, fix);
        issues.push(issue);
      }
    }
 
    for (const m of structure.findAllStatements(Statements.Move)) {
      issues.push(...this.analyzeMove(file, m));
    }
    for (const m of structure.findAllStatements(Statements.Select)) {
      issues.push(...this.analyzeInto(file, m));
    }
 
    return issues;
  }
 
////////////////////
 
  private analyzeInto(file: ABAPFile, m: StatementNode): Issue[] {
    const into = m.findFirstExpression(Expressions.SQLIntoList);
    if (into === undefined) {
      return [];
    }
 
    const second = into.getAllTokens()[1];
    if (second === undefined || second.getStr() !== "(") {
      return [];
    }
 
    const concat = into.concatTokens();
    if (concat.endsWith(")") === true && concat.includes(",") === false) {
      const issue = Issue.atStatement(file, m, "Too many parentheses", this.getMetadata().key, this.conf.severity);
      return [issue];
    }

    return [];
  }
 
  private analyzeMove(file: ABAPFile, m: StatementNode): Issue[] {
    const issues: Issue[] = [];
 
    for (const source of m.findAllExpressionsRecursive(Expressions.Source)) {
      const lastChildren = source.getChildren();
      if (lastChildren.length === 3
          && lastChildren[0].getFirstToken().getStr() === "("
          && lastChildren[1].getChildren().length === 1
          && lastChildren[1].getFirstToken().getStr().startsWith("-") === false
          && lastChildren[2].getFirstToken().getStr() === ")") {
        const fixText = lastChildren[1].concatTokens();
        const start = lastChildren[0].getFirstToken().getStart();
        const end = lastChildren[2].getLastToken().getEnd();
        const fix = EditHelper.replaceRange(file, start, end, fixText);
        const issue = Issue.atToken(file, lastChildren[0].getFirstToken(), "Too many parentheses", this.getMetadata().key, this.conf.severity, fix);
        issues.push(issue);
      }
    }
 
    return issues;
  }
 
  private analyzeCondition(file: ABAPFile, cond: ExpressionNode): Issue[] {
    const issues: Issue[] = [];
    let comparator = "";
    let found = false;
 
    for (const c of cond.getChildren()) {
      let current = "";
      if (c instanceof TokenNode) {
        current = c.get().getStr().toUpperCase();
      } else if (c instanceof ExpressionNode
          && (c.get() instanceof Expressions.CondSub || c.get() instanceof Expressions.ComponentCondSub)) {
        if (countLeadingNot(c) > 0) {
          return [];
        }
        let i = c.findDirectExpression(Expressions.Cond);
        if (i === undefined) {
          i = c.findDirectExpression(Expressions.ComponentCond);
        }
        if (i === undefined) {
          return [];
        }
        current = this.findComparator(i);
        if (current !== "") {
          found = true; // dont report for the simple case that contains quick fixes
        }
      } else if (c instanceof ExpressionNode
          && c.get() instanceof Expressions.Compare
          && c.getFirstChild()?.get() instanceof Expressions.Source
          && c.getChildren().length === 3) {
        const concat = c.getFirstChild()?.concatTokens();
        if (concat?.startsWith("(") && concat.endsWith(")")) {
          const message = "Parentheses can be removed";
          const issue = Issue.atToken(file, c.getFirstToken(), message, this.getMetadata().key, this.conf.severity);
          issues.push(issue);
        }
      }
 
      if (comparator === "") {
        comparator = current;
      } else if (comparator !== "" && current !== "" && comparator !== current) {
        return [];
      }
    }
 
    if (comparator !== "" && comparator !== "MIXED" && found === true) {
      const message = "Too many parentheses, complex";
      const issue = Issue.atToken(file, cond.getFirstToken(), message, this.getMetadata().key, this.conf.severity);
      issues.push(issue);
    }
 
    return issues;
  }
 
  private findComparator(cond: ExpressionNode): string {
    let comparator = "";
 
    const children = cond.getChildren();
    for (const c of children) {
      if (c instanceof TokenNode) {
        const current = c.get().getStr().toUpperCase();
        if (comparator === "") {
          comparator = current;
        } else if (current !== comparator) {
          return "MIXED";
        }
      }
    }
 
    return comparator;
  }
 
}