diff --git a/packages/styled-components/transform/src/visitors/transpile_css_prop/transpile.rs b/packages/styled-components/transform/src/visitors/transpile_css_prop/transpile.rs index 181ce1b28..b89afb220 100644 --- a/packages/styled-components/transform/src/visitors/transpile_css_prop/transpile.rs +++ b/packages/styled-components/transform/src/visitors/transpile_css_prop/transpile.rs @@ -649,14 +649,16 @@ where if let Some(root) = trace_root_value(expr) { match root { Expr::Lit(_) => true, - Expr::Ident(id) if is_top_level_ident(id) => { - if let Expr::Call(CallExpr { args, .. }) = expr { - args.iter() - .all(|arg| -> bool { is_direct_access(&arg.expr, is_top_level_ident) }) - } else { - true - } - } + Expr::Ident(id) if is_top_level_ident(id) => match expr { + Expr::Call(CallExpr { args, .. }) => args + .iter() + .all(|arg| -> bool { is_direct_access(&arg.expr, is_top_level_ident) }), + Expr::Member(MemberExpr { + prop: MemberProp::Computed(ComputedPropName { expr, .. }), + .. + }) => is_direct_access(expr, is_top_level_ident), + _ => true, + }, _ => false, } } else { diff --git a/packages/styled-components/transform/tests/fixtures/transpile-css-prop/code.js b/packages/styled-components/transform/tests/fixtures/transpile-css-prop/code.js index c8d75e45d..a16014f08 100644 --- a/packages/styled-components/transform/tests/fixtures/transpile-css-prop/code.js +++ b/packages/styled-components/transform/tests/fixtures/transpile-css-prop/code.js @@ -276,4 +276,84 @@ function Home() { const myCss = (x) => css` margin: ${x}px; -`; \ No newline at end of file +`; + +const colorMap = { + orange: '#E3750E', + blue: '#38699F', + green: '#3F9348', +}; + +const colorMap2 = { + red: 'orange', + cyan: 'blue', + lime: 'green', +} + +const LocalMemberInterpolation = (p) => { + const color = "red"; + + return ( +
+ H +
+ ); +}; + +const LocalMemberInterpolation2 = (p) => { + const color = "red"; + + return ( ++ H +
+ ); +}; + +const LocalMemberInterpolation3 = (p) => { + const color = "red"; + + return ( ++ H +
+ ); +}; + +const LocalMemberInterpolation4 = () => { + return ( ++ H +
+ ); +}; + +const LocalMemberCssHelperProp = (p) => { + const color = "red"; + + return ( ++ H +
+ ); +}; diff --git a/packages/styled-components/transform/tests/fixtures/transpile-css-prop/output.js b/packages/styled-components/transform/tests/fixtures/transpile-css-prop/output.js index 38abfb0fe..328507be1 100644 --- a/packages/styled-components/transform/tests/fixtures/transpile-css-prop/output.js +++ b/packages/styled-components/transform/tests/fixtures/transpile-css-prop/output.js @@ -163,6 +163,55 @@ function Home() { const myCss = (x)=>css` margin: ${x}px; `; +const colorMap = { + orange: '#E3750E', + blue: '#38699F', + green: '#3F9348' +}; +const colorMap2 = { + red: 'orange', + cyan: 'blue', + lime: 'green' +}; +const LocalMemberInterpolation = (p)=>{ + const color = "red"; + return <_StyledP17 $_css18={colorMap[color]}> + + H + + ; +}; +const LocalMemberInterpolation2 = (p)=>{ + const color = "red"; + return <_StyledP18 $_css19={colorMap[colorMap2[color]]}> + + H + + ; +}; +const LocalMemberInterpolation3 = (p)=>{ + const color = "red"; + return <_StyledP19 $_css20={colorMap[id(color)]}> + + H + + ; +}; +const LocalMemberInterpolation4 = ()=>{ + return <_StyledP20> + + H + + ; +}; +const LocalMemberCssHelperProp = (p)=>{ + const color = "red"; + return <_StyledP21 $_css21={colorMap[color]}> + + H + + ; +}; var _StyledP = _styled("p")`flex: 1;`; var _StyledP2 = _styled("p")` flex: 1; @@ -229,3 +278,18 @@ var _StyledDiv2 = _styled("div")` /* indirect */ ${(p)=>p.$_css17} `; +var _StyledP17 = _styled("p")` + color: ${(p)=>p.$_css18}; + `; +var _StyledP18 = _styled("p")` + color: ${(p)=>p.$_css19}; + `; +var _StyledP19 = _styled("p")` + color: ${(p)=>p.$_css20}; + `; +var _StyledP20 = _styled("p")` + color: ${colorMap["red"]}; + `; +var _StyledP21 = _styled("p")` + color: ${(p)=>p.$_css21}; + `;