-
-
Notifications
You must be signed in to change notification settings - Fork 139
fix: css-prop key shadowing, ts-jest detection, import aliasing #421
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4db3879
fix(css-prop): treat plain object keys as literal property names
claude 242cb5e
fix(detection): recognize TypeScript's __importDefault helper
claude 3b4ecf8
feat(detection): follow local alias of the styled import
claude e429be4
docs: state the tested matrix in README
claude 3d566a4
feat(css-prop): add cssPropImportPath option for React Native targets
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'babel-plugin-styled-components': minor | ||
| --- | ||
|
|
||
| Add a `cssPropImportPath` option to control which package the css-prop transform auto-imports `styled` from when the file has no existing styled import. Defaults to `'styled-components'` (existing behavior). React Native targets can set it to `'styled-components/native'` so the auto-injected import resolves to the right runtime. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'babel-plugin-styled-components': minor | ||
| --- | ||
|
|
||
| Detect styled declarations that go through a local alias of the import, including the TypeScript theme-typing pattern `const styled = baseStyled as ThemedStyledInterface<MyTheme>`. After type-stripping Babel sees a plain `const styled = baseStyled`, and the detector now follows single-identifier alias chains so `styled.div` resolves back to the original import. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'babel-plugin-styled-components': patch | ||
| --- | ||
|
|
||
| Fix invalid output when a `css={{ ... }}` object key matches a local binding name (e.g. `({ position }) => <div css={{ position: 'absolute' }} />`). The reducer no longer mis-treats non-computed property names as scope references, so plain keys stay literal while only computed `[expr]` keys are extracted as prop interpolations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'babel-plugin-styled-components': patch | ||
| --- | ||
|
|
||
| Recognize TypeScript's `__importDefault` interop helper alongside Babel's `_interopRequireDefault`. Files compiled through `tsc` / `ts-jest` (which emit `var sc_1 = __importDefault(require('styled-components'))`) now flow into the same detection path as Babel-compiled output, so styled declarations downstream pick up `displayName` and `componentId` as expected. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
test/fixtures/css-prop-object-key-shadowed-by-binding/.babelrc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "plugins": [ | ||
| [ | ||
| "../../../src", | ||
| { | ||
| "ssr": false, | ||
| "fileName": false, | ||
| "transpileTemplateLiterals": false | ||
| } | ||
| ] | ||
| ] | ||
| } |
9 changes: 9 additions & 0 deletions
9
test/fixtures/css-prop-object-key-shadowed-by-binding/code.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Regression: a non-computed object key (e.g. `position`) is a literal | ||
| // property name, never a binding reference, even when the local scope | ||
| // has a same-named binding. The css-prop object reducer must only rewrite | ||
| // computed keys (`[expr]`) into prop interpolations; plain keys stay literal. | ||
| import React from 'react' | ||
|
|
||
| const Outer = ({ position }) => ( | ||
| <div css={{ position: 'absolute', top: position }} /> | ||
| ) |
15 changes: 15 additions & 0 deletions
15
test/fixtures/css-prop-object-key-shadowed-by-binding/output.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import _styled from "styled-components"; | ||
| // Regression: a non-computed object key (e.g. `position`) is a literal | ||
| // property name, never a binding reference, even when the local scope | ||
| // has a same-named binding. The css-prop object reducer must only rewrite | ||
| // computed keys (`[expr]`) into prop interpolations; plain keys stay literal. | ||
| import React from 'react'; | ||
| const Outer = ({ | ||
| position | ||
| }) => <_StyledDiv $_css={position} />; | ||
| var _StyledDiv = _styled("div").withConfig({ | ||
| displayName: "_StyledDiv" | ||
| })(p => ({ | ||
| position: 'absolute', | ||
| top: p.$_css | ||
| })); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "plugins": [ | ||
| [ | ||
| "../../../src", | ||
| { | ||
| "ssr": false, | ||
| "fileName": false, | ||
| "transpileTemplateLiterals": false, | ||
| "cssPropImportPath": "styled-components/native" | ||
| } | ||
| ] | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // React Native targets need the auto-injected default import to come from | ||
| // `styled-components/native` rather than the DOM package. The | ||
| // `cssPropImportPath` option lets the consumer name the package; the rest | ||
| // of the css-prop transform is shape-compatible with RN already (the | ||
| // PascalCase JSX name routes through the identifier branch and produces | ||
| // `styled(View)` instead of `styled('View')`). | ||
| import { View } from 'react-native' | ||
|
|
||
| const Comp = () => <View css={{ backgroundColor: 'red' }} /> | ||
|
|
||
| export default Comp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import _styled from "styled-components/native"; | ||
| // React Native targets need the auto-injected default import to come from | ||
| // `styled-components/native` rather than the DOM package. The | ||
| // `cssPropImportPath` option lets the consumer name the package; the rest | ||
| // of the css-prop transform is shape-compatible with RN already (the | ||
| // PascalCase JSX name routes through the identifier branch and produces | ||
| // `styled(View)` instead of `styled('View')`). | ||
| import { View } from 'react-native'; | ||
| const Comp = () => <_StyledView />; | ||
| export default Comp; | ||
| var _StyledView = _styled(View).withConfig({ | ||
| displayName: "_StyledView" | ||
| })({ | ||
| backgroundColor: 'red' | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "plugins": [ | ||
| [ | ||
| "../../../src", | ||
| { | ||
| "ssr": false, | ||
| "fileName": false, | ||
| "transpileTemplateLiterals": false | ||
| } | ||
| ] | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Regression: styled-components v6 exposes a named `styled` export. The | ||
| // detector resolves `import { styled } from 'styled-components'` to the same | ||
| // local binding the default import would produce, so member and call forms | ||
| // both pick up displayName and componentId. | ||
| import { styled } from 'styled-components' | ||
|
|
||
| const Foo = styled.div` | ||
| color: red; | ||
| ` | ||
|
|
||
| const Bar = styled('span')` | ||
| color: blue; | ||
| ` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Regression: styled-components v6 exposes a named `styled` export. The | ||
| // detector resolves `import { styled } from 'styled-components'` to the same | ||
| // local binding the default import would produce, so member and call forms | ||
| // both pick up displayName and componentId. | ||
| import { styled } from 'styled-components'; | ||
| const Foo = styled.div.withConfig({ | ||
| displayName: "Foo" | ||
| })`color:red;`; | ||
| const Bar = styled('span').withConfig({ | ||
| displayName: "Bar" | ||
| })`color:blue;`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "plugins": [ | ||
| [ | ||
| "../../../src", | ||
| { | ||
| "fileName": false, | ||
| "transpileTemplateLiterals": false | ||
| } | ||
| ] | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Regression: the TypeScript compiler emits `__importDefault` as its CommonJS | ||
| // interop helper (Babel uses `_interopRequireDefault`). Both wrap a require() | ||
| // to expose `.default`, and the plugin needs to recognize either form so that | ||
| // downstream styled-component detection on `<local>.default.div` succeeds. | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| var styled_components_1 = __importDefault(require("styled-components")); | ||
| var Foo = styled_components_1.default.div` | ||
| color: red; | ||
| `; | ||
| var Bar = (0, styled_components_1.default)('span')` | ||
| color: blue; | ||
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Regression: the TypeScript compiler emits `__importDefault` as its CommonJS | ||
| // interop helper (Babel uses `_interopRequireDefault`). Both wrap a require() | ||
| // to expose `.default`, and the plugin needs to recognize either form so that | ||
| // downstream styled-component detection on `<local>.default.div` succeeds. | ||
| "use strict"; | ||
|
|
||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| var __importDefault = this && this.__importDefault || function (mod) { | ||
| return mod && mod.__esModule ? mod : { | ||
| "default": mod | ||
| }; | ||
| }; | ||
| var styled_components_1 = __importDefault(require("styled-components")); | ||
| var Foo = styled_components_1.default.div.withConfig({ | ||
| displayName: "Foo", | ||
| componentId: "sc-1km53of-0" | ||
| })`color:red;`; | ||
| var Bar = (0, styled_components_1.default)('span').withConfig({ | ||
| displayName: "Bar", | ||
| componentId: "sc-1km53of-1" | ||
| })`color:blue;`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "plugins": [ | ||
| [ | ||
| "../../../src", | ||
| { | ||
| "fileName": false, | ||
| "transpileTemplateLiterals": false | ||
| } | ||
| ] | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Regression: TypeScript theme-typing patterns re-bind the default import | ||
| // through a local `const`, e.g. `const styled = baseStyled as ThemedSC<...>`. | ||
| // After type-stripping, Babel sees a plain `const styled = baseStyled`. | ||
| // The detector must follow such single-identifier aliases so `styled.div` is | ||
| // still recognized as a styled-component declaration. | ||
| import baseStyled from 'styled-components' | ||
|
|
||
| const styled = baseStyled | ||
|
|
||
| const Foo = styled.div` | ||
| color: red; | ||
| ` | ||
|
|
||
| const Bar = styled('span')` | ||
| color: blue; | ||
| ` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Regression: TypeScript theme-typing patterns re-bind the default import | ||
| // through a local `const`, e.g. `const styled = baseStyled as ThemedSC<...>`. | ||
| // After type-stripping, Babel sees a plain `const styled = baseStyled`. | ||
| // The detector must follow such single-identifier aliases so `styled.div` is | ||
| // still recognized as a styled-component declaration. | ||
| import baseStyled from 'styled-components'; | ||
| const styled = baseStyled; | ||
| const Foo = styled.div.withConfig({ | ||
| displayName: "Foo", | ||
| componentId: "sc-ep8j7b-0" | ||
| })`color:red;`; | ||
| const Bar = styled('span').withConfig({ | ||
| displayName: "Bar", | ||
| componentId: "sc-ep8j7b-1" | ||
| })`color:blue;`; |
12 changes: 12 additions & 0 deletions
12
test/fixtures/withconfig-chained-on-existing-config/.babelrc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "plugins": [ | ||
| [ | ||
| "../../../src", | ||
| { | ||
| "ssr": true, | ||
| "fileName": false, | ||
| "transpileTemplateLiterals": false | ||
| } | ||
| ] | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.