forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pigment] Handle more scenarios while transforming sx prop (mui#41372)
- Loading branch information
1 parent
b1a0896
commit 91193fa
Showing
16 changed files
with
416 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,82 @@ | ||
import { addNamed } from '@babel/helper-module-imports'; | ||
import { declare } from '@babel/helper-plugin-utils'; | ||
import { sxObjectExtractor } from './sxObjectExtractor'; | ||
import { NodePath } from '@babel/core'; | ||
import * as Types from '@babel/types'; | ||
import { sxPropConverter } from './sxPropConverter'; | ||
|
||
export const babelPlugin = declare((api) => { | ||
api.assertVersion(7); | ||
const { types: t } = api; | ||
return { | ||
name: '@pigmentcss/zero-babel-plugin', | ||
visitor: { | ||
JSXAttribute(path) { | ||
const namePath = path.get('name'); | ||
const openingElement = path.findParent((p) => p.isJSXOpeningElement()); | ||
if ( | ||
!openingElement || | ||
!openingElement.isJSXOpeningElement() || | ||
!namePath.isJSXIdentifier() || | ||
namePath.node.name !== 'sx' | ||
) { | ||
return; | ||
} | ||
const tagName = openingElement.get('name'); | ||
if (!tagName.isJSXIdentifier()) { | ||
return; | ||
} | ||
const valuePath = path.get('value'); | ||
if (!valuePath.isJSXExpressionContainer()) { | ||
return; | ||
} | ||
const expressionPath = valuePath.get('expression'); | ||
if (!expressionPath.isExpression()) { | ||
return; | ||
} | ||
if (!expressionPath.isObjectExpression() && !expressionPath.isArrowFunctionExpression()) { | ||
return; | ||
} | ||
sxObjectExtractor(expressionPath); | ||
const sxIdentifier = addNamed(namePath, 'sx', process.env.PACKAGE_NAME as string); | ||
expressionPath.replaceWith( | ||
t.callExpression(sxIdentifier, [expressionPath.node, t.identifier(tagName.node.name)]), | ||
); | ||
}, | ||
ObjectProperty(path) { | ||
// @TODO - Maybe add support for React.createElement calls as well. | ||
// Right now, it only checks for jsx(),jsxs(),jsxDEV() and jsxsDEV() calls. | ||
const keyPath = path.get('key'); | ||
if (!keyPath.isIdentifier() || keyPath.node.name !== 'sx') { | ||
return; | ||
} | ||
const valuePath = path.get('value'); | ||
if (!valuePath.isObjectExpression() && !valuePath.isArrowFunctionExpression()) { | ||
return; | ||
} | ||
const parentJsxCall = path.findParent((p) => p.isCallExpression()); | ||
if (!parentJsxCall || !parentJsxCall.isCallExpression()) { | ||
return; | ||
} | ||
const callee = parentJsxCall.get('callee'); | ||
if (!callee.isIdentifier() || !callee.node.name.includes('jsx')) { | ||
return; | ||
} | ||
const jsxElement = parentJsxCall.get('arguments')[0]; | ||
sxObjectExtractor(valuePath); | ||
const sxIdentifier = addNamed(keyPath, 'sx', process.env.PACKAGE_NAME as string); | ||
valuePath.replaceWith(t.callExpression(sxIdentifier, [valuePath.node, jsxElement.node])); | ||
}, | ||
}, | ||
function replaceNodePath( | ||
expressionPath: NodePath<Types.Expression>, | ||
namePath: NodePath<Types.JSXIdentifier | Types.Identifier>, | ||
importName: string, | ||
t: typeof Types, | ||
tagName: NodePath<Types.JSXIdentifier | Types.Identifier>, | ||
) { | ||
const sxIdentifier = addNamed(namePath, importName, process.env.PACKAGE_NAME as string); | ||
|
||
const wrapWithSxCall = (expPath: NodePath<Types.Expression>) => { | ||
expPath.replaceWith( | ||
t.callExpression(sxIdentifier, [expPath.node, t.identifier(tagName.node.name)]), | ||
); | ||
}; | ||
}); | ||
|
||
sxPropConverter(expressionPath, wrapWithSxCall); | ||
} | ||
|
||
export const babelPlugin = declare<{ propName?: string; importName?: string }>( | ||
(api, { propName = 'sx', importName = 'sx' }) => { | ||
api.assertVersion(7); | ||
const { types: t } = api; | ||
return { | ||
name: '@pigmentcss/zero-babel-plugin', | ||
visitor: { | ||
JSXAttribute(path) { | ||
const namePath = path.get('name'); | ||
const openingElement = path.findParent((p) => p.isJSXOpeningElement()); | ||
if ( | ||
!openingElement || | ||
!openingElement.isJSXOpeningElement() || | ||
!namePath.isJSXIdentifier() || | ||
namePath.node.name !== propName | ||
) { | ||
return; | ||
} | ||
const tagName = openingElement.get('name'); | ||
if (!tagName.isJSXIdentifier()) { | ||
return; | ||
} | ||
const valuePath = path.get('value'); | ||
if (!valuePath.isJSXExpressionContainer()) { | ||
return; | ||
} | ||
const expressionPath = valuePath.get('expression'); | ||
if (!expressionPath.isExpression()) { | ||
return; | ||
} | ||
replaceNodePath(expressionPath, namePath, importName, t, tagName); | ||
}, | ||
ObjectProperty(path) { | ||
// @TODO - Maybe add support for React.createElement calls as well. | ||
// Right now, it only checks for jsx(),jsxs(),jsxDEV() and jsxsDEV() calls. | ||
const keyPath = path.get('key'); | ||
if (!keyPath.isIdentifier() || keyPath.node.name !== propName) { | ||
return; | ||
} | ||
const valuePath = path.get('value'); | ||
if (!valuePath.isObjectExpression() && !valuePath.isArrowFunctionExpression()) { | ||
return; | ||
} | ||
const parentJsxCall = path.findParent((p) => p.isCallExpression()); | ||
if (!parentJsxCall || !parentJsxCall.isCallExpression()) { | ||
return; | ||
} | ||
const callee = parentJsxCall.get('callee'); | ||
if (!callee.isIdentifier() || !callee.node.name.includes('jsx')) { | ||
return; | ||
} | ||
const jsxElement = parentJsxCall.get('arguments')[0] as NodePath<Types.Identifier>; | ||
replaceNodePath(valuePath, keyPath, importName, t, jsxElement); | ||
}, | ||
}, | ||
}; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { NodePath } from '@babel/core'; | ||
import { ArrowFunctionExpression, Expression, ObjectExpression } from '@babel/types'; | ||
import { sxObjectExtractor } from './sxObjectExtractor'; | ||
|
||
function isAllowedExpression( | ||
node: NodePath<Expression>, | ||
): node is NodePath<ObjectExpression> | NodePath<ArrowFunctionExpression> { | ||
return node.isObjectExpression() || node.isArrowFunctionExpression(); | ||
} | ||
|
||
export function sxPropConverter( | ||
node: NodePath<Expression>, | ||
wrapWithSxCall: (expPath: NodePath<Expression>) => void, | ||
) { | ||
if (node.isConditionalExpression()) { | ||
const consequent = node.get('consequent'); | ||
const alternate = node.get('alternate'); | ||
|
||
if (isAllowedExpression(consequent)) { | ||
sxObjectExtractor(consequent); | ||
wrapWithSxCall(consequent); | ||
} | ||
if (isAllowedExpression(alternate)) { | ||
sxObjectExtractor(alternate); | ||
wrapWithSxCall(alternate); | ||
} | ||
} else if (node.isLogicalExpression()) { | ||
const right = node.get('right'); | ||
if (isAllowedExpression(right)) { | ||
sxObjectExtractor(right); | ||
wrapWithSxCall(right); | ||
} | ||
} else if (isAllowedExpression(node)) { | ||
sxObjectExtractor(node); | ||
wrapWithSxCall(node); | ||
} else if (node.isIdentifier()) { | ||
const rootScope = node.scope.getProgramParent(); | ||
const binding = node.scope.getBinding(node.node.name); | ||
// Simplest case, ie, const styles = {static object} | ||
// and is used as <Component sx={styles} /> | ||
if (binding?.scope === rootScope) { | ||
wrapWithSxCall(node); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.