Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: codemod for conditionallyrender #8046

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions frontend/scripts/codemod.sh
Original file line number Diff line number Diff line change
@@ -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
82 changes: 82 additions & 0 deletions frontend/scripts/jscodeshift-transform-conditionallyrender.js
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type TargetElement =

type RenderFunc = () => JSX.Element;

/**
* @deprecated Use a ternary operator (`condition ? show : elseShow`)
* @see https://docs.getunleash.io/contributing/ADRs/front-end/jsx-conditionals
*/
export const ConditionallyRender = ({
condition,
show,
Expand Down
Loading