Beispiel #1
0
export function ObjectProperty(node: Object) {
  this.printJoin(node.decorators, node);

  if (node.computed) {
    this.token("[");
    this.print(node.key, node);
    this.token("]");
  } else {
    // print `({ foo: foo = 5 } = {})` as `({ foo = 5 } = {});`
    if (
      t.isAssignmentPattern(node.value) &&
      t.isIdentifier(node.key) &&
      node.key.name === node.value.left.name
    ) {
      this.print(node.value, node);
      return;
    }

    this.print(node.key, node);

    // shorthand!
    if (
      node.shorthand &&
      (t.isIdentifier(node.key) &&
        t.isIdentifier(node.value) &&
        node.key.name === node.value.name)
    ) {
      return;
    }
  }

  this.token(":");
  this.space();
  this.print(node.value, node);
}
  // inherits.
  visitPattern(node, options, callback) {
    if (!node) {
      return;
    }

    // Visit type annotations.
    this._checkIdentifierOrVisit(node.typeAnnotation);
    if (t.isAssignmentPattern(node)) {
      this._checkIdentifierOrVisit(node.left.typeAnnotation);
    }

    // Overwrite `super.visitPattern(node, options, callback)` in order to not visit `ArrayPattern#typeAnnotation` and `ObjectPattern#typeAnnotation`.
    if (typeof options === "function") {
      callback = options;
      options = { processRightHandNodes: false };
    }

    const visitor = new PatternVisitor(this.options, node, callback);
    visitor.visit(node);

    // Process the right hand nodes recursively.
    if (options.processRightHandNodes) {
      visitor.rightHandNodes.forEach(this.visit, this);
    }
  }
Beispiel #3
0
function getVariableDeclarators(node: Object): Object[] | Object {
  if (t.isIdentifier(node)) {
    return t.variableDeclarator(t.identifier(node.name));
  }

  if (t.isObjectProperty(node)) {
    return getVariableDeclarators(node.value);
  }
  if (t.isRestElement(node)) {
    return getVariableDeclarators(node.argument);
  }

  if (t.isAssignmentPattern(node)) {
    return getVariableDeclarators(node.left);
  }

  if (t.isArrayPattern(node)) {
    return node.elements.reduce(
      (acc, element) => acc.concat(getVariableDeclarators(element)),
      []
    );
  }
  if (t.isObjectPattern(node)) {
    return node.properties.reduce(
      (acc, property) => acc.concat(getVariableDeclarators(property)),
      []
    );
  }
  return [];
}
 referencer.prototype.visitFunction = function(node) {
   var typeParamScope;
   if (node.typeParameters) {
     typeParamScope = nestTypeParamScope.call(this, this.scopeManager, node);
   }
   if (node.returnType) {
     checkIdentifierOrVisit.call(this, node.returnType);
   }
   // only visit if function parameters have types
   if (node.params) {
     for (var i = 0; i < node.params.length; i++) {
       var param = node.params[i];
       if (param.typeAnnotation) {
         checkIdentifierOrVisit.call(this, param);
       } else if (t.isAssignmentPattern(param)) {
         if (param.left.typeAnnotation) {
           checkIdentifierOrVisit.call(this, param.left);
         }
       }
     }
   }
   // set ArrayPattern/ObjectPattern visitor keys back to their original. otherwise
   // escope will traverse into them and include the identifiers within as declarations
   estraverse.VisitorKeys.ObjectPattern = ["properties"];
   estraverse.VisitorKeys.ArrayPattern = ["elements"];
   visitFunction.call(this, node);
   // set them back to normal...
   estraverse.VisitorKeys.ObjectPattern = t.VISITOR_KEYS.ObjectPattern;
   estraverse.VisitorKeys.ArrayPattern = t.VISITOR_KEYS.ArrayPattern;
   if (typeParamScope) {
     this.close(node);
   }
 };
Beispiel #5
0
 .map(element => {
   return {
     name: t.isAssignmentPattern(element)
       ? element.left.name
       : element.name || (element.argument && element.argument.name),
     location: element.loc,
   };
 })
Beispiel #6
0
      (acc, param) => {
        acc.done =
          acc.done || t.isAssignmentPattern(param) || t.isRestElement(param);

        if (!acc.done) {
          acc.params.push(path.scope.generateUidIdentifier("x"));
        }

        return acc;
      },
Beispiel #7
0
export default function(node): number {
  const params: Array<Object> = node.params;
  for (let i = 0; i < params.length; i++) {
    const param = params[i];
    if (t.isAssignmentPattern(param) || t.isRestElement(param)) {
      return i;
    }
  }
  return params.length;
}
function getAssignmentTarget(node, bindings) {
  if (t.isObjectPattern(node)) {
    for (const property of node.properties) {
      if (t.isRestElement(property)) {
        property.argument = getAssignmentTarget(property.argument, bindings);
      } else {
        property.value = getAssignmentTarget(property.value, bindings);
      }
    }

    return node;
  }

  if (t.isArrayPattern(node)) {
    for (const [i, element] of node.elements.entries()) {
      node.elements[i] = getAssignmentTarget(element, bindings);
    }

    return node;
  }

  if (t.isAssignmentPattern(node)) {
    node.left = getAssignmentTarget(node.left, bindings);

    return node;
  }

  if (t.isRestElement(node)) {
    node.argument = getAssignmentTarget(node.argument, bindings);

    return node;
  }

  if (t.isIdentifier(node)) {
    return bindings.includes(node.name)
      ? node
      : t.memberExpression(t.identifier("self"), node);
  }

  return node;
}