All files / src/rules cds_field_order.ts

93.18% Statements 123/132
84.61% Branches 22/26
100% Functions 8/8
93.18% Lines 123/132

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 1331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11460x 11460x 11460x 11460x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 56071x 11460x 11460x 10932x 10932x 11460x 11460x 247x 247x 11460x 11460x 256x 256x 11460x 11460x 339x 328x 328x 11x 11x 339x 1x 1x 10x 10x 339x     10x 10x 10x 10x 10x 10x 10x     10x 10x 10x 10x 10x 29x 29x 29x 29x 2x 2x 2x 29x 29x 3x 3x 3x 29x 29x 18x 18x 29x 7x 7x 29x 10x 10x 10x 10x 11460x 11460x 10x 10x 10x 7x 7x     7x 7x 7x 7x 7x 7x 7x       7x 10x 10x 11460x 11460x 29x 29x 29x 29x 11460x 11460x  
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, 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>();
    const found = [...tree.findAllExpressions(CDSAssociation), ...tree.findAllExpressions(CDSComposition)];
    for (const assoc of found) {
      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());
  }
 
}