How to use the @babel/types.binaryExpression function in @babel/types

To help you get started, we’ve selected a few @babel/types examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github babel / babel / packages / babel-plugin-transform-es2015-parameters / src / rest.js View on Github external
let arrKey = key;
  let arrLen = len;
  if (node.params.length) {
    // this method has additional params, so we need to subtract
    // the index of the current argument position from the
    // position in the array that we want to populate
    arrKey = t.binaryExpression("-", key, start);

    // we need to work out the size of the array that we're
    // going to store all the rest parameters
    //
    // we need to add a check to avoid constructing the array
    // with <0 if there are less arguments than params as it'll
    // cause an error
    arrLen = t.conditionalExpression(
      t.binaryExpression(">", len, start),
      t.binaryExpression("-", len, start),
      t.numericLiteral(0),
    );
  }

  const loop = buildRest({
    ARGUMENTS: argsId,
    ARRAY_KEY: arrKey,
    ARRAY_LEN: arrLen,
    START: start,
    ARRAY: rest,
    KEY: key,
    LEN: len,
  });

  if (state.deopted) {
github facebook / prepack / src / serializer / ResidualHeapSerializer.js View on Github external
} else if (value.mightHaveBeenDeleted()) {
        // Let's try for a little peephole optimization, if __empty is a branch of a conditional, and the other side cannot be __empty
        let condition;
        if (value instanceof AbstractValue && value.kind === "conditional") {
          let [c, x, y] = value.args;
          if (x instanceof EmptyValue && !y.mightHaveBeenDeleted()) {
            if (c instanceof AbstractValue && c.kind === "!") condition = this.serializeValue(c.args[0]);
            else condition = t.unaryExpression("!", this.serializeValue(c));
            serializedValue = this.serializeValue(y);
          } else if (y instanceof EmptyValue && !x.mightHaveBeenDeleted()) {
            condition = this.serializeValue(c);
            serializedValue = this.serializeValue(x);
          }
        }
        if (condition === undefined) {
          condition = t.binaryExpression("!==", serializedValue, this._serializeEmptyValue());
        }
        let assignment = t.expressionStatement(t.assignmentExpression("=", location, serializedValue));
        let deletion = null;
        if (deleteIfMightHaveBeenDeleted) {
          invariant(location.type === "MemberExpression");
          deletion = t.expressionStatement(
            t.unaryExpression("delete", ((location: any): BabelNodeMemberExpression), true)
          );
        }
        return t.ifStatement(condition, assignment, deletion);
      }
    }

    return t.expressionStatement(t.assignmentExpression("=", location, this.serializeValue(value)));
  }
