All files / src/rules cds_field_order.ts

93.12% Statements 122/131
84.61% Branches 22/26
100% Functions 8/8
93.12% Lines 122/131

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 1321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11514x 11514x 11514x 11514x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 34368x 11514x 11514x 11011x 11011x 11514x 11514x 235x 235x 11514x 11514x 243x 243x 11514x 11514x 313x 303x 303x 10x 10x 313x 1x 1x 9x 9x 313x     9x 9x 9x 9x 9x 9x 9x     9x 9x 9x 9x 9x 23x 23x 23x 23x 2x 2x 2x 23x 23x 3x 3x 3x 23x 23x 14x 14x 23x 5x 5x 23x 9x 9x 9x 9x 11514x 11514x 9x 9x 5x 5x     5x 5x 5x 5x 5x 5x 5x       5x 9x 9x 11514x 11514x 23x 23x 23x 23x 11514x 11514x  
import {Issue} from "../issue";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {IObject} from "../objects/_iobject";
import {IRegistry} from "../_iregistry";
import {BasicRuleConfig} from "./_basic_rule_config";
import {DataDefinition} from "../objects";
import {CDSSelect, CDSElement, CDSAssociation, CDSAs, CDSName, CDSRelation} from "../cds/expressions";
import {ExpressionNode} from "../abap/nodes/expression_node";
 
export class CDSFieldOrderConf extends BasicRuleConfig {
}
 
export class CDSFieldOrder implements IRule {
  private conf = new CDSFieldOrderConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "cds_field_order",
      title: "CDS Field Order",
      shortDescription: `Checks that CDS key fields are listed first and associations last in the field list`,
      tags: [RuleTag.SingleFile],
      badExample: `define view entity test as select from source
  association [1..1] to target as _Assoc on _Assoc.id = source.id
{
  field1,
  key id,
  _Assoc
}`,
      goodExample: `define view entity test as select from source
  association [1..1] to target as _Assoc on _Assoc.id = source.id
{
  key id,
  field1,
  _Assoc
}`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: CDSFieldOrderConf) {
    this.conf = conf;
  }
 
  public initialize(_reg: IRegistry): IRule {
    return this;
  }
 
  public run(o: IObject): Issue[] {
    if (o.getType() !== "DDLS" || !(o instanceof DataDefinition)) {
      return [];
    }
 
    const tree = o.getTree();
    if (tree === undefined) {
      return [];
    }
 
    const file = o.findSourceFile();
    if (file === undefined) {
      return [];
    }
 
    const issues: Issue[] = [];
    const associationNames = this.getAssociationNames(tree);
 
    for (const select of tree.findAllExpressions(CDSSelect)) {
      const elements = select.findDirectExpressions(CDSElement);
      if (elements.length === 0) {
        continue;
      }
 
      let seenNonKey = false;
      let seenAssociation = false;
 
      for (const element of elements) {
        const isKey = element.findDirectTokenByText("KEY") !== undefined;
        const isAssoc = this.isAssociationElement(element, associationNames);
 
        if (isKey && seenNonKey) {
          const message = "Key fields must be listed before non-key fields";
          issues.push(Issue.atToken(file, element.getFirstToken(), message, this.getMetadata().key, this.getConfig().severity));
        }
 
        if (!isAssoc && seenAssociation) {
          const message = "Associations must be listed last in the field list";
          issues.push(Issue.atToken(file, element.getFirstToken(), message, this.getMetadata().key, this.getConfig().severity));
        }
 
        if (!isKey) {
          seenNonKey = true;
        }
        if (isAssoc) {
          seenAssociation = true;
        }
      }
    }
 
    return issues;
  }
 
  private getAssociationNames(tree: ExpressionNode): Set<string> {
    const names = new Set<string>();
    for (const assoc of tree.findAllExpressions(CDSAssociation)) {
      const relation = assoc.findDirectExpression(CDSRelation);
      if (relation === undefined) {
        continue;
      }
      const asNode = relation.findDirectExpression(CDSAs);
      if (asNode) {
        const nameNode = asNode.findDirectExpression(CDSName);
        if (nameNode) {
          names.add(nameNode.getFirstToken().getStr().toUpperCase());
        }
      } else {
        const name = relation.getFirstToken().getStr();
        names.add(name.toUpperCase());
      }
    }
    return names;
  }
 
  private isAssociationElement(element: ExpressionNode, associationNames: Set<string>): boolean {
    const tokens = element.concatTokens().replace(/^(KEY|VIRTUAL)\s+/i, "").trim();
    const name = tokens.split(/[\s.,:(]+/)[0];
    return associationNames.has(name.toUpperCase());
  }
 
}