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

fix(styled-components): handle topLevelIdent[localIdent] in template literal #233

Merged

Conversation

ciffelia
Copy link
Contributor

@ciffelia ciffelia commented Nov 21, 2023

This fixes local identifiers are not handled properly in styled-components tagged literal.

Input code

const id = (x) => x

const colorMap = {
  orange: '#E3750E',
  blue: '#38699F',
  green: '#3F9348',
}

const Colored = (color, text) => {
  return (
    <div
      css={css`
        /* ${color} */ 
        color: ${colorMap[color]};
        background-color: ${id(color)};
      `}
    >
      {text}
    </div>
  )
}

Previous output

Local variable color is referenced outside the function.

import _styled from "styled-components";
const id = (x)=>x;
const colorMap = {
    orange: '#E3750E',
    blue: '#38699F',
    green: '#3F9348'
};
const Colored = (color, text)=>{
    return <_StyledDiv $_css={color} $_css2={id(color)}>

      {text}

    </_StyledDiv>;
};
var _StyledDiv = _styled("div")`
        /* ${(p)=>p.$_css} */ 
        color: ${colorMap[color]};
        background-color: ${(p)=>p.$_css2};
      `;

Fixed output

import _styled from "styled-components";
const id = (x)=>x;
const colorMap = {
    orange: '#E3750E',
    blue: '#38699F',
    green: '#3F9348'
};
const Colored = (color, text)=>{
    return <_StyledDiv $_css={color} $_css2={colorMap[color]} $_css3={id(color)}>

      {text}

    </_StyledDiv>;
};
var _StyledDiv = _styled("div")`
        /* ${(p)=>p.$_css} */ 
        color: ${(p)=>p.$_css2};
        background-color: ${(p)=>p.$_css3};
      `;

fixes #194
related to #100

@CLAassistant
Copy link

CLAassistant commented Nov 21, 2023

CLA assistant check
All committers have signed the CLA.

@kdy1 kdy1 merged commit fb0d57f into swc-project:main Nov 22, 2023
6 checks passed
@FezVrasta
Copy link

@ciffelia I think the version has to be bumped in order for this to be published to npm?

https://github.com/swc-project/plugins/actions/runs/6953344296/job/18918312401

@ciffelia ciffelia deleted the fix-styled-components-local-variable-member branch November 22, 2023 11:09
@ciffelia ciffelia mentioned this pull request Nov 22, 2023
@kdy1 kdy1 self-assigned this Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

styled-components should support using variables of a closure
4 participants