Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/css-prop-local-component-order.md
Original file line number Diff line number Diff line change
@@ -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.
57 changes: 52 additions & 5 deletions src/visitors/transpileCssProp.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,51 @@ 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, 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]

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 => {
Expand Down Expand Up @@ -112,11 +157,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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
[
"../../../src"
]
]
}
16 changes: 16 additions & 0 deletions test/fixtures/css-prop-local-component-dep-after-component/code.js
Original file line number Diff line number Diff line change
@@ -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 }) => (
<Paragraph css={paragraphCss}>{children}</Paragraph>
)
Original file line number Diff line number Diff line change
@@ -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}</_StyledParagraph>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
[
"../../../src"
]
]
}
12 changes: 12 additions & 0 deletions test/fixtures/css-prop-local-component-dep-after-usage/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { css } from 'styled-components'
import { Typography } from 'antd'

const { Paragraph } = Typography

export const Text = ({ children }) => (
<Paragraph css={paragraphCss}>{children}</Paragraph>
)

const paragraphCss = css`
font-size: 14px;
`
13 changes: 13 additions & 0 deletions test/fixtures/css-prop-local-component-dep-after-usage/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import _styled, { css } from 'styled-components';
import { Typography } from 'antd';
const {
Paragraph
} = Typography;
export const Text = ({
children
}) => <_StyledParagraph>{children}</_StyledParagraph>;
const paragraphCss = css(["font-size:14px;"]);
var _StyledParagraph = _styled(Paragraph).withConfig({
displayName: "code___StyledParagraph",
componentId: "sc-8er70n-0"
})(["", ""], paragraphCss);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
[
"../../../src"
]
]
}
Original file line number Diff line number Diff line change
@@ -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 }) => (
<Paragraph css={paragraphCss}>{children}</Paragraph>
)
Original file line number Diff line number Diff line change
@@ -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}</_StyledParagraph>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
"@babel/preset-env"
],
"plugins": [
[
"../../../src"
]
]
}
Original file line number Diff line number Diff line change
@@ -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 }) => (
<Paragraph css={paragraphCss}>{children}</Paragraph>
)
Original file line number Diff line number Diff line change
@@ -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}</_StyledParagraph>;
};
7 changes: 7 additions & 0 deletions test/fixtures/css-prop-local-component-destructured/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
[
"../../../src"
]
]
}
11 changes: 11 additions & 0 deletions test/fixtures/css-prop-local-component-destructured/code.js
Original file line number Diff line number Diff line change
@@ -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 }) => (
<Paragraph css={paragraphCss}>{children}</Paragraph>
)
13 changes: 13 additions & 0 deletions test/fixtures/css-prop-local-component-destructured/output.js
Original file line number Diff line number Diff line change
@@ -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}</_StyledParagraph>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
[
"../../../src"
]
]
}
Original file line number Diff line number Diff line change
@@ -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 = (
<Paragraph css={css`${size} color: ${() => color};`}>Hi</Paragraph>
)

const color = 'red'
Original file line number Diff line number Diff line change
@@ -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</_StyledParagraph>;
const color = 'red';
7 changes: 7 additions & 0 deletions test/fixtures/css-prop-local-component-member/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
[
"../../../src"
]
]
}
11 changes: 11 additions & 0 deletions test/fixtures/css-prop-local-component-member/code.js
Original file line number Diff line number Diff line change
@@ -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 }) => (
<Paragraph css={paragraphCss}>{children}</Paragraph>
)
11 changes: 11 additions & 0 deletions test/fixtures/css-prop-local-component-member/output.js
Original file line number Diff line number Diff line change
@@ -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}</_StyledParagraph>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
[
"../../../src"
]
]
}
11 changes: 11 additions & 0 deletions test/fixtures/css-prop-local-component-same-statement/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { css } from 'styled-components'
import { Typography } from 'antd'

const { Paragraph } = Typography

export const Text = ({ children }) => (
<Paragraph css={paragraphCss}>{children}</Paragraph>
),
paragraphCss = css`
font-size: 14px;
`
13 changes: 13 additions & 0 deletions test/fixtures/css-prop-local-component-same-statement/output.js
Original file line number Diff line number Diff line change
@@ -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}</_StyledParagraph>,
paragraphCss = css(["font-size:14px;"]);
Loading