All files / src/rules line_length.ts

92.53% Statements 62/67
84.61% Branches 11/13
100% Functions 6/6
92.53% Lines 62/67

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 681x 1x 1x 1x 1x 1x 29365x 29365x 29365x 29365x 29365x 1x 14683x 14683x 14683x 14683x 14683x 128631x 128631x 128631x 128631x 128631x 128631x 128631x 128631x 128631x 128631x 128631x 128631x 14683x 14683x 14415x 14415x 14683x 14683x 265x 265x 14683x 14683x 295x 295x 295x 295x 295x 295x 2x 2x 295x 295x 295x 1996x 1996x 2x 2x 1996x       1996x 1996x     1996x 295x 295x 14683x 14683x  
import {ABAPFile} from "../abap/abap_file";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {RuleTag} from "./_irule";
 
export class LineLengthConf extends BasicRuleConfig {
  /** Maximum line length in characters, trailing whitespace ignored */
  public length: number = 120;
  public maxIssuesPerFile: number | undefined = 10;
}
 
export class LineLength extends ABAPRule {
 
  private conf = new LineLengthConf();
 
  public getMetadata() {
    return {
      key: "line_length",
      title: "Line length",
      shortDescription: `Detects lines exceeding the provided maximum length.`,
      extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#stick-to-a-reasonable-line-length
https://docs.abapopenchecks.org/checks/04/`,
      tags: [RuleTag.Styleguide, RuleTag.SingleFile],
      badExample: "DATA long_text TYPE string VALUE 'this line keeps going until it is longer than the configured maximum'.",
      goodExample: `DATA long_text TYPE string.
long_text = 'split long expressions across multiple lines'.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: LineLengthConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
    // maximum line length in abap files
    const maxLineLength: number = 255;
 
    let max = this.getConfig().maxIssuesPerFile;
    if (max === undefined || max < 1) {
      max = 10;
    }
 
    const array = file.getRawRows();
    for (let rowIndex = 0; rowIndex < array.length; rowIndex++) {
      const row = array[rowIndex].replace("\r", "");
      if (row.length > maxLineLength) {
        const message = `Maximum allowed line length of ${maxLineLength} exceeded, currently ${row.length}`;
        issues.push(Issue.atRow(file, rowIndex + 1, message, this.getMetadata().key, this.conf.severity));
      } else if (row.length > this.conf.length) {
        const message = `Reduce line length to max ${this.conf.length}, currently ${row.length}`;
        issues.push(Issue.atRow(file, rowIndex + 1, message, this.getMetadata().key, this.conf.severity));
      }
 
      if (issues.length >= max) {
        break;
      }
    }
    return issues;
  }
 
}