@babel/types#JSXEmptyExpression TypeScript Examples

The following examples show how to use @babel/types#JSXEmptyExpression. 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: index.ts    From plasmic with MIT License 6 votes vote down vote up
wrapAsJsxAttrValue = (
  rawValue:
    | Expression
    | JSXEmptyExpression
    | JSXExpressionContainer
    | JSXSpreadChild
    | JSXFragment
    | JSXText
    | undefined
) => {
  if (!rawValue) {
    return undefined;
  }
  if (rawValue.type === "JSXText" || rawValue.type === "JSXSpreadChild") {
    // Either newVersion or editedVersion was wrapped in a fragment.
    // We wrap too.
    mkJsxFragment([rawValue]);
  } else if (
    babel.types.isExpression(rawValue) ||
    babel.types.isJSXEmptyExpression(rawValue)
  ) {
    return babel.types.jsxExpressionContainer(rawValue);
  } else {
    return rawValue;
  }
}
Example #2
Source File: index.ts    From plasmic with MIT License 6 votes vote down vote up
serializePlasmicASTNode = (
  newNode: PlasmicASTNode,
  codeVersions: CodeVersions
):
  | Expression
  | JSXEmptyExpression
  | JSXExpressionContainer
  | JSXSpreadChild
  | JSXFragment
  | JSXText
  | undefined => {
  if (newNode.type === "opaque") {
    return newNode.rawNode;
  }
  return serializeNonOpaquePlasmicASTNode(newNode, codeVersions);
}
Example #3
Source File: index.ts    From plasmic with MIT License 6 votes vote down vote up
renameAndSerializePlasmicASTNode = (
  newNode: PlasmicASTNode,
  codeVersions: CodeVersions
):
  | Expression
  | JSXEmptyExpression
  | JSXExpressionContainer
  | JSXSpreadChild
  | JSXFragment
  | JSXText
  | undefined => {
  if (newNode.type === "opaque") {
    return newNode.rawNode;
  }
  return serializeNonOpaquePlasmicASTNode(newNode, {
    baseVersion: codeVersions.baseVersion.renameJsxTree(
      codeVersions.newVersion
    ),
    editedVersion: codeVersions.editedVersion.renameJsxTree(
      codeVersions.newVersion
    ),
    newVersion: codeVersions.newVersion,
  });
}
Example #4
Source File: plasmic-parser.ts    From plasmic with MIT License 6 votes vote down vote up
parseNode = (
  n:
    | Expression
    | JSXEmptyExpression
    | JSXExpressionContainer
    | JSXSpreadChild
    | JSXFragment,
  parent: Node | undefined,
  forceFragmentAsOneNode: boolean
): PlasmicASTNode => {
  let node: PlasmicASTNode | null = null;
  if (n.type === "JSXExpressionContainer") {
    // Always unwrap the expression container
    node = parseNode(n.expression, n, forceFragmentAsOneNode);
  } else if (n.type === "JSXSpreadChild") {
    node = parseAsOneNode(n.expression, n);
  } else if (n.type === "JSXFragment") {
    node = forceFragmentAsOneNode
      ? parseAsOneNode(n, parent)
      : {
          type: "jsx-fragment",
          children: parseChildren(n),
          rawNode: n,
        };
  } else {
    node = parseAsOneNode(n, parent);
  }
  node.rawNode = n;
  return node;
}
Example #5
Source File: index.ts    From plasmic with MIT License 5 votes vote down vote up
mergeAttributeValue = (
  attrName: string,
  newNode: PlasmicTagOrComponent,
  editedNode: PlasmicTagOrComponent,
  baseNode: PlasmicTagOrComponent,
  codeVersions: CodeVersions
) => {
  const parsedNewAttrValue = findParsedNamedAttrs(newNode, attrName);
  const parsedEditedAttrValue = findParsedNamedAttrs(editedNode, attrName);
  const parsedBaseAttrValue = findParsedNamedAttrs(baseNode, attrName);
  // These two must not be undefined (i.e. not found)
  assert(parsedNewAttrValue !== undefined);
  assert(parsedEditedAttrValue !== undefined);
  let mergedAttrValue:
    | Expression
    | JSXEmptyExpression
    | JSXExpressionContainer
    | JSXSpreadChild
    | JSXFragment
    | JSXText
    | undefined = undefined;
  if (!parsedNewAttrValue) {
    if (parsedEditedAttrValue) {
      mergedAttrValue = serializePlasmicASTNode(
        parsedEditedAttrValue,
        codeVersions
      );
    } else {
      mergedAttrValue = undefined;
    }
  } else {
    if (!parsedEditedAttrValue) {
      mergedAttrValue = serializePlasmicASTNode(
        parsedNewAttrValue,
        codeVersions
      );
    } else {
      // do merge, really!
      const asArray = (node: PlasmicASTNode | null | undefined) =>
        !node ? [] : node.type === "jsx-fragment" ? node.children : [node];
      const mergedNodes = mergeNodes(
        asArray(parsedNewAttrValue),
        asArray(parsedEditedAttrValue),
        asArray(parsedBaseAttrValue),
        codeVersions
      );
      // If edited forced wrapping single node with JSXFragment, so do we
      // if the result is a single element.
      const editedForcedFragment =
        parsedEditedAttrValue.type === "jsx-fragment" &&
        parsedEditedAttrValue.children.length === 1;
      mergedAttrValue =
        mergedNodes.length > 1
          ? mkJsxFragment(mergedNodes)
          : mergedNodes.length === 1
          ? editedForcedFragment
            ? mkJsxFragment(mergedNodes)
            : mergedNodes[0]
          : babel.types.jsxEmptyExpression();
    }
  }
  return wrapAsJsxAttrValue(mergedAttrValue);
}
Example #6
Source File: plasmic-parser.ts    From plasmic with MIT License 5 votes vote down vote up
parseAsOneNode = (
  n: Expression | JSXEmptyExpression | JSXFragment,
  parent: Node | undefined
): PlasmicASTNode => {
  if (n.type === "JSXEmptyExpression") {
    return {
      type: "opaque",
      rawNode: n,
    };
  }

  if (n.type === "StringLiteral") {
    return {
      type: "string-lit",
      value: n.value,
      rawNode: n,
    };
  }
  if (n.type === "CallExpression") {
    const callee = tryExtractPropertyNameOfMemberExpression(
      n.callee,
      helperObject
    );
    const m = callee?.match(/^childStr(.+)$/);
    if (m) {
      return { type: "child-str-call", plasmicId: ensure(m[1]), rawNode: n };
    }
  }

  // Need to handle this case specially since traverse doesn't visit n itself.
  if (n.type === "JSXElement") {
    const jsxElement = tryParseAsPlasmicJsxElement(n, parent);
    if (jsxElement) {
      return {
        type: "tag-or-component",
        jsxElement,
        rawNode: n,
        secondaryNodes: [],
      };
    }
  }
  const jsxElements: PlasmicJsxElement[] = [];
  traverse(n, {
    noScope: true,
    JSXElement: function (path) {
      const jsxElement = tryParseAsPlasmicJsxElement(path.node, path.parent);
      if (jsxElement) {
        jsxElements.push(jsxElement);
        path.skip();
      }
    },
  });
  return jsxElements.length > 0
    ? {
        type: "tag-or-component",
        jsxElement: jsxElements[0],
        rawNode: n,
        secondaryNodes: jsxElements.slice(1).map((elt) => ({
          type: "tag-or-component",
          jsxElement: elt,
          rawNode:
            elt.rawParent && elt.rawParent.type === "LogicalExpression"
              ? elt.rawParent
              : elt.rawNode,
          secondaryNodes: [],
        })),
      }
    : {
        type: "opaque",
        rawNode: n,
      };
}