All files / src/rules cds_comment_style.ts

96.87% Statements 62/64
85.71% Branches 12/14
100% Functions 6/6
96.87% Lines 62/64

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 641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9425x 9425x 9425x 9425x 28109x 28109x 28109x 28109x 28109x 28109x 28109x 28109x 28109x 28109x 28109x 28109x 28109x 28109x 9425x 9425x 8957x 8957x 9425x 9425x 218x 218x 9425x 9425x 221x 221x 9425x 9425x 281x 281x 281x 281x 4x 4x     4x 4x 78x 1x 1x 78x 4x 281x 281x 281x 9425x 9425x
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 {CDSMetadataExtension, DataDefinition} from "../objects";
import {CDSLexer} from "../cds/cds_lexer";
import {Comment} from "../abap/1_lexer/tokens";
 
export class CDSCommentStyleConf extends BasicRuleConfig {
}
 
export class CDSCommentStyle implements IRule {
  private conf = new CDSCommentStyleConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "cds_comment_style",
      title: "CDS Comment Style",
      shortDescription: `Check for obsolete comment style`,
      extendedInformation: `Check for obsolete comment style
 
Comments starting with "--" are considered obsolete
 
https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-us/abencds_general_syntax_rules.htm`,
      tags: [RuleTag.SingleFile],
      badExample: "-- this is a comment",
      goodExample: "// this is a comment",
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: CDSCommentStyleConf) {
    this.conf = conf;
  }
 
  public initialize(_reg: IRegistry): IRule {
    return this;
  }
 
  public run(object: IObject): Issue[] {
    const issues: Issue[] = [];
 
    if ((object.getType() === "DDLS" && object instanceof DataDefinition) ||
        (object.getType() === "DDLX" && object instanceof CDSMetadataExtension)) {
      const file = object.findSourceFile();
      if (file === undefined) {
        return issues;
      }
      const tokens = CDSLexer.run(file);
      for (const t of tokens) {
        if (t instanceof Comment && t.getStr().startsWith("--")) {
          issues.push(Issue.atToken(file, t, `Use "//" for comments instead of "--"`, this.getMetadata().key, this.getConfig().severity));
        }
      }
    }
 
    return issues;
  }
 
}