All files / src/rules identical_contents.ts

96.58% Statements 113/117
87.5% Branches 28/32
100% Functions 7/7
96.58% Lines 113/117

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 1171x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7653x 7653x 7653x 7653x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 22818x 7653x 7653x 7346x 7346x 7653x 7653x 139x 139x 7653x 7653x 160x 160x 160x 160x 12x 12x 148x 160x 24x 24x 148x 148x 148x 7653x 7653x 7653x 7653x 24x 13x 13x 11x 11x 24x 2x 2x 24x 24x     9x 9x 9x 9x 24x 1x 24x 3x 3x 3x 3x 24x 5x 5x 5x 5x 24x   24x 1x 1x 1x 1x 24x 4x 4x 4x 7653x 7653x 28x   28x 14x 14x 14x 14x 28x 7653x 7653x
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";
 
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.
 
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 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());
    }
  }
 
}