All files / src/rules identical_contents.ts

96.74% Statements 119/123
88.57% Branches 31/35
100% Functions 7/7
96.74% Lines 119/123

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 1231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10327x 10327x 10327x 10327x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 30814x 10327x 10327x 9814x 9814x 10327x 10327x 241x 241x 10327x 10327x 264x 264x 264x 264x 12x 12x 264x 1497x 7x 7x 1497x 245x 264x 27x 27x 245x 245x 245x 10327x 10327x 10327x 10327x 27x 18x 18x 9x 9x 27x 1x 1x 27x 27x     8x 8x 8x 8x 27x 1x 27x 3x 3x 3x 3x 27x 4x 4x 4x 4x 27x   27x 1x 1x 1x 1x 27x 3x 3x 3x 10327x 10327x 24x   24x 12x 12x 12x 12x 24x 10327x 10327x
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[] {
    if (node.getChildren().length !== 4) {
      return [];
    }
 
    const ifBody = node.findDirectStructure(Structures.Body);
    if (node.findDirectStructure(Structures.ElseIf)) {
      return [];
    }
    const elseBody = node.findDirectStructure(Structures.Else)?.findDirectStructure(Structures.Body);
    if (elseBody === undefined || ifBody === undefined) {
      return [];
    }
 
    {
      const ifFirst = ifBody.getFirstChild();
      const elseFirst = elseBody.getFirstChild();
      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];
      }
    }
 
    {
      const ifLast = ifBody.getLastChild();
      const elseLast = elseBody.getLastChild();
      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());
    }
  }
 
}