From e64ac092e518b2985fa1a74b764afe482df3e785 Mon Sep 17 00:00:00 2001 From: Evan Jacobs Date: Mon, 2 Nov 2026 22:52:34 -0500 Subject: [PATCH 1/2] fix(css-prop): place generated components after the values their css reads --- .changeset/css-prop-local-component-order.md | 5 ++ src/visitors/transpileCssProp.js | 54 +++++++++++++++++-- .../.babelrc | 7 +++ .../code.js | 16 ++++++ .../output.js | 15 ++++++ .../.babelrc | 7 +++ .../code.js | 12 +++++ .../output.js | 13 +++++ .../.babelrc | 7 +++ .../code.js | 11 ++++ .../output.js | 13 +++++ .../.babelrc | 10 ++++ .../code.js | 11 ++++ .../output.js | 20 +++++++ .../.babelrc | 7 +++ .../code.js | 11 ++++ .../output.js | 13 +++++ .../css-prop-local-component-member/.babelrc | 7 +++ .../css-prop-local-component-member/code.js | 11 ++++ .../css-prop-local-component-member/output.js | 11 ++++ .../.babelrc | 7 +++ .../code.js | 11 ++++ .../output.js | 13 +++++ 23 files changed, 287 insertions(+), 5 deletions(-) create mode 100644 .changeset/css-prop-local-component-order.md create mode 100644 test/fixtures/css-prop-local-component-dep-after-component/.babelrc create mode 100644 test/fixtures/css-prop-local-component-dep-after-component/code.js create mode 100644 test/fixtures/css-prop-local-component-dep-after-component/output.js create mode 100644 test/fixtures/css-prop-local-component-dep-after-usage/.babelrc create mode 100644 test/fixtures/css-prop-local-component-dep-after-usage/code.js create mode 100644 test/fixtures/css-prop-local-component-dep-after-usage/output.js create mode 100644 test/fixtures/css-prop-local-component-dep-before-component/.babelrc create mode 100644 test/fixtures/css-prop-local-component-dep-before-component/code.js create mode 100644 test/fixtures/css-prop-local-component-dep-before-component/output.js create mode 100644 test/fixtures/css-prop-local-component-destructured-preset-env/.babelrc create mode 100644 test/fixtures/css-prop-local-component-destructured-preset-env/code.js create mode 100644 test/fixtures/css-prop-local-component-destructured-preset-env/output.js create mode 100644 test/fixtures/css-prop-local-component-destructured/.babelrc create mode 100644 test/fixtures/css-prop-local-component-destructured/code.js create mode 100644 test/fixtures/css-prop-local-component-destructured/output.js create mode 100644 test/fixtures/css-prop-local-component-member/.babelrc create mode 100644 test/fixtures/css-prop-local-component-member/code.js create mode 100644 test/fixtures/css-prop-local-component-member/output.js create mode 100644 test/fixtures/css-prop-local-component-same-statement/.babelrc create mode 100644 test/fixtures/css-prop-local-component-same-statement/code.js create mode 100644 test/fixtures/css-prop-local-component-same-statement/output.js diff --git a/.changeset/css-prop-local-component-order.md b/.changeset/css-prop-local-component-order.md new file mode 100644 index 00000000..c8df68ef --- /dev/null +++ b/.changeset/css-prop-local-component-order.md @@ -0,0 +1,5 @@ +--- +'babel-plugin-styled-components': patch +--- + +Fix a `ReferenceError: Cannot access 'x' before initialization` when the `css` prop is used on a component declared in the same file (such as `const { Paragraph } = Typography`) and the css reads a `const` declared further down; the generated component now follows every value its css reads. diff --git a/src/visitors/transpileCssProp.js b/src/visitors/transpileCssProp.js index c34ac531..6f0bcff7 100644 --- a/src/visitors/transpileCssProp.js +++ b/src/visitors/transpileCssProp.js @@ -45,6 +45,48 @@ const getLocalIdentifier = path => { return identifier } +const topLevelStatement = path => + path.find(p => p.parentPath && p.parentPath.isProgram()) + +// Top-level statements declaring the program-scope values the css value reads. +// Imports and hoisted function declarations are always initialized, so they +// never constrain placement. +const cssDependencyStatements = (attributePath, bindings) => { + const statements = [] + + attributePath.get('value').traverse({ + ReferencedIdentifier(p) { + const binding = bindings[p.node.name] + + if ( + binding && + binding.kind !== 'module' && + binding.kind !== 'hoisted' && + p.scope.getBinding(p.node.name) === binding + ) { + statements.push(topLevelStatement(binding.path)) + } + }, + }) + + return statements +} + +// The generated declaration goes after the component's own statement and after +// every dependency declared in a later statement. A dependency sharing the +// usage's statement cannot be satisfied, so it is skipped. +const latestProgramStatement = (componentPath, dependencies, usagePath) => { + const usageKey = topLevelStatement(usagePath).key + + return dependencies.reduce( + (latest, statement) => + statement.key > latest.key && statement.key !== usageKey + ? statement + : latest, + topLevelStatement(componentPath) + ) +} + const PROGRAM_BODY_PRENUMBERED = 'styled-components-program-prenumbered' export default t => { @@ -112,11 +154,13 @@ export default t => { bindings[name] && !t.isImportDeclaration(bindings[name].path.parent) ) { - injector = nodeToInsert => - (t.isVariableDeclaration(bindings[name].path.parent) - ? bindings[name].path.parentPath - : bindings[name].path - ).insertAfter(nodeToInsert) + const anchor = latestProgramStatement( + bindings[name].path, + cssDependencyStatements(path, bindings), + path + ) + + injector = nodeToInsert => anchor.insertAfter(nodeToInsert) } } diff --git a/test/fixtures/css-prop-local-component-dep-after-component/.babelrc b/test/fixtures/css-prop-local-component-dep-after-component/.babelrc new file mode 100644 index 00000000..460a26ff --- /dev/null +++ b/test/fixtures/css-prop-local-component-dep-after-component/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + [ + "../../../src" + ] + ] +} diff --git a/test/fixtures/css-prop-local-component-dep-after-component/code.js b/test/fixtures/css-prop-local-component-dep-after-component/code.js new file mode 100644 index 00000000..4690e44f --- /dev/null +++ b/test/fixtures/css-prop-local-component-dep-after-component/code.js @@ -0,0 +1,16 @@ +import { css } from 'styled-components' +import { Typography } from 'antd' + +const { Paragraph } = Typography +const color = 'red' +const base = css` + color: ${color}; +` +const paragraphCss = css` + ${base}; + font-size: 14px; +` + +export const Text = ({ children }) => ( + {children} +) diff --git a/test/fixtures/css-prop-local-component-dep-after-component/output.js b/test/fixtures/css-prop-local-component-dep-after-component/output.js new file mode 100644 index 00000000..1b97c13a --- /dev/null +++ b/test/fixtures/css-prop-local-component-dep-after-component/output.js @@ -0,0 +1,15 @@ +import _styled, { css } from 'styled-components'; +import { Typography } from 'antd'; +const { + Paragraph +} = Typography; +const color = 'red'; +const base = css(["color:", ";"], color); +const paragraphCss = css(["", ";font-size:14px;"], base); +var _StyledParagraph = _styled(Paragraph).withConfig({ + displayName: "code___StyledParagraph", + componentId: "sc-1vj5czu-0" +})(["", ""], paragraphCss); +export const Text = ({ + children +}) => <_StyledParagraph>{children}; diff --git a/test/fixtures/css-prop-local-component-dep-after-usage/.babelrc b/test/fixtures/css-prop-local-component-dep-after-usage/.babelrc new file mode 100644 index 00000000..460a26ff --- /dev/null +++ b/test/fixtures/css-prop-local-component-dep-after-usage/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + [ + "../../../src" + ] + ] +} diff --git a/test/fixtures/css-prop-local-component-dep-after-usage/code.js b/test/fixtures/css-prop-local-component-dep-after-usage/code.js new file mode 100644 index 00000000..0baf998a --- /dev/null +++ b/test/fixtures/css-prop-local-component-dep-after-usage/code.js @@ -0,0 +1,12 @@ +import { css } from 'styled-components' +import { Typography } from 'antd' + +const { Paragraph } = Typography + +export const Text = ({ children }) => ( + {children} +) + +const paragraphCss = css` + font-size: 14px; +` diff --git a/test/fixtures/css-prop-local-component-dep-after-usage/output.js b/test/fixtures/css-prop-local-component-dep-after-usage/output.js new file mode 100644 index 00000000..6b676082 --- /dev/null +++ b/test/fixtures/css-prop-local-component-dep-after-usage/output.js @@ -0,0 +1,13 @@ +import _styled, { css } from 'styled-components'; +import { Typography } from 'antd'; +const { + Paragraph +} = Typography; +export const Text = ({ + children +}) => <_StyledParagraph>{children}; +const paragraphCss = css(["font-size:14px;"]); +var _StyledParagraph = _styled(Paragraph).withConfig({ + displayName: "code___StyledParagraph", + componentId: "sc-8er70n-0" +})(["", ""], paragraphCss); diff --git a/test/fixtures/css-prop-local-component-dep-before-component/.babelrc b/test/fixtures/css-prop-local-component-dep-before-component/.babelrc new file mode 100644 index 00000000..460a26ff --- /dev/null +++ b/test/fixtures/css-prop-local-component-dep-before-component/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + [ + "../../../src" + ] + ] +} diff --git a/test/fixtures/css-prop-local-component-dep-before-component/code.js b/test/fixtures/css-prop-local-component-dep-before-component/code.js new file mode 100644 index 00000000..d43a6def --- /dev/null +++ b/test/fixtures/css-prop-local-component-dep-before-component/code.js @@ -0,0 +1,11 @@ +import { css } from 'styled-components' +import { Typography } from 'antd' + +const paragraphCss = css` + font-size: 14px; +` +const { Paragraph } = Typography + +export const Text = ({ children }) => ( + {children} +) diff --git a/test/fixtures/css-prop-local-component-dep-before-component/output.js b/test/fixtures/css-prop-local-component-dep-before-component/output.js new file mode 100644 index 00000000..052d8240 --- /dev/null +++ b/test/fixtures/css-prop-local-component-dep-before-component/output.js @@ -0,0 +1,13 @@ +import _styled, { css } from 'styled-components'; +import { Typography } from 'antd'; +const paragraphCss = css(["font-size:14px;"]); +const { + Paragraph +} = Typography; +var _StyledParagraph = _styled(Paragraph).withConfig({ + displayName: "code___StyledParagraph", + componentId: "sc-16e4q1q-0" +})(["", ""], paragraphCss); +export const Text = ({ + children +}) => <_StyledParagraph>{children}; diff --git a/test/fixtures/css-prop-local-component-destructured-preset-env/.babelrc b/test/fixtures/css-prop-local-component-destructured-preset-env/.babelrc new file mode 100644 index 00000000..a5f4926d --- /dev/null +++ b/test/fixtures/css-prop-local-component-destructured-preset-env/.babelrc @@ -0,0 +1,10 @@ +{ + "presets": [ + "@babel/preset-env" + ], + "plugins": [ + [ + "../../../src" + ] + ] +} diff --git a/test/fixtures/css-prop-local-component-destructured-preset-env/code.js b/test/fixtures/css-prop-local-component-destructured-preset-env/code.js new file mode 100644 index 00000000..5d59567a --- /dev/null +++ b/test/fixtures/css-prop-local-component-destructured-preset-env/code.js @@ -0,0 +1,11 @@ +import { css } from 'styled-components' +import { Typography } from 'antd' + +const { Paragraph } = Typography +const paragraphCss = css` + font-size: 14px; +` + +export const Text = ({ children }) => ( + {children} +) diff --git a/test/fixtures/css-prop-local-component-destructured-preset-env/output.js b/test/fixtures/css-prop-local-component-destructured-preset-env/output.js new file mode 100644 index 00000000..27290c73 --- /dev/null +++ b/test/fixtures/css-prop-local-component-destructured-preset-env/output.js @@ -0,0 +1,20 @@ +"use strict"; + +function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Text = void 0; +var _styledComponents = _interopRequireWildcard(require("styled-components")); +var _antd = require("antd"); +function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } +var Paragraph = _antd.Typography.Paragraph; +var paragraphCss = (0, _styledComponents.css)(["font-size:14px;"]); +var _StyledParagraph = (0, _styledComponents["default"])(Paragraph).withConfig({ + displayName: "code___StyledParagraph", + componentId: "sc-slzj7w-0" +})(["", ""], paragraphCss); +var Text = exports.Text = function Text(_ref) { + var children = _ref.children; + return <_StyledParagraph>{children}; +}; diff --git a/test/fixtures/css-prop-local-component-destructured/.babelrc b/test/fixtures/css-prop-local-component-destructured/.babelrc new file mode 100644 index 00000000..460a26ff --- /dev/null +++ b/test/fixtures/css-prop-local-component-destructured/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + [ + "../../../src" + ] + ] +} diff --git a/test/fixtures/css-prop-local-component-destructured/code.js b/test/fixtures/css-prop-local-component-destructured/code.js new file mode 100644 index 00000000..5d59567a --- /dev/null +++ b/test/fixtures/css-prop-local-component-destructured/code.js @@ -0,0 +1,11 @@ +import { css } from 'styled-components' +import { Typography } from 'antd' + +const { Paragraph } = Typography +const paragraphCss = css` + font-size: 14px; +` + +export const Text = ({ children }) => ( + {children} +) diff --git a/test/fixtures/css-prop-local-component-destructured/output.js b/test/fixtures/css-prop-local-component-destructured/output.js new file mode 100644 index 00000000..3ffe810e --- /dev/null +++ b/test/fixtures/css-prop-local-component-destructured/output.js @@ -0,0 +1,13 @@ +import _styled, { css } from 'styled-components'; +import { Typography } from 'antd'; +const { + Paragraph +} = Typography; +const paragraphCss = css(["font-size:14px;"]); +var _StyledParagraph = _styled(Paragraph).withConfig({ + displayName: "code___StyledParagraph", + componentId: "sc-1fjk2wh-0" +})(["", ""], paragraphCss); +export const Text = ({ + children +}) => <_StyledParagraph>{children}; diff --git a/test/fixtures/css-prop-local-component-member/.babelrc b/test/fixtures/css-prop-local-component-member/.babelrc new file mode 100644 index 00000000..460a26ff --- /dev/null +++ b/test/fixtures/css-prop-local-component-member/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + [ + "../../../src" + ] + ] +} diff --git a/test/fixtures/css-prop-local-component-member/code.js b/test/fixtures/css-prop-local-component-member/code.js new file mode 100644 index 00000000..aa4c3670 --- /dev/null +++ b/test/fixtures/css-prop-local-component-member/code.js @@ -0,0 +1,11 @@ +import { css } from 'styled-components' +import { Typography } from 'antd' + +const Paragraph = Typography.Paragraph +const paragraphCss = css` + font-size: 14px; +` + +export const Text = ({ children }) => ( + {children} +) diff --git a/test/fixtures/css-prop-local-component-member/output.js b/test/fixtures/css-prop-local-component-member/output.js new file mode 100644 index 00000000..3b33c320 --- /dev/null +++ b/test/fixtures/css-prop-local-component-member/output.js @@ -0,0 +1,11 @@ +import _styled, { css } from 'styled-components'; +import { Typography } from 'antd'; +const Paragraph = Typography.Paragraph; +const paragraphCss = css(["font-size:14px;"]); +var _StyledParagraph = _styled(Paragraph).withConfig({ + displayName: "code___StyledParagraph", + componentId: "sc-vg1kw6-0" +})(["", ""], paragraphCss); +export const Text = ({ + children +}) => <_StyledParagraph>{children}; diff --git a/test/fixtures/css-prop-local-component-same-statement/.babelrc b/test/fixtures/css-prop-local-component-same-statement/.babelrc new file mode 100644 index 00000000..460a26ff --- /dev/null +++ b/test/fixtures/css-prop-local-component-same-statement/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + [ + "../../../src" + ] + ] +} diff --git a/test/fixtures/css-prop-local-component-same-statement/code.js b/test/fixtures/css-prop-local-component-same-statement/code.js new file mode 100644 index 00000000..a5e028af --- /dev/null +++ b/test/fixtures/css-prop-local-component-same-statement/code.js @@ -0,0 +1,11 @@ +import { css } from 'styled-components' +import { Typography } from 'antd' + +const { Paragraph } = Typography + +export const Text = ({ children }) => ( + {children} + ), + paragraphCss = css` + font-size: 14px; + ` diff --git a/test/fixtures/css-prop-local-component-same-statement/output.js b/test/fixtures/css-prop-local-component-same-statement/output.js new file mode 100644 index 00000000..af958f58 --- /dev/null +++ b/test/fixtures/css-prop-local-component-same-statement/output.js @@ -0,0 +1,13 @@ +import _styled, { css } from 'styled-components'; +import { Typography } from 'antd'; +const { + Paragraph +} = Typography; +var _StyledParagraph = _styled(Paragraph).withConfig({ + displayName: "code___StyledParagraph", + componentId: "sc-ou3w6i-0" +})(["", ""], paragraphCss); +export const Text = ({ + children + }) => <_StyledParagraph>{children}, + paragraphCss = css(["font-size:14px;"]); From 969dfbd861e1cfd9ceafee60ab2c404265960754 Mon Sep 17 00:00:00 2001 From: Evan Jacobs Date: Mon, 2 Nov 2026 23:08:30 -0500 Subject: [PATCH 2/2] fix(css-prop): ignore reads inside css functions when placing generated components A function interpolation reads its values only when called, so a later binding it references must not move the generated declaration past an eager top-level usage. --- src/visitors/transpileCssProp.js | 7 +++++-- .../.babelrc | 7 +++++++ .../code.js | 14 ++++++++++++++ .../output.js | 12 ++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/.babelrc create mode 100644 test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/code.js create mode 100644 test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/output.js diff --git a/src/visitors/transpileCssProp.js b/src/visitors/transpileCssProp.js index 6f0bcff7..1cb5b633 100644 --- a/src/visitors/transpileCssProp.js +++ b/src/visitors/transpileCssProp.js @@ -49,12 +49,15 @@ const topLevelStatement = path => path.find(p => p.parentPath && p.parentPath.isProgram()) // Top-level statements declaring the program-scope values the css value reads. -// Imports and hoisted function declarations are always initialized, so they -// never constrain placement. +// Imports and hoisted function declarations are always initialized, and a +// function body reads only when called, so none of them constrain placement. const cssDependencyStatements = (attributePath, bindings) => { const statements = [] attributePath.get('value').traverse({ + Function(p) { + p.skip() + }, ReferencedIdentifier(p) { const binding = bindings[p.node.name] diff --git a/test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/.babelrc b/test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/.babelrc new file mode 100644 index 00000000..460a26ff --- /dev/null +++ b/test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + [ + "../../../src" + ] + ] +} diff --git a/test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/code.js b/test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/code.js new file mode 100644 index 00000000..70d64ee7 --- /dev/null +++ b/test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/code.js @@ -0,0 +1,14 @@ +import { css } from 'styled-components' +import { Typography } from 'antd' + +const { Paragraph } = Typography + +const size = css` + font-size: 14px; +` + +export const el = ( + color};`}>Hi +) + +const color = 'red' diff --git a/test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/output.js b/test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/output.js new file mode 100644 index 00000000..41e52277 --- /dev/null +++ b/test/fixtures/css-prop-local-component-lazy-dep-after-eager-usage/output.js @@ -0,0 +1,12 @@ +import _styled, { css } from 'styled-components'; +import { Typography } from 'antd'; +const { + Paragraph +} = Typography; +const size = css(["font-size:14px;"]); +var _StyledParagraph = _styled(Paragraph).withConfig({ + displayName: "code___StyledParagraph", + componentId: "sc-4sa468-0" +})(["", " color:", ";"], size, () => color); +export const el = <_StyledParagraph>Hi; +const color = 'red';