github babel / babel / packages / babel-plugin-transform-es2015-parameters / src / rest.js View on Github external
function optimiseIndexGetter(path, argsId, offset) {
  const offsetLiteral = t.numericLiteral(offset);
  let index;

  if (t.isNumericLiteral(path.parent.property)) {
    index = t.numericLiteral(path.parent.property.value + offset);
  } else if (offset === 0) {
    // Avoid unnecessary '+ 0'
    index = path.parent.property;
  } else {
    index = t.binaryExpression("+", path.parent.property, offsetLiteral);
  }

  const { scope } = path;
  if (!scope.isPure(index)) {
    const temp = scope.generateUidIdentifierBasedOnNode(index);
    scope.push({ id: temp, kind: "var" });
    path.parentPath.replaceWith(
      restIndexImpure({
        ARGUMENTS: argsId,
        OFFSET: offsetLiteral,
        INDEX: index,
        REF: temp,
      }),
    );
  } else {
    const parentPath = path.parentPath;
github facebook / prepack / src / serializer / ResidualOperationSerializer.js View on Github external
{ path, value }: OperationDescriptorData,
    [o, v, e, keyKey]: Array,
    context?: SerializationContext,
    valuesToProcess?: Set
  ): BabelNodeStatement {
    invariant(value instanceof AbstractValue);
    invariant(path instanceof AbstractValue);
    let mightHaveBeenDeleted = value.mightHaveBeenDeleted();
    let mightBeUndefined = value.mightBeUndefined();
    invariant(path.operationDescriptor !== undefined);
    let lh = this.serializeExpression(path.operationDescriptor, [o, keyKey], context, valuesToProcess);
    let r = t.expressionStatement(t.assignmentExpression("=", (lh: any), v));
    if (mightHaveBeenDeleted) {
      // If v === __empty || (v === undefined  && !(key.key in o))  then delete it
      let emptyTest = t.binaryExpression("===", v, e);
      let undefinedTest = t.binaryExpression("===", v, voidExpression);
      let inTest = t.unaryExpression("!", t.binaryExpression("in", keyKey, o));
      let guard = t.logicalExpression("||", emptyTest, t.logicalExpression("&&", undefinedTest, inTest));
      let deleteIt = t.expressionStatement(t.unaryExpression("delete", (lh: any)));
      return t.ifStatement(mightBeUndefined ? emptyTest : guard, deleteIt, r);
    }
    return r;
  }
github gajus / flow-runtime / packages / babel-plugin-flow-runtime / src / patternMatchVisitors.js View on Github external
else if (typeAnnotation.isNumberLiteralTypeAnnotation()) {
    return t.binaryExpression(
      '===',
      replacement,
      t.NumericLiteral(typeAnnotation.node.value)
    );
  }
  else if (typeAnnotation.isBooleanLiteralTypeAnnotation()) {
    return t.binaryExpression(
      '===',
      replacement,
      t.booleanLiteral(typeAnnotation.node.value)
    );
  }
  else if (typeAnnotation.isNullLiteralTypeAnnotation()) {
    return t.binaryExpression(
      '===',
      replacement,
      t.nullLiteral()
    );
  }
  else if (typeAnnotation.isUnionTypeAnnotation()) {
    return typeAnnotation.get('types').reduce((last, item) => {
      if (last === null) {
        return inlineTest(context, item, replacement);
      }
      else {
        return t.logicalExpression(
          '||',
          last,
          inlineTest(context, item, replacement)
        );
github gajus / flow-runtime / packages / babel-plugin-flow-runtime / src / patternMatchVisitors.js View on Github external
else if (typeAnnotation.isStringLiteralTypeAnnotation()) {
    return t.binaryExpression(
      '===',
      replacement,
      t.stringLiteral(typeAnnotation.node.value)
    );
  }
  else if (typeAnnotation.isNumberLiteralTypeAnnotation()) {
    return t.binaryExpression(
      '===',
      replacement,
      t.NumericLiteral(typeAnnotation.node.value)
    );
  }
  else if (typeAnnotation.isBooleanLiteralTypeAnnotation()) {
    return t.binaryExpression(
      '===',
      replacement,
      t.booleanLiteral(typeAnnotation.node.value)
    );
  }
  else if (typeAnnotation.isNullLiteralTypeAnnotation()) {
    return t.binaryExpression(
      '===',
      replacement,
      t.nullLiteral()
    );
  }
  else if (typeAnnotation.isUnionTypeAnnotation()) {
    return typeAnnotation.get('types').reduce((last, item) => {
      if (last === null) {
        return inlineTest(context, item, replacement);
github facebook / prepack / src / serializer / ResidualOperationSerializer.js View on Github external
_serializeDerivedAbstractInvariant(
    {  }: OperationDescriptorData,
    [typeOfStringNode, typeofNode]: Array
  ): BabelNodeExpression {
    let typeofString = ((typeOfStringNode: any): BabelNodeStringLiteral).value;
    let condition = t.binaryExpression("!==", t.unaryExpression("typeof", typeofNode), t.stringLiteral(typeofString));
    if (typeofString === "object") {
      condition = t.logicalExpression(
        "&&",
        condition,
        t.binaryExpression("!==", t.unaryExpression("typeof", typeofNode), t.stringLiteral("function"))
      );
      condition = t.logicalExpression("||", condition, t.binaryExpression("===", typeofNode, nullExpression));
    }
    return condition;
  }
github ismail-codar / fidan / packages / deprecated-babel-plugin-transform-jsx / build / modify.js View on Github external
const fidanCall = (left, right, operator) => {
    if (operator === "=")
        return t.callExpression(left, [right]);
    else {
        operator = operator.substr(0, 1);
        return t.callExpression(left, [
            t.binaryExpression(operator, left, right)
        ]);
    }
};
const assignmentExpressionToCallCompute = (expression, fComputeParameters) => {
github salesforce / lwc / packages / lwc-template-compiler / src / codegen / index.ts View on Github external
if (!element.if) {
            return babelNode;
        }

        if (!testExpression) {
            testExpression = bindExpression(element.if!, element).expression;
        }

        let leftExpression: t.Expression;
        const modifier = element.ifModifier!;
        if (modifier === 'true') {
            leftExpression = testExpression;
        } else if (modifier === 'false') {
            leftExpression = t.unaryExpression('!', testExpression);
        } else if (modifier === 'strict-true') {
            leftExpression = t.binaryExpression('===', testExpression, t.booleanLiteral(true));
        } else {
            throw generateCompilerError(TemplateErrors.UNKNOWN_IF_MODIFIER, {
                messageArgs: [modifier]
            });
        }

        return t.conditionalExpression(
            leftExpression,
            babelNode,
            falseValue,
        );
    }