Skip to content

Commit

Permalink
support nested object
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Apr 18, 2024
1 parent aa43e26 commit f5480fd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 32 deletions.
74 changes: 43 additions & 31 deletions packages/mui-codemod/src/v6.0.0/styled/styled-v6.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ export default function styledV6(file, api, options) {
const root = j(file.source);
const printOptions = options.printOptions;

function createBuildStyle(key, upperBuildStyle) {
return function buildStyle(styleExpression) {
if (key) {
if (key.type === 'Identifier' || key.type === 'StringLiteral') {
return upperBuildStyle(j.objectExpression([j.objectProperty(key, styleExpression)]));
}
if (key.type === 'TemplateLiteral') {
return upperBuildStyle(
j.objectExpression([
{
...j.objectProperty(key, styleExpression),
computed: true,
},
]),
);
}
}
return upperBuildStyle ? upperBuildStyle(styleExpression) : styleExpression;
};
}

/**
*
* @param {import('ast-types').namedTypes.MemberExpression | import('ast-types').namedTypes.Identifier} node
Expand Down Expand Up @@ -309,7 +330,7 @@ export default function styledV6(file, api, options) {
}
}

recurseObjectExpression({ node: objectExpression });
recurseObjectExpression({ node: objectExpression, buildStyle: createBuildStyle() });

if (variants.length) {
objectExpression.properties.push(
Expand Down Expand Up @@ -343,13 +364,19 @@ export default function styledV6(file, api, options) {
node: prop.value,
parentNode: data.node,
key: prop.key,
buildStyle: createBuildStyle(prop.key, data.buildStyle),
replaceValue: (newValue) => {
prop.value = newValue;
},
modeStyles,
});
} else {
recurseObjectExpression({ ...data, node: prop, parentNode: data.node });
recurseObjectExpression({
...data,
node: prop,
parentNode: data.node,
buildStyle: createBuildStyle(prop.key, data.buildStyle),
});
}
});
appendPaletteModeStyles(data.node, modeStyles);
Expand Down Expand Up @@ -378,6 +405,7 @@ export default function styledV6(file, api, options) {
parentNode: variant.style,
props: variant.props,
key: prop.key,
buildStyle: createBuildStyle(prop.key, data.buildStyle),
replaceValue: (newValue) => {
prop.value = newValue;
},
Expand All @@ -389,23 +417,21 @@ export default function styledV6(file, api, options) {
node: prop,
parentNode: variant.style,
props: variant.props,
buildStyle: createBuildStyle(prop.key, data.buildStyle),
});
}
});
appendPaletteModeStyles(variant.style, modeStyles);
if (data.key) {
variant.style = j.objectExpression([
{
...j.objectProperty(data.key, variant.style),
computed: data.key.type === 'TemplateLiteral',
},
]);
}
variant.style = data.buildStyle(variant.style);
variants[lastLength - 1] = buildObjectAST(variant);
removeProperty(data.parentNode, data.node);
}
if (data.node.argument.type === 'ConditionalExpression') {
recurseObjectExpression({ ...data, node: data.node.argument, parentNode: data.node });
recurseObjectExpression({
...data,
node: data.node.argument,
parentNode: data.node,
});
removeProperty(data.parentNode, data.node);
}
}
Expand All @@ -427,17 +453,10 @@ export default function styledV6(file, api, options) {
if (data.props) {
props = mergeProps(data.props, props);
}
const styleVal = data.node.consequent;
let newStyle = styleVal;
if (
data.key &&
(data.key.type === 'Identifier' || data.key.type === 'StringLiteral')
) {
newStyle = j.objectExpression([j.objectProperty(data.key, styleVal)]);
}
const styleVal = data.buildStyle(data.node.consequent);
const variant = {
props,
style: newStyle,
style: styleVal,
};
variants.push(buildObjectAST(variant));

Expand All @@ -446,17 +465,10 @@ export default function styledV6(file, api, options) {
if (data.props) {
props2 = mergeProps(data.props, props2);
}
const styleVal2 = data.node.alternate;
let newStyle2 = styleVal2;
if (
data.key &&
(data.key.type === 'Identifier' || data.key.type === 'StringLiteral')
) {
newStyle2 = j.objectExpression([j.objectProperty(data.key, styleVal2)]);
}
const styleVal2 = data.buildStyle(data.node.alternate);
const variant2 = {
props: props2,
style: newStyle2,
style: styleVal2,
};
variants.push(buildObjectAST(variant2));
if (data.parentNode?.type === 'ObjectExpression') {
Expand Down Expand Up @@ -494,7 +506,7 @@ export default function styledV6(file, api, options) {
...data,
node: expression,
parentNode: data.parentNode,
key: data.key,
buildStyle: createBuildStyle(data.key, data.buildStyle),
replaceValue: (newValue) => {
data.node.expressions[index] = newValue;
},
Expand Down Expand Up @@ -533,7 +545,7 @@ export default function styledV6(file, api, options) {
j.stringLiteral(prop.key.name),
),
]),
style: j.objectExpression([j.objectProperty(data.key, prop.value)]),
style: data.buildStyle(prop.value),
}),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,14 @@ const ImageListItemRoot = styled('li')(({ ownerState }) => ({
height: 'auto',
flexGrow: 1,
}),
'&:hover': {
'&[data-shape="circular"]': {
borderRadius: '50%',
...(ownerState.variant === 'unique' && {
height: 'auto',
flexGrow: 1,
}),
},
},
},
}));
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ const ImageListItemRoot = styled('li')(({
objectFit: 'cover',
width: '100%',
height: '100%',
display: 'block'
display: 'block',
'&:hover': {
'&[data-shape="circular"]': {
borderRadius: '50%'
},
}
},
variants: [{
props: {
Expand All @@ -137,5 +142,19 @@ const ImageListItemRoot = styled('li')(({
flexGrow: 1,
}
}
}, {
props: {
variant: 'unique'
},
style: {
[`& .${imageListItemClasses.img}`]: {
'&:hover': {
'&[data-shape="circular"]': {
height: 'auto',
flexGrow: 1,
}
}
}
}
}]
}));

0 comments on commit f5480fd

Please sign in to comment.