@babel/types#BinaryExpression TypeScript Examples

The following examples show how to use @babel/types#BinaryExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: context-free.ts    From next-core with GNU General Public License v3.0 6 votes vote down vote up
// https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
export function ApplyStringOrNumericBinaryOperator(
  leftValue: number,
  operator: BinaryExpression["operator"] | "|>",
  rightValue: number
): unknown {
  switch (operator) {
    case "+":
      return leftValue + rightValue;
    case "-":
      return leftValue - rightValue;
    case "/":
      return leftValue / rightValue;
    case "%":
      return leftValue % rightValue;
    case "*":
      return leftValue * rightValue;
    case "**":
      return leftValue ** rightValue;
    case "==":
      return leftValue == rightValue;
    case "===":
      return leftValue === rightValue;
    case "!=":
      return leftValue != rightValue;
    case "!==":
      return leftValue !== rightValue;
    case ">":
      return leftValue > rightValue;
    case "<":
      return leftValue < rightValue;
    case ">=":
      return leftValue >= rightValue;
    case "<=":
      return leftValue <= rightValue;
  }
  throw new SyntaxError(`Unsupported binary operator \`${operator}\``);
}
Example #2
Source File: context-free.ts    From next-core with GNU General Public License v3.0 6 votes vote down vote up
// https://tc39.es/ecma262/#sec-assignment-operators
export function ApplyStringOrNumericAssignment(
  leftValue: string | number,
  operator: string,
  rightValue: string | number
): unknown {
  switch (operator) {
    case "+=":
    case "-=":
    case "*=":
    case "/=":
    case "%=":
    case "**=":
      return ApplyStringOrNumericBinaryOperator(
        leftValue as number,
        operator.substr(0, operator.length - 1) as BinaryExpression["operator"],
        rightValue as number
      );
  }

  throw new SyntaxError(`Unsupported assignment operator \`${operator}\``);
}