mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-12 13:48:35 +02:00
refactor: codemod for conditionallyrender
This commit is contained in:
parent
0b2479517f
commit
33f692c272
8
frontend/scripts/codemod.sh
Executable file
8
frontend/scripts/codemod.sh
Executable file
@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "Running codemod on: '$1'"
|
||||||
|
npx jscodeshift --extensions=tsx,jsx -t="scripts/jscodeshift-transform-conditionallyrender.js" $1
|
||||||
|
npx jscodeshift --extensions=tsx,jsx -t="scripts/jscodeshift-transform-conditionallyrender.js" $1
|
||||||
|
|
||||||
|
./node_modules/.bin/biome lint src --write --unsafe
|
||||||
|
./node_modules/.bin/biome format src --write
|
@ -0,0 +1,82 @@
|
|||||||
|
// @ts-check
|
||||||
|
|
||||||
|
export const parser = 'tsx';
|
||||||
|
|
||||||
|
const getAttr = (j, node, attribute) => {
|
||||||
|
const attributes = node.value?.openingElement?.attributes || [];
|
||||||
|
const attr = attributes.find(
|
||||||
|
(attr) => j.JSXAttribute.check(attr) && attr.name.name === attribute,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!attr) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = attr.value;
|
||||||
|
|
||||||
|
if (value.type === 'StringLiteral') {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return value?.expression || null;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @type {import('jscodeshift').Transform} */
|
||||||
|
const transform = (file, api, options) => {
|
||||||
|
const j = api.jscodeshift;
|
||||||
|
const root = j(file.source);
|
||||||
|
|
||||||
|
root.findJSXElements('ConditionallyRender')
|
||||||
|
.forEach((path) => {
|
||||||
|
const attributes = path.node.openingElement.attributes;
|
||||||
|
|
||||||
|
attributes?.forEach((attr) => {
|
||||||
|
if (
|
||||||
|
j.JSXAttribute.check(attr) &&
|
||||||
|
(attr.name.name === 'show' || attr.name.name === 'elseShow')
|
||||||
|
) {
|
||||||
|
const attrValue = attr.value;
|
||||||
|
|
||||||
|
// Check if the attribute value is an arrow function returning JSX
|
||||||
|
if (
|
||||||
|
j.JSXExpressionContainer.check(attrValue) &&
|
||||||
|
j.ArrowFunctionExpression.check(attrValue.expression)
|
||||||
|
) {
|
||||||
|
const arrowFunctionBody = attrValue.expression.body;
|
||||||
|
|
||||||
|
if (
|
||||||
|
j.JSXElement.check(arrowFunctionBody) ||
|
||||||
|
j.JSXFragment.check(arrowFunctionBody)
|
||||||
|
) {
|
||||||
|
// Replace the arrow function with the direct JSX element or fragment
|
||||||
|
attr.value =
|
||||||
|
j.jsxExpressionContainer(arrowFunctionBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.replaceWith((node) => {
|
||||||
|
const isInJSX = ['JSXElement', 'JSXFragment'].includes(
|
||||||
|
node.parent.value.type,
|
||||||
|
);
|
||||||
|
|
||||||
|
const condition = getAttr(j, node, 'condition');
|
||||||
|
const show = getAttr(j, node, 'show');
|
||||||
|
const elseShow = getAttr(j, node, 'elseShow');
|
||||||
|
const alternate = elseShow === null ? j.nullLiteral() : elseShow;
|
||||||
|
|
||||||
|
return isInJSX
|
||||||
|
? j.jsxExpressionContainer({
|
||||||
|
type: 'ConditionalExpression',
|
||||||
|
test: condition,
|
||||||
|
consequent: show,
|
||||||
|
alternate,
|
||||||
|
})
|
||||||
|
: j.conditionalExpression(condition, show, alternate);
|
||||||
|
});
|
||||||
|
|
||||||
|
return root.toSource();
|
||||||
|
};
|
||||||
|
|
||||||
|
export default transform;
|
Loading…
Reference in New Issue
Block a user