All files / src config.ts

90.56% Statements 192/212
81.03% Branches 47/58
100% Functions 13/13
90.56% Lines 192/212

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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 2131x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11232x 11232x 11232x 5222880x 11232x 11232x 11232x 2223936x 2223936x 11232x 11232x 11232x 11232x 72x 72x 72x 22x 72x 72x 72x 11232x 9988x 9988x 9988x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 11232x 1x 1x 271x 271x 53658x 53658x 53658x 53658x 50112x 1x 50112x 11x 50111x 50100x 50100x 50100x 50112x 53658x 271x 271x 271x 1x 1x 12742x 12742x       12742x 12742x 12742x     12742x     12742x     12742x   12742x 12742x 12742x 12742x 12742x   12742x 12742x 12742x 12742x 15x 15x 12742x 15x 15x 12742x 12742x 1x 1x 2645x 2645x 1x 1x 52470x 52470x 52470x     52470x 1x 1x 78x 78x 1x 1x 23591x 23591x 1x 1x 30134x 30134x 1x 1x 24004x 24004x 20994x 20994x 24004x     3010x 3010x 1x 1x 2299x 2299x 1x 1x 109x 109x 108x 108x 1x 1x 1x 1x 14920x 14920x 14240x 14240x 680x 680x 1x 1x 12742x 12742x 12357x 12357x 385x 385x 12742x 1267x 384x 384x 384x 1267x 12742x 1x 1x 1x 12742x           12742x 12742x 12742x 1x 1x  
import {Version, LanguageVersion, ABAPRelease, versionToABAPRelease, Release, defaultRelease, VersionOldOrNew, ReleaseName} 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?: VersionOldOrNew, langVer?: LanguageVersion): 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();
    }
 
    const version: VersionOldOrNew =
      langVer !== undefined
        ? {
          release: ver === undefined || ver === Version.Cloud
            ? Release.Newest.name as ReleaseName
            : typeof ver === "string"
              ? versionToABAPRelease(ver).name as ReleaseName
              : ver.release,
          language: langVer,
        }
        : ver ?? {
          release: Release.Newest.name as ReleaseName,
          language: LanguageVersion.Normal,
        };
 
    // defaults: dont skip anything, report everything. The user can decide to skip stuff
    // its difficult to debug errors not being reported
    const config: IConfig = {
      global: {
        files: "/src/**/*.*",
        exclude: [],
        noIssues: [],
        skipGeneratedBOPFInterfaces: false,
        skipGeneratedFunctionGroups: false,
        skipGeneratedGatewayClasses: false,
        skipGeneratedPersistentClasses: false,
        skipGeneratedProxyClasses: false,
        skipGeneratedProxyInterfaces: false,
        useApackDependencies: false,
        skipIncludesWithoutMain: false,
        errorOnDuplicateFilenames: false,
      },
      dependencies: [{
        url: "https://github.com/abaplint/deps",
        folder: "/deps",
        files: "/src/**/*.*",
      }],
      syntax: {
        version,
        errorNamespace: "^(Z|Y|LCL\_|TY\_|LIF\_)",
        globalConstants: [],
        ambigiousVoids: [],
        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 if (typeof ruleConfig === "object") { // "rule": { ...config }
          rule.setConfig(ruleConfig);
          rules.push(rule);
        }
      }
    }
 
    return rules;
  }
 
  public constructor(json: string) {
    // huh, hack
    if (JSON5.parse === undefined) {
      // @ts-ignore
      JSON5.parse = JSON5.default.parse;
    }
    this.config = JSON5.parse(json);
 
    if (this.config.global === undefined) {
      this.config.global = Config.getDefault().getGlobal();
    }
    if (this.config.syntax === undefined) {
      this.config.syntax = Config.getDefault().getSyntaxSetttings();
    }
    if (this.config.syntax.globalMacros === undefined) {
      this.config.syntax.globalMacros = [];
    }
    if (this.config.syntax.globalConstants === undefined) {
      this.config.syntax.globalConstants = [];
    } else {
      // remove duplicates,
      this.config.syntax.globalConstants = [...new Set(this.config.syntax.globalConstants)];
    }
    if (this.config.syntax.ambigiousVoids === undefined) {
      this.config.syntax.ambigiousVoids = [];
    } else {
      this.config.syntax.ambigiousVoids = [...new Set(this.config.syntax.ambigiousVoids)];
    }
    if (this.config.global.skipIncludesWithoutMain === undefined) {
      this.config.global.skipIncludesWithoutMain = false;
    }
    if (this.config.global.errorOnDuplicateFilenames === undefined) {
      this.config.global.errorOnDuplicateFilenames = false;
    }
    this.checkVersion();
  }
 
  public get(): IConfig {
    return this.config;
  }
 
  public readByKey(rule: string, key: string) {
    if (this.config["rules"]) {
      return this.config["rules"][rule] ? this.config["rules"][rule][key] : undefined;
    } else {
      return 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 getRelease(): ABAPRelease {
    const v = this.config.syntax.version;
    if (v !== undefined && typeof v !== "string") {
      return Release[v.release];
    }
    if (this.config.global === undefined || v === undefined) {
      return defaultRelease;
    }
    return versionToABAPRelease(v);
  }
 
  public isOpenABAP(): boolean {
    return this.getRelease() === Release["open-abap"];
  }
 
  public getVersion(): Version {
    const v = this.config.syntax.version;
    if (this.config.global === undefined || v === undefined || typeof v !== "string") {
      return Version.v816;
    }
    return v;
  }
 
  public getLanguageVersion(): LanguageVersion {
    const v = this.config.syntax.version;
    if (v !== undefined && typeof v !== "string") {
      return v.language;
    }
    return LanguageVersion.Normal;
  }
 
  private checkVersion() {
    const version = this.config.syntax.version;
    if (version === undefined || typeof version !== "string") {
      return; // undefined handled in getVersion, object form is already explicit
    }
    let match = false;
    const vers: any = Version;
    for (const v in Version) {
      if (vers[v] === version) {
        match = true;
        break;
      }
    }
    if (match === false) {
      this.config.syntax.version = undefined;
      return;
    }
    if (version === Version.Cloud) {
      this.config.syntax.version = {
        release: Release.Newest.name as ReleaseName,
        language: LanguageVersion.Cloud,
      };
    }
    // OpenABAP keeps its own version identity; open-abap-ness is derived from the
    // release in getOpenABAP() rather than a separate stored flag.
  }
 
}