All files / src config.ts

87.93% Statements 51/58
76.47% Branches 26/34
100% Functions 11/11
87.93% Lines 51/58

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 133 134 135 136 137 138 139 140 141 142 143 1441x 1x     1x     1x       6991x   6991x 894848x     6991x 901839x     6991x 6991x 206x     6991x                                               6991x       131x 131x 16899x 16899x   16899x 14722x 1x 14721x 9x 14712x 14712x 14712x         131x         7380x       7380x   7380x     7380x     7380x     7380x     7380x 15x   7380x       374x       16125x       114x       5280x       9529x       10480x     10480x       7380x     7380x 7380x 79173x 7379x 7379x     7380x 1x        
import {Version, defaultVersion} from "./version";
import {ArtifactsRules} from "./artifacts_rules";
import {IRule} from "./rules/_irule";
import {IConfig, IGlobalConfig, ISyntaxSettings, IConfiguration} from "./_config";
import * as JSON5 from "json5";
 
// assumption: this class is immutable
export class Config implements IConfiguration {
  private readonly config: IConfig;
 
  public static getDefault(ver?: Version): Config {
    const rules: any = {};
 
    const sorted = ArtifactsRules.getRules().sort((a, b) => {
      return a.getMetadata().key.localeCompare(b.getMetadata().key);
    });
 
    for (const rule of sorted) {
      rules[rule.getMetadata().key] = rule.getConfig();
    }
 
    let version = defaultVersion;
    if (ver) {
      version = ver;
    }
 
    const config: IConfig = {
      global: {
        files: "/src/**/*.*",
        exclude: [],
        skipGeneratedGatewayClasses: true,
        skipGeneratedPersistentClasses: true,
        skipGeneratedFunctionGroups: true,
        useApackDependencies: false,
        skipIncludesWithoutMain: false,
      },
      dependencies: [{
        url: "https://github.com/abaplint/deps",
        folder: "/deps",
        files: "/src/**/*.*",
      }],
      syntax: {
        version,
        errorNamespace: "^(Z|Y|LCL\_|TY\_|LIF\_)",
        globalConstants: [],
        globalMacros: [],
      },
      rules: rules,
    };
 
    return new Config(JSON.stringify(config));
  }
 
  public getEnabledRules(): IRule[] {
    const rules: IRule[] = [];
    for (const rule of ArtifactsRules.getRules()) {
      const ruleConfig = this.config["rules"][rule.getMetadata().key];
      const ruleExists = ruleConfig !== undefined;
 
      if (ruleExists) {
        if (ruleConfig === false) { // "rule": false
          continue;
        } else if (ruleConfig === true) { // "rule": true
          rules.push(rule);
        } else Eif (typeof ruleConfig === "object") { // "rule": { ...config }
          rule.setConfig(ruleConfig);
          rules.push(rule);
        }
      }
    }
 
    return rules;
  }
 
  public constructor(json: string) {
    // huh, hack
    Iif (JSON5.parse === undefined) {
      // @ts-ignore
      JSON5.parse = JSON5.default.parse;
    }
    this.config = JSON5.parse(json);
 
    Iif (this.config.global === undefined) {
      this.config.global = Config.getDefault().getGlobal();
    }
    Iif (this.config.syntax === undefined) {
      this.config.syntax = Config.getDefault().getSyntaxSetttings();
    }
    Iif (this.config.syntax.globalMacros === undefined) {
      this.config.syntax.globalMacros = [];
    }
    Iif (this.config.syntax.globalConstants === undefined) {
      this.config.syntax.globalConstants = [];
    }
    if (this.config.global.skipIncludesWithoutMain === undefined) {
      this.config.global.skipIncludesWithoutMain = false;
    }
    this.checkVersion();
  }
 
  public get(): IConfig {
    return this.config;
  }
 
  public readByKey(rule: string, key: string) {
    return this.config["rules"][rule] ? this.config["rules"][rule][key] : undefined;
  }
 
  public readByRule(rule: string) {
    return this.config["rules"][rule];
  }
 
  public getGlobal(): IGlobalConfig {
    return this.config.global;
  }
 
  public getSyntaxSetttings(): ISyntaxSettings {
    return this.config.syntax;
  }
 
  public getVersion(): Version {
    Iif (this.config.global === undefined || this.config.syntax.version === undefined) {
      return defaultVersion;
    }
    return this.config.syntax.version;
  }
 
  private checkVersion() {
    Iif (this.config.syntax.version === undefined) {
      return; // handled in getVersion
    }
    let match = false;
    for (const v in Version) {
      if (v === this.config.syntax.version) {
        match = true;
        break;
      }
    }
    if (match === false) {
      this.config.syntax.version = defaultVersion;
    }
  }
 
}