All files / src/rules xml_bom.ts

100% Statements 53/53
100% Branches 10/10
100% Functions 7/7
100% Lines 53/53

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 541x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21103x 21103x 21103x 21103x 21103x 164284x 164284x 164284x 164284x 164284x 164284x 164284x 164284x 21103x 21103x 20205x 20205x 21103x 21103x 374x 374x 21103x 21103x 379x 379x 21103x 21103x 589x 589x 380x 380x 209x 209x 209x 21103x 21103x 209x 209x 209x 209x 21103x  
import {EditHelper} from "../edit_helper";
import {IFile} from "../files/_ifile";
import {Issue} from "../issue";
import {IObject} from "../objects/_iobject";
import {Position} from "../position";
import {IRegistry} from "../_iregistry";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
 
export class XMLBOMConf extends BasicRuleConfig {
}
 
export class XMLBOM implements IRule {
 
  private conf = new XMLBOMConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "xml_bom",
      title: "XML UTF-8 byte order mark",
      shortDescription: "Checks that XML files start with a UTF-8 byte order mark",
      extendedInformation: "Checks that each XML file has a UTF-8 byte order mark (BOM) at its beginning.",
      tags: [RuleTag.Syntax, RuleTag.Quickfix],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: XMLBOMConf) {
    this.conf = conf;
  }
 
  public initialize(_reg: IRegistry) {
    return this;
  }
 
  public run(obj: IObject): Issue[] {
    const file = obj.getXMLFile();
    if (file === undefined || file.getRaw().startsWith("\uFEFF")) {
      return [];
    }
 
    return [this.createIssue(file)];
  }
 
  private createIssue(file: IFile): Issue {
    const start = new Position(1, 1);
    const fix = EditHelper.insertAt(file, start, "\uFEFF");
    return Issue.atRange(file, start, start, "XML file must start with a UTF-8 byte order mark", this.getMetadata().key, this.conf.severity, fix);
  }
}