All files / src/rules parser_missing_space.ts

97.7% Statements 256/262
96.25% Branches 77/80
100% Functions 14/14
97.7% Lines 256/262

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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 2621x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9361x 9361x 9361x 9361x 27941x 27941x 27941x 27941x 27941x 27941x 27941x 27941x 27941x 27941x 9361x 9361x 8898x 8898x 9361x 9361x 216x 216x 9361x 9361x 261x 261x 261x 261x 1379x 1379x 18x 18x 18x 18x 18x 1379x 261x 261x 261x 9361x 9361x 1379x 1379x 1379x 1379x 1379x 1379x 76x 76x 36x 76x 4x 40x 3x 36x 2x 33x 3x 31x 3x 28x 5x 25x 20x 20x 76x 76x 18x 18x 76x 1361x 1361x 1361x 9361x 9361x 3x 3x 8x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 1x 1x 2x 8x 2x 2x 9361x 9361x 5x 5x 5x 5x 5x 5x 5x 2x 2x 5x 3x 3x 3x 9361x 9361x 4x 4x 9x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 4x 1x 1x 4x 4x 9x 1x 1x 9361x 9361x 2x 2x 5x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 1x 1x 2x 2x 5x 1x 1x 9361x 9361x 3x 3x 10x 10x 10x 10x 10x 10x     10x 3x 3x 3x 9361x 9361x 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 9361x 9361x 36x 36x 94x 94x 94x 94x 94x 94x 2x 2x 94x 34x 34x 34x 9361x 9361x 20x 20x 20x 20x 20x 20x 20x 3x 3x 20x 17x 17x 17x 17x 17x 20x 4x 4x 20x 13x 13x 13x 9361x 9361x
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";
 
// todo: this rule needs refactoring
 
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.ValueBodyLine, Expressions.NewObject, Expressions.Cond,
      Expressions.ComponentCond, Expressions.ComponentCondSub, Expressions.MethodCallParam], true);
    let pos: Position | undefined = undefined;
    for (const f of found) {
      const type = f.get();
      if (type instanceof Expressions.Cond) {
        pos = this.checkCond(f);
      } else if (type instanceof Expressions.CondSub) {
        pos = this.checkCondSub(f);
      } else if (type instanceof Expressions.ComponentCond) {
        pos = this.checkComponentCond(f);
      } else if (type instanceof Expressions.ComponentCondSub) {
        pos = this.checkComponentCondSub(f);
      } else if (type instanceof Expressions.SQLCond) {
        pos = this.checkSQLCond(f);
      } else if (type instanceof Expressions.ValueBodyLine) {
        pos = this.checkValueBodyLine(f);
      } else if (type instanceof Expressions.NewObject) {
        pos = this.checkNewObject(f);
      } else if (type 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 checkNewObject(cond: ExpressionNode): Position | undefined {
    const children = cond.getChildren();
 
    {
      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;
  }
 
  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 checkComponentCondSub(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 checkComponentCond(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 checkValueBodyLine(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;
  }
 
}