Skip to content

Commit 1b3791d

Browse files
ahutchingsemmatown
authored andcommitted
babel-plugin-emotion: Fix autoLabel crash on css used in expression assigned to an object pattern (#1526)
* Add failing test * babel-plugin-emotion: Fix `autoLabel` crash on `css` used in expression assigned to an object pattern Ensure that the variable declarator's id is an identifier before using its name as the label. * Update snapshot * Add a changeset
1 parent a835b89 commit 1b3791d

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"releases": [{ "name": "babel-plugin-emotion", "type": "patch" }],
3+
"dependents": []
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `autoLabel` crash on `css` used in expression assigned to an object pattern

packages/babel-plugin-emotion/__tests__/__snapshots__/css.js.snap

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,41 @@ function doThing() {
446446
}"
447447
`;
448448
449+
exports[`@emotion/babel-plugin-core css object-pattern-variable-declarator 1`] = `
450+
"import { css } from '@emotion/core'
451+
import { extractCritical } from 'emotion-server'
452+
import React from 'react'
453+
import { renderToString } from 'react-dom/server'
454+
455+
const { css: styles } = extractCritical(
456+
renderToString(
457+
<div
458+
css={css\`
459+
color: hotpink;
460+
\`}
461+
/>
462+
)
463+
)
464+
465+
466+
↓ ↓ ↓ ↓ ↓ ↓
467+
468+
import { css } from '@emotion/core';
469+
import { extractCritical } from 'emotion-server';
470+
import React from 'react';
471+
import { renderToString } from 'react-dom/server';
472+
const {
473+
css: styles
474+
} = extractCritical(renderToString(<div css={process.env.NODE_ENV === \\"production\\" ? {
475+
name: \\"1lrxbo5\\",
476+
styles: \\"color:hotpink;\\"
477+
} : {
478+
name: \\"1lrxbo5\\",
479+
styles: \\"color:hotpink;\\",
480+
map: \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm9iamVjdC1wYXR0ZXJuLXZhcmlhYmxlLWRlY2xhcmF0b3IuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBUWMiLCJmaWxlIjoib2JqZWN0LXBhdHRlcm4tdmFyaWFibGUtZGVjbGFyYXRvci5qcyIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IGNzcyB9IGZyb20gJ0BlbW90aW9uL2NvcmUnXG5pbXBvcnQgeyBleHRyYWN0Q3JpdGljYWwgfSBmcm9tICdlbW90aW9uLXNlcnZlcidcbmltcG9ydCBSZWFjdCBmcm9tICdyZWFjdCdcbmltcG9ydCB7IHJlbmRlclRvU3RyaW5nIH0gZnJvbSAncmVhY3QtZG9tL3NlcnZlcidcblxuY29uc3QgeyBjc3M6IHN0eWxlcyB9ID0gZXh0cmFjdENyaXRpY2FsKFxuICByZW5kZXJUb1N0cmluZyhcbiAgICA8ZGl2XG4gICAgICBjc3M9e2Nzc2BcbiAgICAgICAgY29sb3I6IGhvdHBpbms7XG4gICAgICBgfVxuICAgIC8+XG4gIClcbilcbiJdfQ== */\\"
481+
}} />));"
482+
`;
483+
449484
exports[`@emotion/babel-plugin-core css other-imports 1`] = `
450485
"import { nonExistantImport } from '@emotion/css'
451486
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { css } from '@emotion/core'
2+
import { extractCritical } from 'emotion-server'
3+
import React from 'react'
4+
import { renderToString } from 'react-dom/server'
5+
6+
const { css: styles } = extractCritical(
7+
renderToString(
8+
<div
9+
css={css`
10+
color: hotpink;
11+
`}
12+
/>
13+
)
14+
)

packages/babel-plugin-emotion/__tests__/css-macro/__snapshots__/index.js.snap

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,36 @@ function doThing() {
446446
}"
447447
`;
448448
449+
exports[`@emotion/css/macro object-pattern-variable-declarator 1`] = `
450+
"import { css } from '@emotion/core'
451+
import { extractCritical } from 'emotion-server'
452+
import React from 'react'
453+
import { renderToString } from 'react-dom/server'
454+
455+
const { css: styles } = extractCritical(
456+
renderToString(
457+
<div
458+
css={css\`
459+
color: hotpink;
460+
\`}
461+
/>
462+
)
463+
)
464+
465+
466+
↓ ↓ ↓ ↓ ↓ ↓
467+
468+
import { css } from '@emotion/core';
469+
import { extractCritical } from 'emotion-server';
470+
import React from 'react';
471+
import { renderToString } from 'react-dom/server';
472+
const {
473+
css: styles
474+
} = extractCritical(renderToString(<div css={css\`
475+
color: hotpink;
476+
\`} />));"
477+
`;
478+
449479
exports[`@emotion/css/macro other-imports 1`] = `
450480
"import { nonExistantImport } from '@emotion/css/macro'
451481

packages/babel-plugin-emotion/src/utils/label.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ function getDeclaratorName(path, t) {
5959
// we probably have a css call assigned to a variable
6060
// so we'll just return the variable name
6161
if (parent.isVariableDeclarator()) {
62-
return parent.node.id.name
62+
if (t.isIdentifier(parent.node.id)) {
63+
return parent.node.id.name
64+
}
65+
return ''
6366
}
6467

6568
// we probably have an inline css prop usage

0 commit comments

Comments
 (0)