All files / src/rules _compare.ts

100% Statements 52/52
100% Branches 20/20
100% Functions 3/3
100% Lines 52/52

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 531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 137x 137x 137x 137x 137x 137x 137x 11x 11x 126x 126x 126x 126x 126x 126x 126x 1x 1x 160x 160x 160x 160x 160x 1x 1x 127x 127x 237x 237x 237x 237x 237x 9x 9x 228x 127x 127x  
import * as Expressions from "../abap/2_statements/expressions";
import * as Tokens from "../abap/1_lexer/tokens";
import {ExpressionNode} from "../abap/nodes";
import {countLeadingNot} from "../abap/5_syntax/_key_compare";
 
export interface IComparison {
  /** an odd number of leading NOT */
  negated: boolean;
  left: ExpressionNode;
  /** upper case, without blanks, e.g. "=", "NE" or "<>" */
  operator: string;
  right: ExpressionNode;
}
 
/** A Compare of two operands with a comparison operator, "a = b", "NOT a NE b" etc., else undefined */
export function splitComparison(compare: ExpressionNode): IComparison | undefined {
  const nots = countLeadingNot(compare);
  const rest = compare.getChildren().slice(nots);
  if (rest.length !== 3
      || !(rest[0] instanceof ExpressionNode) || !(rest[0].get() instanceof Expressions.Source)
      || !(rest[1] instanceof ExpressionNode) || !(rest[1].get() instanceof Expressions.CompareOperator)
      || !(rest[2] instanceof ExpressionNode) || !(rest[2].get() instanceof Expressions.Source)) {
    return undefined;
  }
  return {
    negated: nots % 2 === 1,
    left: rest[0],
    operator: rest[1].concatTokens().toUpperCase().replace(/ /g, ""),
    right: rest[2],
  };
}
 
/** The node calls a method or creates an object, so two evaluations can give different values */
export function hasCall(node: ExpressionNode): boolean {
  return node.findFirstExpression(Expressions.MethodCall) !== undefined
    || node.findFirstExpression(Expressions.MethodCallChain) !== undefined
    || node.findFirstExpression(Expressions.NewObject) !== undefined;
}
 
/** The tokens of the node joined by blanks, in upper case except for literals and string templates */
export function normalizedTokens(node: ExpressionNode): string {
  return node.getAllTokens().map(token => {
    if (token instanceof Tokens.StringToken
        || token instanceof Tokens.StringTemplate
        || token instanceof Tokens.StringTemplateBegin
        || token instanceof Tokens.StringTemplateMiddle
        || token instanceof Tokens.StringTemplateEnd) {
      return token.getStr();
    }
    return token.getStr().toUpperCase();
  }).join(" ");
}