@babel/types#jsxText TypeScript Examples

The following examples show how to use @babel/types#jsxText. 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
serializeNonOpaquePlasmicASTNode = (
  newNode: PlasmicASTNode,
  codeVersions: CodeVersions
): Expression | JSXExpressionContainer | JSXText | JSXFragment | undefined => {
  assert(newNode.type !== "opaque");
  if (newNode.type === "child-str-call") {
    // Just output the new version
    return newNode.rawNode;
  } else if (newNode.type === "string-lit") {
    // Just output the new version
    return newNode.rawNode;
  } else if (newNode.type === "text") {
    // Just output the new version
    return newNode.rawNode;
  } else if (newNode.type === "jsx-fragment") {
    return babel.types.jsxFragment(
      babel.types.jsxOpeningFragment(),
      babel.types.jsxClosingFragment(),
      withoutNils(
        newNode.children.map((child) => {
          const newRawNode = serializePlasmicASTNode(child, codeVersions);
          if (!newRawNode) {
            return undefined;
          }
          if (
            babel.types.isExpression(newRawNode) ||
            babel.types.isJSXEmptyExpression(newRawNode)
          ) {
            return babel.types.jsxExpressionContainer(newRawNode);
          }
          return newRawNode;
        })
      )
    );
  } else {
    assert(newNode.type === "tag-or-component");
    return serializeTagOrComponent(newNode, codeVersions);
  }
}
Example #3
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 #4
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 #5
Source File: jsxConverter.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private parseJsx(node: Expression): JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment {
    if (isStringLiteral(node)) {
      return jsxText(node.value);
    }
    if (isCallExpression(node)) {
      const args = node.arguments;

      let name: JSXIdentifier | JSXMemberExpression | undefined;
      if (isIdentifier(args[0]) || isStringLiteral(args[0])) {
        name = jsxIdentifier(isIdentifier(args[0]) ? args[0].name : args[0].value);
      } else if (isMemberExpression(args[0]) && isIdentifier(args[0].object) && isIdentifier(args[0].property)) {
        name = jsxMemberExpression(jsxIdentifier(args[0].object.name), jsxIdentifier(args[0].property.name));
      } else {
        this.debugLog(`fail to parse component ${args[0].type} inside callExpression`);
        return jsxExpressionContainer(node);
      }

      let props: JSXAttribute[] = [];
      if (isObjectExpression(args[1])) {
        props = args[1].properties.map((prop) => {
          if (!isObjectProperty(prop) || !isIdentifier(prop.key)) return null;
          if (isStringLiteral(prop.value)) {
            return jsxAttribute(jsxIdentifier(prop.key.name), prop.value);
          }
          if (isBooleanLiteral(prop.value) && prop.value.value) {
            return jsxAttribute(jsxIdentifier(prop.key.name), null);
          }
          if (isExpression(prop.value)) {
            return jsxAttribute(jsxIdentifier(prop.key.name), jsxExpressionContainer(prop.value));
          }
          return null;
        }).filter((e): e is JSXAttribute => e != null);
      }

      const children = args.slice(2).map((e) => (isExpression(e) ? this.parseJsx(e) : null)).filter((e): e is JSXElement => e != null);

      if (children.length) {
        return jsxElement(jsxOpeningElement(name, props), jsxClosingElement(name), children);
      }

      return jsxElement(jsxOpeningElement(name, props, true), null, []);
    }

    this.debugLog(`fail to parse component ${node.type}`);
    return jsxExpressionContainer(node);
  }
Example #6
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 #7
Source File: jsxConverter.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private parseJsx(node: Expression): JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment {
    if (isStringLiteral(node)) {
      return jsxText(node.value);
    }
    if (isCallExpression(node)) {
      const args = node.arguments;

      let name: JSXIdentifier | JSXMemberExpression | undefined;
      if (isIdentifier(args[0]) || isStringLiteral(args[0])) {
        name = jsxIdentifier(isIdentifier(args[0]) ? args[0].name : args[0].value);
      } else if (isMemberExpression(args[0]) && isIdentifier(args[0].object) && isIdentifier(args[0].property)) {
        name = jsxMemberExpression(jsxIdentifier(args[0].object.name), jsxIdentifier(args[0].property.name));
      } else {
        this.debugLog(`fail to parse component ${args[0].type} inside callExpression`);
        return jsxExpressionContainer(node);
      }

      let props: JSXAttribute[] = [];
      if (isObjectExpression(args[1])) {
        props = args[1].properties.map((prop) => {
          if (!isObjectProperty(prop) || !isIdentifier(prop.key)) return null;
          if (isStringLiteral(prop.value)) {
            return jsxAttribute(jsxIdentifier(prop.key.name), prop.value);
          }
          if (isBooleanLiteral(prop.value) && prop.value.value) {
            return jsxAttribute(jsxIdentifier(prop.key.name), null);
          }
          if (isExpression(prop.value)) {
            return jsxAttribute(jsxIdentifier(prop.key.name), jsxExpressionContainer(prop.value));
          }
          return null;
        }).filter((e): e is JSXAttribute => e != null);
      }

      const children = args.slice(2).map((e) => (isExpression(e) ? this.parseJsx(e) : null)).filter((e): e is JSXElement => e != null);

      if (children.length) {
        return jsxElement(jsxOpeningElement(name, props), jsxClosingElement(name), children);
      }

      return jsxElement(jsxOpeningElement(name, props, true), null, []);
    }

    this.debugLog(`fail to parse component ${node.type}`);
    return jsxExpressionContainer(node);
  }
