All files / src/rules cds_field_order.ts

95.2% Statements 119/125
85.71% Branches 24/28
100% Functions 8/8
95.2% Lines 119/125

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 1261x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21113x 21113x 21113x 21113x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 103469x 21113x 21113x 20214x 20214x 21113x 21113x 374x 374x 21113x 21113x 389x 389x 21113x 21113x 599x 580x 580x 19x 19x 599x 1x 1x 18x 18x 599x     18x 18x 18x 18x 18x 18x 18x     18x 18x 18x 18x 18x 51x 51x 51x 51x 2x 2x 2x 51x 51x 7x 7x 7x 51x 51x 32x 32x 51x 16x 16x 51x 18x 18x 18x 18x 21113x 21113x 18x 18x 18x 15x 15x     15x 15x 15x 18x 18x 21113x 21113x 21113x 51x 51x 51x 21113x 21113x  
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, CDSComposition, CDSName, CDSPrefixedName, CDSRelation} from "../cds/expressions";
import {readRelation} from "../cds/_cds_nodes";
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>();
    const found = [...tree.findAllExpressions(CDSAssociation), ...tree.findAllExpressions(CDSComposition)];
    for (const assoc of found) {
      const relation = assoc.findDirectExpression(CDSRelation);
      if (relation === undefined) {
        continue;
      }
      const {name, alias} = readRelation(relation);
      names.add((alias ?? name).toUpperCase());
    }
    return names;
  }
 
  /** the element names an association, also when annotated, with path filter, parameters or a field of the target */
  private isAssociationElement(element: ExpressionNode, associationNames: Set<string>): boolean {
    const name = element.findDirectExpression(CDSPrefixedName)?.findDirectExpression(CDSName);
    return name !== undefined && associationNames.has(name.concatTokens().replace(/ /g, "").toUpperCase());
  }
 
}