export default function transformer()

in packages/docusaurus-migrate/src/transform.ts [115:182]


export default function transformer(file: string): string {
  const root = jscodeshift(file);
  const r = getImportDeclaratorPaths(root);
  r.forEach((node) => {
    if (node?.value?.init?.type === 'CallExpression') {
      processCallExpression(node);
    } else if (node?.value?.init?.type === 'MemberExpression') {
      processMemberExpression(node);
    }
  });
  if (r[r.length - 1]) {
    jscodeshift(r[r.length - 1]!.parent).insertAfter(
      jscodeshift.importDeclaration(
        [jscodeshift.importDefaultSpecifier(jscodeshift.identifier('Layout'))],
        jscodeshift.literal('@theme/Layout'),
      ),
    );
  }

  root
    .find(AssignmentExpression, {
      operator: '=',
      left: {
        type: 'MemberExpression',
        object: {
          name: 'module',
        },
        property: {
          name: 'exports',
        },
      },
      right: {
        type: 'Identifier',
      },
    })
    .filter((p) => p.parentPath.parentPath.name === 'body')
    .forEach((p) => {
      const exportDecl = jscodeshift.exportDeclaration(
        true,
        jscodeshift.arrowFunctionExpression(
          [jscodeshift.identifier('props')],
          jscodeshift.jsxElement(
            jscodeshift.jsxOpeningElement(
              jscodeshift.jsxIdentifier('Layout'),
              [],
            ),
            jscodeshift.jsxClosingElement(jscodeshift.jsxIdentifier('Layout')),
            [
              jscodeshift.jsxElement(
                jscodeshift.jsxOpeningElement(
                  jscodeshift.jsxIdentifier((p.value.right as Identifier).name),
                  [
                    jscodeshift.jsxSpreadAttribute(
                      jscodeshift.identifier('props'),
                    ),
                  ],
                  true,
                ),
              ),
            ],
          ),
        ),
      );
      exportDecl.comments = p.parentPath.value.comments;
      jscodeshift(p.parentPath).replaceWith(exportDecl);
    });
  return root.toSource();
}