All files / src/rules parser_missing_space.ts

97.88% Statements 278/284
96.62% Branches 86/89
100% Functions 15/15
97.88% Lines 278/284

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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 2841x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10327x 10327x 10327x 10327x 30829x 30829x 30829x 30829x 30829x 30829x 30829x 30829x 30829x 30829x 10327x 10327x 9814x 9814x 10327x 10327x 241x 241x 10327x 10327x 284x 284x 284x 284x 1474x 1474x 19x 19x 19x 19x 19x 1474x 284x 284x 284x 10327x 10327x 1474x 1474x 1474x 1474x 1474x 1474x 1474x 1474x 233x 233x 37x 233x 156x 196x 4x 40x 3x 36x 2x 33x 3x 31x 4x 28x 4x 24x 20x 20x 233x 233x 19x 19x 233x 1455x 1455x 1455x 10327x 10327x 3x 3x 8x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 1x 1x 2x 8x 2x 2x 10327x 10327x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 3x 3x 3x 10327x 10327x 4x 4x 9x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 4x 1x 1x 4x 4x 9x 1x 1x 10327x 10327x 2x 2x 5x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 1x 1x 2x 2x 5x 1x 1x 10327x 10327x 3x 3x 10x 10x 10x 10x 10x 10x     10x 3x 3x 3x 10327x 10327x 4x 4x 10x 10x 10x 7x 7x 7x 7x 7x 7x 7x 1x 1x 6x 6x 6x 7x 7x 1x 1x 7x 7x 10x 2x 2x 10327x 10327x 37x 37x 96x 96x 96x 96x 96x 96x 2x 2x 96x 35x 35x 35x 10327x 10327x 156x 156x 131x 131x 25x 25x 25x 25x 25x 156x 156x 2x 2x 23x 23x 23x 10327x 10327x 20x 20x 20x 20x 20x 20x 20x 3x 3x 20x 17x 17x 17x 17x 17x 20x 4x 4x 20x 13x 13x 13x 10327x 10327x
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.Source,
      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.Source) {
        pos = this.checkSource(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 checkSource(cond: ExpressionNode): Position | undefined {
    const children = cond.getAllTokens();
    if (children.length < 2) {
      return undefined;
    }
 
    const nextLast = children[children.length - 2];
    const last = children[children.length - 1];
 
    if (nextLast.getStr().startsWith("'")
        && nextLast.getRow() === last.getRow()
        && nextLast.getEnd().getCol() === last.getStart().getCol()) {
      return last.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;
  }
 
}