Example #8
Source File: jsxConverter.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private parseJsx(node: Expression): JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment {
    if (isStringLiteral(node)) {
      return jsxText(node.value);
    }
    if (isCallExpression(node)) {
      const args = node.arguments;

      let name: JSXIdentifier | JSXMemberExpression | undefined;
      if (isIdentifier(args[0]) || isStringLiteral(args[0])) {
        name = jsxIdentifier(isIdentifier(args[0]) ? args[0].name : args[0].value);
      } else if (isMemberExpression(args[0]) && isIdentifier(args[0].object) && isIdentifier(args[0].property)) {
        name = jsxMemberExpression(jsxIdentifier(args[0].object.name), jsxIdentifier(args[0].property.name));
      } else {
        this.debugLog(`fail to parse component ${args[0].type} inside callExpression`);
        return jsxExpressionContainer(node);
      }

      let props: JSXAttribute[] = [];
      if (isObjectExpression(args[1])) {
        props = args[1].properties.map((prop) => {
          if (!isObjectProperty(prop) || !isIdentifier(prop.key)) return null;
          if (isStringLiteral(prop.value)) {
            return jsxAttribute(jsxIdentifier(prop.key.name), prop.value);
          }
          if (isBooleanLiteral(prop.value) && prop.value.value) {
            return jsxAttribute(jsxIdentifier(prop.key.name), null);
          }
          if (isExpression(prop.value)) {
            return jsxAttribute(jsxIdentifier(prop.key.name), jsxExpressionContainer(prop.value));
          }
          return null;
        }).filter((e): e is JSXAttribute => e != null);
      }

      const children = args.slice(2).map((e) => (isExpression(e) ? this.parseJsx(e) : null)).filter((e): e is JSXElement => e != null);

      if (children.length) {
        return jsxElement(jsxOpeningElement(name, props), jsxClosingElement(name), children);
      }

      return jsxElement(jsxOpeningElement(name, props, true), null, []);
    }

    this.debugLog(`fail to parse component ${node.type}`);
    return jsxExpressionContainer(node);
  }
Example #9
Source File: jsxConverter.ts    From react-native-decompiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private parseJsx(node: Expression): JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment {
    if (isStringLiteral(node)) {
      return jsxText(node.value);
    }
    if (isCallExpression(node)) {
      const args = node.arguments;

      let name: JSXIdentifier | JSXMemberExpression | undefined;
      if (isIdentifier(args[0]) || isStringLiteral(args[0])) {
        name = jsxIdentifier(isIdentifier(args[0]) ? args[0].name : args[0].value);
      } else if (isMemberExpression(args[0]) && isIdentifier(args[0].object) && isIdentifier(args[0].property)) {
        name = jsxMemberExpression(jsxIdentifier(args[0].object.name), jsxIdentifier(args[0].property.name));
      } else {
        this.debugLog(`fail to parse component ${args[0].type} inside callExpression`);
        return jsxExpressionContainer(node);
      }

      let props: JSXAttribute[] = [];
      if (isObjectExpression(args[1])) {
        props = args[1].properties.map((prop) => {
          if (!isObjectProperty(prop) || !isIdentifier(prop.key)) return null;
          if (isStringLiteral(prop.value)) {
            return jsxAttribute(jsxIdentifier(prop.key.name), prop.value);
          }
          if (isBooleanLiteral(prop.value) && prop.value.value) {
            return jsxAttribute(jsxIdentifier(prop.key.name), null);
          }
          if (isExpression(prop.value)) {
            return jsxAttribute(jsxIdentifier(prop.key.name), jsxExpressionContainer(prop.value));
          }
          return null;
        }).filter((e): e is JSXAttribute => e != null);
      }

      const children = args.slice(2).map((e) => (isExpression(e) ? this.parseJsx(e) : null)).filter((e): e is JSXElement => e != null);

      if (children.length) {
        return jsxElement(jsxOpeningElement(name, props), jsxClosingElement(name), children);
      }

      return jsxElement(jsxOpeningElement(name, props, true), null, []);
    }

    this.debugLog(`fail to parse component ${node.type}`);
    return jsxExpressionContainer(node);
  }