All files / src/rules whitespace_end.ts

97.1% Statements 67/69
91.66% Branches 11/12
100% Functions 7/7
97.1% Lines 67/69

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 691x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10324x 10324x 10324x 10324x 10324x 30804x 30804x 30804x 30804x 30804x 30804x 30804x 30804x 30804x 30804x 10324x 10324x 3x 3x 10324x 10324x 9811x 9811x 10324x 10324x 241x 241x 10324x 10324x 245x 245x 10324x 10324x 311x 311x 311x 328x     328x 328x 328x 328x 3785x 3x 3x 3x 3x 3x 3x 3x 3785x 328x 311x 311x 311x 10324x
import {Issue} from "../issue";
import {Position} from "../position";
import {BasicRuleConfig} from "./_basic_rule_config";
import {EditHelper} from "../edit_helper";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {IRegistry} from "../_iregistry";
import {IObject} from "../objects/_iobject";
import {MIMEObject, WebMIME} from "../objects";
 
export class WhitespaceEndConf extends BasicRuleConfig {
}
 
export class WhitespaceEnd implements IRule {
 
  private conf = new WhitespaceEndConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "whitespace_end",
      title: "Whitespace at end of line",
      shortDescription: `Checks for redundant whitespace at the end of each line.`,
      extendedInformation: `SMIM and W3MI files are not checked.`,
      tags: [RuleTag.Whitespace, RuleTag.Quickfix, RuleTag.SingleFile],
      badExample: `WRITE 'hello'.      `,
      goodExample: `WRITE 'hello'.`,
    };
  }
 
  private getMessage(): string {
    return "Remove whitespace at end of line";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: WhitespaceEndConf) {
    this.conf = conf;
  }
 
  public initialize(_reg: IRegistry): IRule {
    return this;
  }
 
  public run(obj: IObject): readonly Issue[] {
    const issues: Issue[] = [];
 
    for (const file of obj.getFiles()) {
      if (obj instanceof MIMEObject || obj instanceof WebMIME) {
        continue;
      }
 
      const rows = file.getRawRows();
 
      for (let i = 0; i < rows.length; i++) {
        if (rows[i].endsWith(" ") || rows[i].endsWith(" \r")) {
          const match = / +\r?$/.exec(rows[i]);
          const start = new Position(i + 1, match!.index + 1);
          const end = new Position(i + 1, rows[i].length + 1);
          const fix = EditHelper.deleteRange(file, start, end);
          const issue = Issue.atRange(file, start, end, this.getMessage(), this.getMetadata().key, this.conf.severity, fix);
          issues.push(issue);
        }
      }
    }
 
    return issues;
  }
}