All files / src/rules parser_missing_space.ts

98.21% Statements 219/223
97.06% Branches 66/68
100% Functions 12/12
98.21% Lines 219/223

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 213 214 215 216 217 218 219 220 221 222 2231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7653x 7653x 7653x 7653x 22828x 22828x 22828x 22828x 22828x 22828x 22828x 22828x 22828x 22828x 7653x 7653x 7346x 7346x 7653x 7653x 139x 139x 7653x 7653x 172x 172x 172x 172x 866x 866x 14x 14x 14x 14x 14x 866x 172x 172x 172x 7653x 7653x 866x 866x 866x 866x 866x 46x 4x 46x 3x 42x 26x 39x 3x 13x 2x 10x 8x 8x 46x 46x 14x 14x 46x 852x 852x 852x 7653x 7653x 2x 2x 5x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 1x 1x 2x 5x 1x 1x 7653x 7653x 4x 4x 9x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 4x 1x 1x 4x 4x 9x 1x 1x 7653x 7653x 3x 3x 6x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 1x 1x 2x 2x 6x 2x 2x 7653x 7653x 3x 3x 7x 7x 7x 5x 5x 5x 5x 5x 5x 5x 1x 1x 4x 4x 4x 5x 5x 1x 1x 5x 5x 7x 1x 1x 7653x 7653x 26x 26x 68x 68x 68x 68x 68x 68x 2x 2x 68x 24x 24x 24x 7653x 7653x 8x 8x 8x 8x 8x 8x 8x 3x 3x 8x 5x 5x 5x 5x 5x 8x 2x 2x 8x 3x 3x 3x 7653x 7653x
import * as Expressions from "../abap/2_statements/expressions";
import {Issue} from "../issue";
import {Position} from "../position";
import {ABAPRule} from "./_abap_rule";
import {ExpressionNode, StatementNode, TokenNode} from "../abap/nodes";
import {BasicRuleConfig} from "./_basic_rule_config";
import {RuleTag, IRuleMetadata} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class ParserMissingSpaceConf extends BasicRuleConfig {
}
 
