All files / src/rules select_single_full_key.ts

92% Statements 138/150
82.35% Branches 42/51
100% Functions 9/9
92% Lines 138/150

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 1511x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42989x 42989x 42989x 1x 21503x 21503x 21503x 21503x 21503x 84867x 84867x 84867x 84867x 84867x 84867x 84867x 84867x 84867x 84867x 84867x 21503x 21503x 409x 409x 409x 21503x 21503x 20580x         20580x     20580x 20580x 21503x 21503x 392x 392x 21503x 21503x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 21503x 21503x 671x 253x 253x 418x 418x 671x 11x 11x 407x 407x 407x 407x 671x 417x 417x 417x 2645x 2645x 2616x 2645x 1x 29x     28x 2645x     28x 2645x 2645x 8x     8x 8x 20x 20x 20x 2645x 1x 1x 19x 19x 19x 19x 19x 2645x 30x 5x 5x 25x 25x 19x 2645x 8x 8x 8x 8x 8x 8x 19x 2645x 15x 15x 15x 2645x 417x 407x 407x 407x 21503x 21503x 20x 20x 20x 21503x 21503x  
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
import {BasicRuleConfig} from "./_basic_rule_config";
import {EditHelper} from "../edit_helper";
import {IObject} from "../objects/_iobject";
import {IRegistry} from "../_iregistry";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {Issue} from "../issue";
import {StatementNode} from "../abap/nodes";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {Table} from "../objects";
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {Comment} from "../abap/2_statements/statements/_statement";
import {Position} from "../position";
import {ISpaghettiScope} from "../abap/5_syntax/_spaghetti_scope";
 
export class SelectSingleFullKeyConf extends BasicRuleConfig {
  public allowPseudo = true;
}
 
export class SelectSingleFullKey implements IRule {
  private reg: IRegistry;
  private conf = new SelectSingleFullKeyConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "select_single_full_key",
      title: "Detect SELECT SINGLE which are possibily not unique",
      shortDescription: `Detect SELECT SINGLE which are possibily not unique`,
      extendedInformation: `Table definitions must be known, ie. inside the errorNamespace
 
If the statement contains a JOIN it is not checked`,
      pseudoComment: "EC CI_NOORDER",
      tags: [RuleTag.Quickfix],
    };
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public getConfig() {
    if (this.conf === undefined) {
      this.conf = {
        allowPseudo: true,
      };
    }
    if (this.conf.allowPseudo === undefined) {
      this.conf.allowPseudo = true;
    }
    return this.conf;
  }
 
  public setConfig(conf: SelectSingleFullKeyConf) {
    this.conf = conf;
  }
 
  private buildFix(file: ABAPFile, statement: StatementNode, rows: readonly string[]) {
    const end = statement.getLastToken().getEnd();
    const suffix = rows[end.getRow() - 1].slice(end.getCol() - 1).trimStart();
    // Keep chained '*' continuations out of the full-line comment column.
    const newline = suffix !== "" && suffix.startsWith('"') === false ? "\n " : "";
 
    return {
      description: `Add "#EC CI_NOORDER`,
      edit: EditHelper.insertAt(file, end, ` "#EC CI_NOORDER${newline}`),
    };
  }
 
  public run(obj: IObject): readonly Issue[] {
    if (!(obj instanceof ABAPObject)) {
      return [];
    }
 
    const syntax = new SyntaxLogic(this.reg, obj).run();
    if (syntax.issues.length > 0) {
      return [];
    }
 
    const issues: Issue[] = [];
    const message = "SELECT SINGLE possibily not unique";
 
    for (const file of obj.getABAPFiles()) {
      const statements = file.getStatements();
      const rows = file.getRawRows();
      for (let i = 0; i < statements.length; i++) {
        const s = statements[i];
        if (!(s.get() instanceof Statements.Select)) {
          continue;
        } else if (s.findFirstExpression(Expressions.SQLJoin)) {
          continue;
        } else if (s.findTokenSequencePosition("SELECT", "SINGLE") === undefined) {
          continue;
        }
        const databaseTable = s.findFirstExpression(Expressions.DatabaseTable);
        if (databaseTable === undefined) {
          continue;
        }
        const next = statements[i + 1];
        if (next?.get() instanceof Comment
            && next.concatTokens().includes(this.getMetadata().pseudoComment + "")) {
          if (this.getConfig().allowPseudo !== true) {
            issues.push(Issue.atStatement(file, s, "Pseudo comment not allowed", this.getMetadata().key, this.getConfig().severity));
          }
          continue;
        }
 
        const tabl = this.findReference(databaseTable.getFirstToken().getStart(), syntax.spaghetti, file);
        const table = this.reg.getObject("TABL", tabl) as Table | undefined;
        if (table === undefined) {
          continue;
        }
        const keys = table.listKeys(this.reg);
        const client = table.getClientField(this.reg);
 
        const cond = s.findFirstExpression(Expressions.SQLCond);
        const set = new Set<string>();
        for (const key of keys) {
          if (key === client) {
            continue;
          }
          set.add(key);
        }
 
        for (const compare of cond?.findAllExpressionsRecursive(Expressions.SQLCompare) || []) {
          const fname = compare.findDirectExpression(Expressions.SQLFieldName)?.concatTokens().toUpperCase();
          const operator = compare.findDirectExpression(Expressions.SQLCompareOperator)?.concatTokens().toUpperCase();
          if (fname && (operator === "=" || operator === "EQ")) {
            set.delete(fname);
          }
        }
 
        if (set.size > 0) {
          const fix = this.buildFix(file, s, rows);
          issues.push(Issue.atStatement(file, s, message, this.getMetadata().key, this.getConfig().severity, undefined, [fix]));
        }
      }
    }
 
    return issues;
  }
 
  private findReference(position: Position, spaghetti: ISpaghettiScope, file: ABAPFile) {
    const scope = spaghetti.lookupPosition(position, file.getFilename());
    return scope?.findTableReference(position);
  }
 
}