All files / src/rules identical_contents.ts

96.2% Statements 152/158
79.24% Branches 42/53
100% Functions 7/7
96.2% Lines 152/158

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 1581x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11215x 11215x 11215x 11215x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 33493x 11215x 11215x 10730x 10730x 11215x 11215x 227x 227x 11215x 11215x 252x 252x 252x 252x 12x 12x 240x 252x 1451x 8x 8x 1451x 232x 252x 19x 19x 232x 232x 232x 11215x 11215x 11215x 11215x 19x 19x 19x 19x 10x 10x 9x 19x 19x 19x 19x 19x 19x 19x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 1x 1x 2x 2x 2x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 19x     7x 7x 19x 1x 19x 3x 3x 3x 3x 19x 3x 3x 19x   19x 1x 1x 1x 1x 19x 2x 2x 2x 11215x 11215x 20x   20x 10x 10x 10x 10x 20x 11215x 11215x
import * as Structures from "../abap/3_structures/structures";
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import {IRuleMetadata, RuleTag} from "./_irule";
import {StatementNode, StructureNode} from "../abap/nodes";
import {ABAPFile} from "../abap/abap_file";
import {Unknown} from "../abap/2_statements/statements/_statement";
 
export class IdenticalContentsConf extends BasicRuleConfig {
}
 
export class IdenticalContents extends ABAPRule {
  private conf = new IdenticalContentsConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "identical_contents",
      title: "Identical contents",
      shortDescription: `Find identical contents in blocks inside IFs, both in the beginning and in the end.`,
      extendedInformation: `
Prerequsites: code is pretty printed with identical cAsE
 
Chained statments are ignored`,
      tags: [RuleTag.SingleFile],
      badExample: `IF foo = bar.
  WRITE 'bar'.
  WRITE 'world'.
ELSE.
  WRITE 'foo'.
  WRITE 'world'.
ENDIF.`,
      goodExample: `IF foo = bar.
  WRITE 'bar'.
ELSE.
  WRITE 'foo'.
ENDIF.
WRITE 'world'.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: IdenticalContentsConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    let issues: Issue[] = [];
 
    const structure = file.getStructure();
    if (structure === undefined) {
      return [];
    }
 
    for (const statement of file.getStatements()) {
      if (statement.get() instanceof Unknown) {
        return []; // contains parser errors
      }
    }
 
    for (const i of structure.findAllStructuresRecursive(Structures.If)) {
      issues = issues.concat(this.analyzeIf(file, i));
    }
 
    return issues;
  }
 
////////////////
 
  private analyzeIf(file: ABAPFile, node: StructureNode): Issue[] {
    const ifBody = node.findDirectStructure(Structures.Body);
    const elseIfBodies = node.findDirectStructures(Structures.ElseIf) || [];
    const elseBody = node.findDirectStructure(Structures.Else)?.findDirectStructure(Structures.Body);
    if (elseBody === undefined) {
      return [];
    }
 
    const ifFirst = ifBody?.getFirstChild();
    const elseFirst = elseBody?.getFirstChild();
 
    const ifLast = ifBody?.getLastChild();
    const elseLast = elseBody?.getLastChild();
 
    if (elseIfBodies.length > 0) {
      let firstMatch = true;
      let lastMatch = true;
      for (const elseif of elseIfBodies) {
        const elseifBody = elseif.findDirectStructure(Structures.Body);
        const elseifFirst = elseifBody?.getFirstChild();
        const elseifLast = elseifBody?.getLastChild();
 
        if (ifFirst === undefined
            || ifLast === undefined
            || elseLast === undefined
            || elseFirst === undefined) {
          return [];
        }
 
        if (elseifFirst === undefined
            || ifFirst.concatTokens() !== elseifFirst.concatTokens()
            || elseFirst.concatTokens() !== elseifFirst.concatTokens()) {
          firstMatch = false;
        }
        if (elseifLast === undefined
            || ifLast.concatTokens() !== elseifLast.concatTokens()
            || elseLast.concatTokens() !== elseifLast.concatTokens()) {
          lastMatch = false;
        }
      }
 
      if (firstMatch === true || lastMatch === true) {
        const message = "Identical contents";
        const issue = Issue.atToken(file, node.getFirstToken(), message, this.getMetadata().key, this.conf.severity);
        return [issue];
      }
 
      return [];
    } else if (elseBody === undefined || ifBody === undefined) {
      return [];
    }
 
    {
      if (ifFirst === undefined || elseFirst === undefined || this.isChained(ifFirst)) {
        return [];
      } else if (ifFirst.concatTokens() === elseFirst.concatTokens()) {
        const message = "Identical contents";
        const issue = Issue.atToken(file, node.getFirstToken(), message, this.getMetadata().key, this.conf.severity);
        return [issue];
      }
    }
 
    {
      if (ifLast === undefined || elseLast === undefined || this.isChained(ifLast)) {
        return [];
      } else if (ifLast.concatTokens() === elseLast.concatTokens()) {
        const message = "Identical contents";
        const issue = Issue.atToken(file, node.getFirstToken(), message, this.getMetadata().key, this.conf.severity);
        return [issue];
      }
    }
 
    return [];
  }
 
  private isChained(node: StructureNode | StatementNode | undefined): boolean {
    if (node === undefined) {
      return false;
    } else if (node instanceof StatementNode) {
      return node.getColon() !== undefined;
    } else {
      return this.isChained(node.getFirstStatement());
    }
  }
 
}