export class ParserMissingSpace extends ABAPRule {
  private conf = new ParserMissingSpaceConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "parser_missing_space",
      title: "Parser Error, missing space",
      shortDescription: `In special cases the ABAP language allows for not having spaces before or after string literals.
This rule makes sure the spaces are consistently required across the language.`,
      tags: [RuleTag.Syntax, RuleTag.Whitespace, RuleTag.SingleFile],
      badExample: `IF ( foo = 'bar').`,
      goodExample: `IF ( foo = 'bar' ).`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: ParserMissingSpaceConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    let start = new Position(0, 0);
    for (const statement of file.getStatements()) {
      const missing = this.missingSpace(statement);
      if (missing) {
        const message = "Missing space between string or character literal and parentheses";
        start = missing;
        const issue = Issue.atPosition(file, start, message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    return issues;
  }
 
  private missingSpace(statement: StatementNode): Position | undefined {
 
    const found = statement.findAllExpressionsMulti([Expressions.CondSub, Expressions.SQLCond,
      Expressions.ValueBody, Expressions.Cond, Expressions.ComponentCond, Expressions.MethodCallParam], true);
    let pos: Position | undefined = undefined;
    for (const f of found) {
      if (f.get() instanceof Expressions.CondSub) {
        pos = this.checkCondSub(f);
      } else if (f.get() instanceof Expressions.ValueBody) {
        pos = this.checkValueBody(f);
      } else if (f.get() instanceof Expressions.Cond) {
        pos = this.checkCond(f);
      } else if (f.get() instanceof Expressions.ComponentCond) {
        pos = this.checkComponentCond(f);
      } else if (f.get() instanceof Expressions.SQLCond) {
        pos = this.checkSQLCond(f);
      } else if (f.get() instanceof Expressions.MethodCallParam) {
        pos = this.checkMethodCallParam(f);
      }
 
      if (pos) {
        return pos;
      }
    }
 
    return undefined;
  }
 
  private checkSQLCond(cond: ExpressionNode): Position | undefined {
    const children = cond.getChildren();
    for (let i = 0; i < children.length; i++) {
      if (children[i].get() instanceof Expressions.SQLCond) {
        const current = children[i];
        const prev = children[i - 1].getLastToken();
        const next = children[i + 1].getFirstToken();
 
        if (prev.getStr() === "("
            && prev.getRow() === current.getFirstToken().getRow()
            && prev.getCol() + 1 === current.getFirstToken().getStart().getCol()) {
          return current.getFirstToken().getStart();
        }
 
        if (next.getStr() === ")"
            && next.getRow() === current.getLastToken().getRow()
            && next.getCol() === current.getLastToken().getEnd().getCol()) {
          return current.getLastToken().getEnd();
        }
      }
    }
    return undefined;
  }
 
  private checkCondSub(cond: ExpressionNode): Position | undefined {
    const children = cond.getChildren();
    for (let i = 0; i < children.length; i++) {
      if (children[i].get() instanceof Expressions.Cond) {
        const current = children[i];
        const prev = children[i - 1].getLastToken();
        const next = children[i + 1].getFirstToken();
 
        if (prev.getStr() === "("
            && prev.getRow() === current.getFirstToken().getRow()
            && prev.getCol() + 1 === current.getFirstToken().getStart().getCol()) {
          return current.getFirstToken().getStart();
        }
 
        if (next.getStr() === ")"
            && next.getRow() === current.getLastToken().getRow()
            && next.getCol() === current.getLastToken().getEnd().getCol()) {
          return current.getLastToken().getEnd();
        }
 
      }
    }
    return undefined;
  }
 
  private checkComponentCond(cond: ExpressionNode): Position | undefined {
    const children = cond.getChildren();
    for (let i = 0; i < children.length; i++) {
      if (children[i].get() instanceof Expressions.ComponentCond) {
        const current = children[i];
        const prev = children[i - 1].getLastToken();
        const next = children[i + 1].getFirstToken();
 
        if (prev.getStr() === "("
            && prev.getRow() === current.getFirstToken().getRow()
            && prev.getCol() + 1 === current.getFirstToken().getStart().getCol()) {
          return current.getFirstToken().getStart();
        }
 
        if (next.getStr() === ")"
            && next.getRow() === current.getLastToken().getRow()
            && next.getCol() === current.getLastToken().getEnd().getCol()) {
          return current.getLastToken().getEnd();
        }
 
      }
    }
    return undefined;
  }
 
  private checkValueBody(vb: ExpressionNode): Position | undefined {
    const children = vb.getChildren();
    for (let i = 0; i < children.length; i++) {
      const current = children[i];
 
      if (current instanceof TokenNode) {
        const prev = children[i - 1]?.getLastToken();
        const next = children[i + 1]?.getFirstToken();
 
        if (current.getFirstToken().getStr() === "("
            && next
            && next.getRow() === current.getLastToken().getRow()
            && next.getCol() === current.getLastToken().getEnd().getCol()) {
          return current.getFirstToken().getStart();
        }
 
        if (current.getFirstToken().getStr() === ")"
            && prev
            && prev.getRow() === current.getFirstToken().getRow()
            && prev.getEnd().getCol() === current.getFirstToken().getStart().getCol()) {
          return current.getLastToken().getEnd();
        }
 
      }
    }
    return undefined;
  }
 
  private checkCond(cond: ExpressionNode): Position | undefined {
    const children = cond.getAllTokens();
    for (let i = 0; i < children.length - 1; i++) {
      const current = children[i];
      const next = children[i + 1];
 
      if (next.getStr().startsWith("'")
          && next.getRow() === current.getRow()
          && next.getCol() === current.getEnd().getCol()) {
        return current.getEnd();
      }
    }
 
    return undefined;
  }
 
  private checkMethodCallParam(call: ExpressionNode): Position | undefined {
    const children = call.getChildren();
 
    {
      const first = children[0].getFirstToken();
      const second = children[1].getFirstToken();
      if (first.getRow() === second.getRow()
          && first.getCol() + 1 === second.getStart().getCol()) {
        return second.getStart();
      }
    }
 
    {
      const first = children[children.length - 2].getLastToken();
      const second = children[children.length - 1].getFirstToken();
      if (first.getRow() === second.getRow()
          && first.getEnd().getCol() === second.getStart().getCol()) {
        return second.getStart();
      }
    }
 
    return undefined;
  }
 
}