All files / src/utils include_graph.ts

95.45% Statements 210/220
92.45% Branches 49/53
92.85% Functions 13/14
95.45% Lines 210/220

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 207 208 209 210 211 212 213 214 215 216 217 218 219 2201x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1026x 1026x 1026x 1306x 1058x 1058x 1306x 1026x 1026x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 513x 513x 513x 1x 1x 503x 503x 1x 1x 15x 21x 12x 12x 21x 3x 3x 1x 1x 25x 33x 25x 25x 33x     1x 1x 9x 9x 1x 1x 35x 35x 20x 10x 10x 20x 35x 25x 25x 25x 25x 25x 35x 35x 1x 1x 1x 1x 1x 1x 1x 1x 1x 513x 513x 513x 513x 513x 1x 1x     1x 1x 25x 25x     25x 25x 22x 22x 25x 25x 25x 1x 1x 273x 273x 8x 5x 5x 8x 273x 273x 1x 1x 1x 1x 513x 513x 513x 529x 545x 2990x 19x 19x 19x     19x 19x 4x 4x 15x 19x 3x 2x 2x 2x 19x 3x 3x 12x 9x 9x 19x 2990x 545x 529x 513x 513x 513x 1x 1x 513x 503x 11x 2x 2x     2x 2x 2x 11x 503x 513x 1x 1x 513x 529x 376x 376x 376x 376x 376x 376x 376x 529x 7x 7x 7x 7x 7x 7x 7x 153x 102x 112x 112x 112x 112x 112x 146x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 529x 513x 1x 1x
import {Include} from "../abap/2_statements/statements";
import {IncludeName} from "../abap/2_statements/expressions";
import {Class, FunctionGroup, Program, TypePool} from "../objects";
import {CheckInclude} from "../rules/check_include";
import {Position} from "../position";
import {Issue} from "../issue";
import {IFile} from "../files/_ifile";
import {IIncludeGraph} from "./_include_graph";
import {IRegistry} from "../_iregistry";
import {ABAPObject} from "../objects/_abap_object";
import {Severity} from "../severity";
 
// todo, check for cycles/circular dependencies, method findTop
// todo, add configurable error for multiple use includes
 
function getABAPObjects(reg: IRegistry): ABAPObject[] {
  const ret: ABAPObject[] = [];
  for (const o of reg.getObjects()) {
    if (o instanceof ABAPObject) {
      ret.push(o);
    }
  }
  return ret;
}
 
interface IVertex {
  filename: string;
  includeName: string;
  include: boolean;
}
 
class Graph {
  public readonly vertices: IVertex[];
  public readonly edges: {from: string, to: string}[];
 
  public constructor() {
    this.vertices = [];
    this.edges = [];
  }
 
  public addVertex(vertex: IVertex) {
    this.vertices.push(vertex);
  }
 
  public findInclude(includeName: string): IVertex | undefined {
    for (const v of this.vertices) {
      if (v.includeName.toUpperCase() === includeName.toUpperCase()) {
        return v;
      }
    }
    return undefined;
  }
 
  public findVertex(filename: string): IVertex | undefined {
    for (const v of this.vertices) {
      if (v.filename.toUpperCase() === filename.toUpperCase()) {
        return v;
      }
    }
    return undefined;
  }
 
  public addEdge(from: IVertex, toFilename: string) {
    this.edges.push({from: from.filename, to: toFilename});
  }
 
  public findTop(filename: string): IVertex[] {
    const ret: IVertex[] = [];
    for (const e of this.edges) {
      if (e.from === filename) {
        ret.push(...this.findTop(e.to));
      }
    }
    if (ret.length === 0) {
      const found = this.findVertex(filename);
      if (found !== undefined) {
        ret.push(found);
      }
    }
    return ret;
  }
 
}
 
export class IncludeGraph implements IIncludeGraph {
  private readonly reg: IRegistry;
  private readonly issues: Issue[];
  private readonly graph: Graph;
 
  public constructor(reg: IRegistry) {
    this.reg = reg;
    this.issues = [];
    this.graph = new Graph();
    this.build();
  }
 
  public getIssues(): Issue[] {
    return this.issues;
  }
 
  public listMainForInclude(filename: string | undefined): string[] {
    const ret: string[] = [];
    if (filename === undefined) {
      return [];
    }
    for (const f of this.graph.findTop(filename)) {
      if (f.include === false) {
        ret.push(f.filename);
      }
    }
    return ret;
  }
 
  public getIssuesFile(file: IFile): Issue[] {
    const ret: Issue[] = [];
    for (const i of this.issues) {
      if (i.getFilename() === file.getFilename()) {
        ret.push(i);
      }
    }
    return ret;
  }
 
///////////////////////////////
 
  private build() {
    this.addVertices();
 
    for (const o of getABAPObjects(this.reg)) {
      for (const f of o.getABAPFiles()) {
        for (const s of f.getStatements()) {
          if (s.get() instanceof Include) {
            const ifFound = s.concatTokens().toUpperCase().includes("IF FOUND");
            const iexp = s.findFirstExpression(IncludeName);
            if (iexp === undefined) {
              throw new Error("unexpected Include node");
            }
            const name = iexp.getFirstToken().getStr().toUpperCase();
            if (name.match(/^(\/\w+\/)?L.+XX$/)) { // function module XX includes, possibily namespaced
              continue;
            }
            const found = this.graph.findInclude(name);
            if (found === undefined) {
              if (ifFound === false) {
                const issue = Issue.atStatement(f, s, "Include " + name + " not found", new CheckInclude().getMetadata().key, Severity.Error);
                this.issues.push(issue);
              }
            } else if (found.include === false) {
              const issue = Issue.atStatement(f, s, "Not possible to INCLUDE a main program", new CheckInclude().getMetadata().key, Severity.Error);
              this.issues.push(issue);
            } else {
              this.graph.addEdge(found, f.getFilename());
            }
          }
        }
      }
    }
 
    this.findUnusedIncludes();
  }
 
  private findUnusedIncludes() {
    for (const v of this.graph.vertices) {
      if (v.include === true) {
        if (this.listMainForInclude(v.filename).length === 0) {
          const f = this.reg.getFileByName(v.filename);
          if (f === undefined) {
            throw new Error("findUnusedIncludes internal error");
          }
          const issue = Issue.atPosition(f, new Position(1, 1), "INCLUDE not used anywhere", new CheckInclude().getMetadata().key, Severity.Error);
          this.issues.push(issue);
        }
      }
    }
  }
 
  private addVertices() {
    for (const o of getABAPObjects(this.reg)) {
      if (o instanceof Program) {
        const file = o.getMainABAPFile();
        if (file) {
          this.graph.addVertex({
            filename: file.getFilename(),
            includeName: o.getName(),
            include: o.isInclude()});
        }
      } else if (o instanceof TypePool) {
        const file = o.getMainABAPFile();
        if (file) {
          this.graph.addVertex({
            filename: file.getFilename(),
            includeName: o.getName(),
            include: false});
        }
      } else if (o instanceof Class) {
        for (const f of o.getSequencedFiles()) {
          this.graph.addVertex({
            filename: f.getFilename(),
            includeName: o.getName(),
            include: false});
        }
      } else if (o instanceof FunctionGroup) {
        for (const i of o.getIncludeFiles()) {
          this.graph.addVertex({
            filename: i.file.getFilename(),
            includeName: i.name,
            include: true});
        }
        const file = o.getMainABAPFile();
        if (file) {
          this.graph.addVertex({
            filename: file.getFilename(),
            includeName: o.getName(),
            include: false});
        }
      }
    }
  }
 
}