All files / src/rules cds_legacy_view.ts

97.22% Statements 70/72
85.71% Branches 12/14
100% Functions 6/6
97.22% Lines 70/72

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 721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8924x 8924x 8924x 8924x 8924x 77518x 77518x 77518x 77518x 77518x 77518x 77518x 77518x 77518x 77518x 77518x 77518x 77518x 8924x 8924x 8486x 8486x 8924x 8924x 202x 202x 8924x 8924x 205x 205x 205x 8924x 8924x 260x 260x 260x 260x     260x 260x 257x 257x 3x 3x 3x 3x 1x 1x 3x 1x 1x 1x 1x 1x 3x 2x 2x 2x 8924x 8924x
import {Issue} from "../issue";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {IObject} from "../objects/_iobject";
import {IRegistry} from "../_iregistry";
import {Version} from "../version";
import {BasicRuleConfig} from "./_basic_rule_config";
import {DataDefinition} from "../objects";
 
export class CDSLegacyViewConf extends BasicRuleConfig {
}
 
export class CDSLegacyView implements IRule {
  private conf = new CDSLegacyViewConf();
  private reg: IRegistry;
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "cds_legacy_view",
      title: "CDS Legacy View",
      shortDescription: `Identify CDS Legacy Views`,
      // eslint-disable-next-line max-len
      extendedInformation: `Use DEFINE VIEW ENTITY instead of DEFINE VIEW
 
https://blogs.sap.com/2021/10/16/a-new-generation-of-cds-views-how-to-migrate-your-cds-views-to-cds-view-entities/
 
v755 and up`,
      tags: [RuleTag.SingleFile],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: CDSLegacyViewConf) {
    this.conf = conf;
  }
 
  public initialize(reg: IRegistry): IRule {
    this.reg = reg;
    return this;
  }
 
  public run(o: IObject): Issue[] {
    const issues: Issue[] = [];
 
    if (this.reg.getConfig().getVersion() < Version.v755
        && this.reg.getConfig().getVersion() !== Version.Cloud) {
      return [];
    }
 
    if (o.getType() !== "DDLS") {
      return [];
    }
 
    if (o instanceof DataDefinition) {
      const tree = o.getTree();
      if (tree === undefined) {
        return []; // parser error
      }
      if (tree.findDirectTokenByText("ENTITY") === undefined) {
        const file = o.findSourceFile();
        if (file) {
          issues.push(Issue.atRow(file, 1, "CDS Legacy View", this.getMetadata().key, this.getConfig().severity));
        }
      }
    }
 
    return issues;
  }
 
}