atlassian-labs / compiled

A familiar and performant compile time CSS-in-JS library for React.
https://compiledcssinjs.com
Apache License 2.0
1.99k stars 67 forks source link

Fix Compiled props being incorrectly forwarded when default values used #1630

Closed dddlr closed 8 months ago

dddlr commented 8 months ago

Fixes #1629

https://github.com/atlassian-labs/compiled/blob/9860df3850ac5023ffa93201f5648fd7c81807c5/packages/babel-plugin/src/utils/build-styled-component.ts#L80 expects a MemberExpression in order to detect what props shouldn't be forwarded to React. All other props are forwarded to React. (see this PR for more context: https://github.com/atlassian-labs/compiled/pull/1273)

However, when we handle default values in situations like these...

import { styled } from '@compiled/react';
const styled.div({
    margin: ({ marginTop = 12 }) => marginTop,
});

@compiled/babel-plugin used the t.identifier function to generate the __cmplp.marginTop expression:

styled.div<{
  marginTop?: string;
}>({
  marginTop: __cmplp => __cmplp.marginTop ?? '24px'
})

The above appears correct, because the build output is identical and Babel doesn't stop us from passing invalid expressions to the t.identifier function... however, the use of t.identifier and t.memberExpression meant that it would never get detected as a MemberExpression, and thus the prop would never not be forwarded to React :(

This PR fixes this by replacing t.identifier with t.memberExpression.

changeset-bot[bot] commented 8 months ago

🦋 Changeset detected

Latest commit: c3b3baccb14fab20fdecd6c2271d6e95f17ca4e4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ---------------------- | ----- | | @compiled/babel-plugin | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR