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 11455x 11455x 11455x 11455x 11455x 45224x 45224x 45224x 45224x 45224x 45224x 45224x 45224x 11455x 11455x 10927x 10927x 11455x 11455x 247x 247x 11455x 11455x 251x 251x 11455x 11455x 334x 334x 249x 249x 85x 85x 85x 11455x 11455x 85x 85x 85x 85x 11455x  
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);
  }
}