All files / src/utils include_graph.ts

97.13% Statements 203/209
96.15% Branches 50/52
92.86% Functions 13/14
97.13% Lines 203/209

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 2091x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 610x 610x 610x 726x 646x 646x 726x 610x 610x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 305x 305x 305x 1x 1x 311x 311x 1x 1x 9x 10x 6x 6x 10x 3x 3x 1x 1x 159x 172x 148x 148x 172x 11x 11x 1x 1x 5x 5x 1x 1x 164x 164x 10x 5x 5x 10x 164x 159x 159x 148x 148x 159x 164x 164x 1x 1x 1x 1x 1x 1x 1x 1x 1x 305x 305x 305x 305x 305x 1x 1x     1x 1x 159x 159x 148x 145x 145x 148x 159x 159x 1x 1x 167x 167x 5x 4x 4x 5x 167x 167x 1x 1x 1x 1x 305x 305x 305x 323x 333x 1770x 11x 11x 11x     11x 11x 2x 2x 9x 11x 3x 2x 2x 2x 11x 1x 1x 6x 5x 5x 11x 1770x 333x 323x 305x 305x 305x 1x 1x 305x 311x 7x 2x 2x     2x 2x 2x 7x 311x 305x 1x 1x 305x 323x 233x 233x 233x 233x 233x 233x 233x 323x 66x 74x 74x 74x 74x 74x 90x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 323x 305x 1x 1x
import {Include} from "../abap/2_statements/statements";
import {IncludeName} from "../abap/2_statements/expressions";
import {Class, FunctionGroup, Program} 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): string[] {
    const ret: string[] = [];
    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 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});
        }
      }
    }
  }
 
}