All files / src/rules parser_missing_space.ts

98.36% Statements 240/244
97.26% Branches 71/73
100% Functions 13/13
98.36% Lines 240/244

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 2441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8922x 8922x 8922x 8922x 26625x 26625x 26625x 26625x 26625x 26625x 26625x 26625x 26625x 26625x 8922x 8922x 8485x 8485x 8922x 8922x 202x 202x 8922x 8922x 241x 241x 241x 241x 1241x 1241x 17x 17x 17x 17x 17x 1241x 241x 241x 241x 8922x 8922x 1241x 1241x 1241x 1241x 1241x 1241x 70x 70x 4x 70x 3x 66x 36x 63x 3x 27x 3x 24x 5x 21x 16x 16x 70x 70x 17x 17x 70x 1224x 1224x 1224x 8922x 8922x 3x 3x 8x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 1x 1x 2x 8x 2x 2x 8922x 8922x 5x 5x 5x 5x 5x 5x 5x 2x 2x 5x 3x 3x 3x 8922x 8922x 4x 4x 9x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 4x 1x 1x 4x 4x 9x 1x 1x 8922x 8922x 3x 3x 6x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 1x 1x 2x 2x 6x 2x 2x 8922x 8922x 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 8922x 8922x 36x 36x 94x 94x 94x 94x 94x 94x 2x 2x 94x 34x 34x 34x 8922x 8922x 16x 16x 16x 16x 16x 16x 16x 3x 3x 16x 13x 13x 13x 13x 13x 16x 3x 3x 16x 10x 10x 10x 8922x 8922x
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.ValueBody, Expressions.NewObject, Expressions.Cond,
      Expressions.ComponentCond, Expressions.MethodCallParam], true);
    let pos: Position | undefined = undefined;
    for (const f of found) {
      const type = f.get();
      if (type instanceof Expressions.CondSub) {
        pos = this.checkCondSub(f);
      } else if (type instanceof Expressions.ValueBody) {
        pos = this.checkValueBody(f);
      } else if (type instanceof Expressions.Cond) {
        pos = this.checkCond(f);
      } else if (type instanceof Expressions.ComponentCond) {
        pos = this.checkComponentCond(f);
      } else if (type instanceof Expressions.SQLCond) {
        pos = this.checkSQLCond(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 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;
  }
 
}