Пример #1
0
 isPure(node, constantsOnly?: boolean) {
   if (t.isIdentifier(node)) {
     const binding = this.getBinding(node.name);
     if (!binding) return false;
     if (constantsOnly) return binding.constant;
     return true;
   } else if (t.isClass(node)) {
     if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
       return false;
     }
     return this.isPure(node.body, constantsOnly);
   } else if (t.isClassBody(node)) {
     for (const method of node.body) {
       if (!this.isPure(method, constantsOnly)) return false;
     }
     return true;
   } else if (t.isBinary(node)) {
     return (
       this.isPure(node.left, constantsOnly) &&
       this.isPure(node.right, constantsOnly)
     );
   } else if (t.isArrayExpression(node)) {
     for (const elem of (node.elements: Array<Object>)) {
       if (!this.isPure(elem, constantsOnly)) return false;
     }
     return true;
   } else if (t.isObjectExpression(node)) {
     for (const prop of (node.properties: Array<Object>)) {
       if (!this.isPure(prop, constantsOnly)) return false;
     }
     return true;
   } else if (t.isClassMethod(node)) {
     if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
     if (node.kind === "get" || node.kind === "set") return false;
     return true;
   } else if (t.isProperty(node)) {
     if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
     return this.isPure(node.value, constantsOnly);
   } else if (t.isUnaryExpression(node)) {
     return this.isPure(node.argument, constantsOnly);
   } else if (t.isTaggedTemplateExpression(node)) {
     return (
       t.matchesPattern(node.tag, "String.raw") &&
       !this.hasBinding("String", true) &&
       this.isPure(node.quasi, constantsOnly)
     );
   } else if (t.isTemplateLiteral(node)) {
     for (const expression of (node.expressions: Array<Object>)) {
       if (!this.isPure(expression, constantsOnly)) return false;
     }
     return true;
   } else {
     return t.isPureish(node);
   }
 }
Пример #2
0
export function ConditionalExpression(node: Object, parent: Object): boolean {
  if (
    t.isUnaryLike(parent) ||
    t.isBinary(parent) ||
    t.isConditionalExpression(parent, { test: node }) ||
    t.isAwaitExpression(parent) ||
    t.isOptionalMemberExpression(parent) ||
    t.isTaggedTemplateExpression(parent) ||
    t.isTSTypeAssertion(parent) ||
    t.isTSAsExpression(parent)
  ) {
    return true;
  }

  return UnaryLike(node, parent);
}
Пример #3
0
// Walk up the print stack to determine if our node can come first
// in statement.
function isFirstInStatement(
  printStack: Array<Object>,
  { considerArrow = false, considerDefaultExports = false } = {},
): boolean {
  let i = printStack.length - 1;
  let node = printStack[i];
  i--;
  let parent = printStack[i];
  while (i > 0) {
    if (
      t.isExpressionStatement(parent, { expression: node }) ||
      t.isTaggedTemplateExpression(parent) ||
      (considerDefaultExports &&
        t.isExportDefaultDeclaration(parent, { declaration: node })) ||
      (considerArrow && t.isArrowFunctionExpression(parent, { body: node }))
    ) {
      return true;
    }

    if (
      t.isCallExpression(parent, { callee: node }) ||
      (t.isSequenceExpression(parent) && parent.expressions[0] === node) ||
      t.isMemberExpression(parent, { object: node }) ||
      t.isConditional(parent, { test: node }) ||
      t.isBinary(parent, { left: node }) ||
      t.isAssignmentExpression(parent, { left: node })
    ) {
      node = parent;
      i--;
      parent = printStack[i];
    } else {
      return false;
    }
  }

  return false;
}