|
| 1 | +import { addNamed } from '@babel/helper-module-imports'; |
| 2 | +import { declare } from '@babel/helper-plugin-utils'; |
| 3 | +import { NodePath } from '@babel/core'; |
| 4 | +import * as Types from '@babel/types'; |
| 5 | + |
| 6 | +import { sxPropConverter } from './sxPropConverter'; |
| 7 | + |
| 8 | +function convertJsxMemberExpressionToMemberExpression( |
| 9 | + t: typeof Types, |
| 10 | + nodePath: NodePath<Types.JSXMemberExpression>, |
| 11 | +): Types.MemberExpression { |
| 12 | + const object = nodePath.get('object'); |
| 13 | + const property = nodePath.get('property'); |
| 14 | + |
| 15 | + if (object.isJSXMemberExpression()) { |
| 16 | + return t.memberExpression( |
| 17 | + convertJsxMemberExpressionToMemberExpression(t, object), |
| 18 | + t.identifier(property.node.name), |
| 19 | + ); |
| 20 | + } |
| 21 | + return t.memberExpression( |
| 22 | + t.identifier((object.node as Types.JSXIdentifier).name), |
| 23 | + t.identifier(property.node.name), |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +function wrapWithSxComponent( |
| 28 | + t: typeof Types, |
| 29 | + tagNamePath: NodePath<Types.JSXIdentifier | Types.JSXMemberExpression | Types.JSXNamespacedName>, |
| 30 | + sxComponentName: string, |
| 31 | +) { |
| 32 | + const sxComponent = addNamed( |
| 33 | + tagNamePath, |
| 34 | + sxComponentName, |
| 35 | + `${process.env.PACKAGE_NAME}/private-runtime`, |
| 36 | + ); |
| 37 | + const jsxElement = tagNamePath.findParent((p) => p.isJSXElement()); |
| 38 | + if (!jsxElement?.isJSXElement()) { |
| 39 | + return; |
| 40 | + } |
| 41 | + const component = t.jsxIdentifier(sxComponent.name); |
| 42 | + |
| 43 | + const newChildren = (jsxElement.get('children') ?? []).map((child) => child.node); |
| 44 | + let sxComponentValue: Types.Identifier | Types.MemberExpression | null = null; |
| 45 | + |
| 46 | + if (tagNamePath.isJSXIdentifier()) { |
| 47 | + sxComponentValue = t.identifier(tagNamePath.node.name); |
| 48 | + } else if (tagNamePath.isJSXMemberExpression()) { |
| 49 | + sxComponentValue = convertJsxMemberExpressionToMemberExpression(t, tagNamePath); |
| 50 | + } |
| 51 | + |
| 52 | + const newElement = t.jsxElement( |
| 53 | + t.jsxOpeningElement( |
| 54 | + component, |
| 55 | + [ |
| 56 | + t.jsxAttribute( |
| 57 | + t.jsxIdentifier('sxComponent'), |
| 58 | + t.jsxExpressionContainer(sxComponentValue ?? t.nullLiteral()), |
| 59 | + ), |
| 60 | + ...jsxElement |
| 61 | + .get('openingElement') |
| 62 | + .get('attributes') |
| 63 | + .map((attr) => attr.node), |
| 64 | + ], |
| 65 | + !newChildren.length, |
| 66 | + ), |
| 67 | + newChildren.length ? t.jsxClosingElement(component) : null, |
| 68 | + newChildren, |
| 69 | + !newChildren.length, |
| 70 | + ); |
| 71 | + jsxElement.replaceWith(newElement); |
| 72 | +} |
| 73 | + |
| 74 | +function replaceNodePath( |
| 75 | + expressionPath: NodePath<Types.Expression>, |
| 76 | + namePath: NodePath<Types.JSXIdentifier | Types.Identifier>, |
| 77 | + importName: string, |
| 78 | + t: typeof Types, |
| 79 | + tagNamePath: NodePath< |
| 80 | + Types.JSXIdentifier | Types.Identifier | Types.JSXMemberExpression | Types.MemberExpression |
| 81 | + >, |
| 82 | + sxComponentName: string, |
| 83 | +) { |
| 84 | + const sxIdentifier = addNamed(namePath, importName, process.env.PACKAGE_NAME as string); |
| 85 | + let wasSxTransformed = false; |
| 86 | + |
| 87 | + const wrapWithSxCall = (expPath: NodePath<Types.Expression>) => { |
| 88 | + let tagNameArg: Types.Identifier | Types.MemberExpression | null = null; |
| 89 | + if (tagNamePath.isJSXIdentifier()) { |
| 90 | + tagNameArg = t.identifier(tagNamePath.node.name); |
| 91 | + } else if (tagNamePath.isJSXMemberExpression()) { |
| 92 | + tagNameArg = convertJsxMemberExpressionToMemberExpression(t, tagNamePath); |
| 93 | + } else { |
| 94 | + tagNameArg = tagNamePath.node as Types.Identifier | Types.MemberExpression; |
| 95 | + } |
| 96 | + expPath.replaceWith(t.callExpression(sxIdentifier, [expPath.node, tagNameArg])); |
| 97 | + wasSxTransformed = true; |
| 98 | + }; |
| 99 | + |
| 100 | + sxPropConverter(expressionPath, wrapWithSxCall); |
| 101 | + |
| 102 | + if (wasSxTransformed) { |
| 103 | + if (tagNamePath.isJSXIdentifier() || tagNamePath.isJSXMemberExpression()) { |
| 104 | + wrapWithSxComponent(t, tagNamePath, sxComponentName); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export const babelPlugin = declare<{ |
| 110 | + propName?: string; |
| 111 | + importName?: string; |
| 112 | + sxComponentName?: string; |
| 113 | +}>((api, { propName = 'sx', importName = 'sx', sxComponentName = 'ForwardSx' }) => { |
| 114 | + api.assertVersion(7); |
| 115 | + const { types: t } = api; |
| 116 | + return { |
| 117 | + name: '@pigmentcss/sx-plugin', |
| 118 | + visitor: { |
| 119 | + JSXAttribute(path) { |
| 120 | + const namePath = path.get('name'); |
| 121 | + const openingElement = path.findParent((p) => p.isJSXOpeningElement()); |
| 122 | + if ( |
| 123 | + !openingElement || |
| 124 | + !openingElement.isJSXOpeningElement() || |
| 125 | + !namePath.isJSXIdentifier() || |
| 126 | + namePath.node.name !== propName |
| 127 | + ) { |
| 128 | + return; |
| 129 | + } |
| 130 | + const tagName = openingElement.get('name'); |
| 131 | + const valuePath = path.get('value'); |
| 132 | + if (!valuePath.isJSXExpressionContainer()) { |
| 133 | + return; |
| 134 | + } |
| 135 | + const expressionPath = valuePath.get('expression'); |
| 136 | + if (!expressionPath.isExpression()) { |
| 137 | + return; |
| 138 | + } |
| 139 | + // @ts-ignore |
| 140 | + replaceNodePath(expressionPath, namePath, importName, t, tagName, sxComponentName); |
| 141 | + }, |
| 142 | + ObjectProperty(path) { |
| 143 | + // @TODO - Maybe add support for React.createElement calls as well. |
| 144 | + // Right now, it only checks for jsx(),jsxs(),jsxDEV() and jsxsDEV() calls. |
| 145 | + const keyPath = path.get('key'); |
| 146 | + if (!keyPath.isIdentifier() || keyPath.node.name !== propName) { |
| 147 | + return; |
| 148 | + } |
| 149 | + const valuePath = path.get('value'); |
| 150 | + if (!valuePath.isObjectExpression() && !valuePath.isArrowFunctionExpression()) { |
| 151 | + return; |
| 152 | + } |
| 153 | + const parentJsxCall = path.findParent((p) => p.isCallExpression()); |
| 154 | + if (!parentJsxCall || !parentJsxCall.isCallExpression()) { |
| 155 | + return; |
| 156 | + } |
| 157 | + const callee = parentJsxCall.get('callee'); |
| 158 | + if (!callee.isIdentifier() || !callee.node.name.includes('jsx')) { |
| 159 | + return; |
| 160 | + } |
| 161 | + const jsxElement = parentJsxCall.get('arguments')[0] as NodePath<Types.Identifier>; |
| 162 | + replaceNodePath(valuePath, keyPath, importName, t, jsxElement, sxComponentName); |
| 163 | + }, |
| 164 | + }, |
| 165 | + }; |
| 166 | +}); |
0 commit comments