All files / src/rules cds_association_name.ts

95.12% Statements 78/82
90% Branches 18/20
100% Functions 6/6
95.12% Lines 78/82

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 831x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11655x 11655x 11655x 11655x 45943x 45943x 45943x 45943x 45943x 45943x 45943x 45943x 45943x 45943x 45943x 45943x 45943x 45943x 45943x 45943x 11655x 11655x 11144x 11144x 11655x 11655x 239x 239x 11655x 11655x 244x 244x 11655x 11655x 318x 311x 311x 7x 7x 318x 1x 1x 6x 6x 318x     6x 6x 6x 318x 5x 5x 1x 1x 4x 5x     4x 5x 2x 2x 2x 2x 5x 6x 6x 6x 11655x 11655x  
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 {CDSAssociation, CDSAs, CDSName} from "../cds/expressions";
 
export class CDSAssociationNameConf extends BasicRuleConfig {
}
 
export class CDSAssociationName implements IRule {
  private conf = new CDSAssociationNameConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "cds_association_name",
      title: "CDS Association Name",
      shortDescription: `CDS association names should start with an underscore`,
      extendedInformation: `By convention, CDS association names must start with an underscore character.
 
https://help.sap.com/docs/abap-cloud/abap-data-models/cds-associations`,
      tags: [RuleTag.SingleFile, RuleTag.Naming],
      badExample: `define view entity test as select from source
  association [1..1] to target as Assoc on Assoc.id = source.id
{ key id }`,
      goodExample: `define view entity test as select from source
  association [1..1] to target as _Assoc on _Assoc.id = source.id
{ key id }`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: CDSAssociationNameConf) {
    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[] = [];
 
    for (const assoc of tree.findAllExpressions(CDSAssociation)) {
      const asExpr = assoc.findFirstExpression(CDSAs);
      if (asExpr === undefined) {
        continue;
      }
      const nameExpr = asExpr.findDirectExpression(CDSName);
      if (nameExpr === undefined) {
        continue;
      }
      const name = nameExpr.getFirstToken().getStr();
      if (!name.startsWith("_")) {
        const token = nameExpr.getFirstToken();
        const message = `CDS association name "${name}" should start with an underscore`;
        issues.push(Issue.atToken(file, token, message, this.getMetadata().key, this.getConfig().severity));
      }
    }
 
    return issues;
  }
 
}