All files / src/rules unused_types.ts

92.57% Statements 187/202
83.05% Branches 49/59
100% Functions 16/16
92.57% Lines 187/202

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 2021x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 152x 152x 152x 152x 85x 41x 21x 21x 41x 64x 64x 152x 152x 137x 94x 50x 50x 50x 94x 87x 152x 152x 13x 13x 152x 152x 180x 180x 152x 1x 13x 13x 13x 14x 14x 1x       1x 14x     14x 14x 13x 13x 1x 15323x 15323x 15323x 15323x 1x 7671x 7671x 7671x 7671x 7671x 7671x 22827x 22827x 22827x 22827x 22827x 22827x 22827x 22827x 7671x 7671x 7346x 7346x 7671x 7671x 139x 139x     139x 7671x 7671x 158x 158x 158x 7671x 7671x 184x 20x 20x 164x 164x 164x 184x 12x 12x 152x 152x 152x 184x 132x 132x 20x 184x 28x 28x     28x 28x 28x 28x 7x 7x 28x 13x 13x 13x 184x 14x 14x 14x 14x     14x 14x     14x 1x 1x 13x 13x 13x 13x 13x 13x 7671x 7671x 7671x 7671x 1405x 1405x 1073x 1073x 1405x 1405x 1073x 1073x 1405x 1405x 7671x 7671x 1073x 1073x 1073x 498x 498x 97x 97x   97x 97x   97x 12x 12x 85x 85x 498x 1073x 1073x 514x 137x 137x 514x 1073x 1073x 1073x 7671x 7671x 13x 1x 1x 12x 12x 7671x
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRegistry} from "../_iregistry";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {IObject} from "../objects/_iobject";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ABAPObject} from "../objects/_abap_object";
import {ScopeType} from "../abap/5_syntax/_scope_type";
import {TypedIdentifier} from "../abap/types/_typed_identifier";
import {ISpaghettiScopeNode} from "../abap/5_syntax/_spaghetti_scope";
import {EditHelper, IEdit} from "../edit_helper";
import {ReferenceType} from "../abap/5_syntax/_reference";
import {Identifier} from "../abap/4_file_information/_identifier";
import {ABAPFile} from "../abap/abap_file";
import {StatementNode} from "../abap/nodes";
 
class WorkArea {
  private readonly workarea: TypedIdentifier[] = [];
 
  public push(id: TypedIdentifier) {
    for (const w of this.workarea) {
      if (id.equals(w)) {
        return;
      }
    }
    this.workarea.push(id);
  }
 
  public removeIfExists(id: Identifier) {
    for (let i = 0; i < this.workarea.length; i++) {
      if (id.equals(this.workarea[i])) {
        this.workarea.splice(i, 1);
        return;
      }
    }
  }
 
  public get(): readonly TypedIdentifier[] {
    return this.workarea;
  }
 
  public count(): number {
    return this.workarea.length;
  }
}
 
function removeDuplicates(list: readonly TypedIdentifier[]): readonly TypedIdentifier[] {
  const deduplicated: TypedIdentifier[] = [];
  for (const result of list) {
    let cont = false;
    for (const d of deduplicated) {
      if (result.getStart().equals(d.getStart())) {
        cont = true;
        break;
      }
    }
    if (cont === true) {
      continue;
    }
    deduplicated.push(result);
  }
  return deduplicated;
}
 
export class UnusedTypesConf extends BasicRuleConfig {
  /** skip specific names, case insensitive */
  public skipNames: string[] = [];
}
 
export class UnusedTypes implements IRule {
  private conf = new UnusedTypesConf();
  private reg: IRegistry;
  private workarea: WorkArea;
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "unused_types",
      title: "Unused types",
      shortDescription: `Checks for unused TYPE definitions`,
      tags: [RuleTag.Quickfix],
      pragma: "##NEEDED",
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: UnusedTypesConf) {
    this.conf = conf;
    if (this.conf.skipNames === undefined) {
      this.conf.skipNames = [];
    }
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public run(obj: IObject): Issue[] {
    if (!(obj instanceof ABAPObject)) {
      return [];
    }
 
    // dont report unused variables when there are syntax errors
    const syntax = new SyntaxLogic(this.reg, obj).run();
    if (syntax.issues.length > 0) {
      return [];
    }
    this.workarea = new WorkArea();
    this.traverse(syntax.spaghetti.getTop(), obj, true);
    this.traverse(syntax.spaghetti.getTop(), obj, false);
    if (this.workarea.count() === 0) {
      return []; // exit early if all types are used in the current object
    }
 
    for (const o of this.reg.getObjects()) {
      if (o instanceof ABAPObject) {
        if (this.reg.isDependency(o)) {
          continue; // do not search in dependencies
        }
        const syntax = new SyntaxLogic(this.reg, o).run();
        this.traverse(syntax.spaghetti.getTop(), o, false);
      }
      if (this.workarea.count() === 0) {
        return []; // exit early if all types are used
      }
    }
 
    // what is left is unused
    const ret: Issue[] = [];
    for (const t of removeDuplicates(this.workarea.get())) {
      const message = "Type \"" + t.getName() + "\" not used";
 
      const file = obj.getABAPFileByName(t.getFilename());
      if (file === undefined) {
        continue;
      }
      const statement = EditHelper.findStatement(t.getToken(), file);
      if (statement === undefined) {
        continue;
      }
      if (statement.getPragmas().some(t => t.getStr() === this.getMetadata().pragma)) {
        continue;
      }
 
      const fix = this.buildFix(file, statement);
      ret.push(Issue.atIdentifier(t, message, this.getMetadata().key, this.conf.severity, fix));
    }
    return ret;
  }
 
////////////////////////////
 
  private traverse(node: ISpaghettiScopeNode, obj: ABAPObject, add: boolean) {
 
    if (node.getIdentifier().stype !== ScopeType.BuiltIn) {
      this.checkNode(node, obj, add);
    }
 
    for (const c of node.getChildren()) {
      this.traverse(c, obj, add);
    }
 
  }
 
  private checkNode(node: ISpaghettiScopeNode, obj: ABAPObject, add: boolean) {
    const ret: Issue[] = [];
 
    if (add === true) {
      const types = node.getData().types;
      for (const name in types) {
        const identifier = types[name];
        if (obj.containsFile(identifier.getFilename()) === false) {
          continue;
        } else if (this.conf.skipNames?.length > 0
            && this.conf.skipNames.some((a) => a.toUpperCase() === name)) {
          continue;
        } else if (name !== identifier.getName().toUpperCase()) {
          continue; // may have aliases via interfaces
        }
        this.workarea.push(identifier);
      }
    }
 
    for (const r of node.getData().references) {
      if (r.referenceType === ReferenceType.TypeReference && r.resolved) {
        this.workarea.removeIfExists(r.resolved);
      }
    }
 
    return ret;
  }
 
  private buildFix(file: ABAPFile, statement: StatementNode): IEdit | undefined {
    if (statement.concatTokens().toUpperCase().includes("BEGIN OF")) {
      return undefined;
    }
    return EditHelper.deleteStatement(file, statement);
  }
}