All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.shiftleft.js2cpg.astcreation.AstHelpers.scala Maven / Gradle / Ivy

There is a newer version: 0.3.3
Show newest version
package io.shiftleft.js2cpg.astcreation

import com.oracle.js.parser.TokenType
import io.shiftleft.codepropertygraph.generated.Operators

object AstHelpers {
  def getBinaryOperation(op: TokenType): String = op match {
    // arithmetic
    case TokenType.ADD => Operators.addition
    case TokenType.SUB => Operators.subtraction
    case TokenType.MUL => Operators.multiplication
    case TokenType.DIV => Operators.division
    case TokenType.MOD => Operators.modulo
    case TokenType.EXP => Operators.exponentiation
    // comparison
    case TokenType.LT         => Operators.lessThan
    case TokenType.GT         => Operators.greaterThan
    case TokenType.LE         => Operators.lessEqualsThan
    case TokenType.GE         => Operators.greaterEqualsThan
    case TokenType.EQ         => Operators.equals
    case TokenType.EQ_STRICT  => Operators.equals
    case TokenType.NE         => Operators.notEquals
    case TokenType.NE_STRICT  => Operators.notEquals
    case TokenType.INSTANCEOF => Operators.instanceOf
    // logical
    case TokenType.AND => Operators.logicalAnd
    case TokenType.OR  => Operators.logicalOr
    case TokenType.NOT => Operators.logicalNot
    // bitwise
    case TokenType.BIT_AND => Operators.and
    case TokenType.BIT_OR  => Operators.or
    case TokenType.BIT_NOT => Operators.not
    case TokenType.BIT_XOR => Operators.xor
    case TokenType.SHL     => Operators.shiftLeft
    case TokenType.SAR     => Operators.arithmeticShiftRight
    case TokenType.SHR     => Operators.logicalShiftRight
    // assignment operators
    case TokenType.ASSIGN         => Operators.assignment
    case TokenType.ASSIGN_INIT    => Operators.assignment
    case TokenType.ASSIGN_ADD     => Operators.assignmentPlus
    case TokenType.ASSIGN_SUB     => Operators.assignmentMinus
    case TokenType.ASSIGN_MUL     => Operators.assignmentMultiplication
    case TokenType.ASSIGN_DIV     => Operators.assignmentDivision
    case TokenType.ASSIGN_MOD     => Operators.assignmentModulo
    case TokenType.ASSIGN_EXP     => Operators.assignmentExponentiation
    case TokenType.ASSIGN_BIT_AND => Operators.assignmentAnd
    case TokenType.ASSIGN_BIT_OR  => Operators.assignmentOr
    case TokenType.ASSIGN_BIT_XOR => Operators.assignmentXor
    case TokenType.ASSIGN_SHL     => Operators.assignmentShiftLeft
    case TokenType.ASSIGN_SAR     => Operators.assignmentArithmeticShiftRight
    case TokenType.ASSIGN_SHR     => Operators.assignmentLogicalShiftRight
    // others
    case TokenType.COMMALEFT      => ".commaleft"
    case TokenType.IN             => ".in"
    case TokenType.NULLISHCOALESC => ".nullishcoalesc"
    // TODO: this list is still incomplete:
    case other => throw new NotImplementedError(s"TokenType '$other' not yet supported!")
  }

  def getUnaryOperation(op: String): String = op match {
    case "new"             => "constructor"
    case "!"               => Operators.logicalNot
    case "++"              => Operators.preIncrement
    case "--"              => Operators.preDecrement
    case "-"               => Operators.minus
    case "+"               => Operators.plus
    case "~"               => ".bitNot"
    case "incpostfix"      => Operators.postIncrement
    case "decpostfix"      => Operators.postDecrement
    case "typeof"          => Operators.instanceOf
    case "delete"          => Operators.delete
    case "yield"           => ".yield"
    case "yield_star"      => ".yieldStar"
    case "ident"           => ".ident"
    case "void"            => ".void"
    case "await"           => ".await"
    case "string"          => ".string"
    case "spread_array"    => ".spreadArray"
    case "spread_object"   => ".spreadObject"
    case "spread_argument" => ".spreadArgument"
    // TODO: this list is still incomplete:
    case other => throw new NotImplementedError(s"Unary operator '$other' not yet supported!")
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy