Skip to content

Commit ca95f38

Browse files
Andaristemmatown
authored andcommittedOct 29, 2019
Warn about being used as object style's key (#1580)
1 parent a55f3d4 commit ca95f38

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed
 

‎.changeset/bright-rules-know.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/serialize': patch
3+
---
4+
5+
Warn about `undefined` being used as object style's key

‎packages/core/__tests__/__snapshots__/css.js.snap

+17-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,25 @@ Array [
2323
]
2424
`;
2525

26+
exports[`array fallback (using camelCased property) 1`] = `
27+
.emotion-0 {
28+
background-color: green;
29+
background-color: hotpink;
30+
}
31+
32+
<div>
33+
<div
34+
className="emotion-0"
35+
>
36+
something
37+
</div>
38+
</div>
39+
`;
40+
2641
exports[`array fallback 1`] = `
2742
.emotion-0 {
28-
display: green;
29-
display: hotpink;
43+
color: green;
44+
color: hotpink;
3045
}
3146
3247
<div>

‎packages/core/__tests__/css.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,23 @@ test('array fallback', () => {
102102
<div>
103103
<div
104104
css={{
105-
display: ['green', 'hotpink']
105+
color: ['green', 'hotpink']
106+
}}
107+
>
108+
something
109+
</div>
110+
</div>
111+
)
112+
113+
expect(tree.toJSON()).toMatchSnapshot()
114+
})
115+
116+
test('array fallback (using camelCased property)', () => {
117+
const tree = renderer.create(
118+
<div>
119+
<div
120+
css={{
121+
backgroundColor: ['green', 'hotpink']
106122
}}
107123
>
108124
something

‎packages/serialize/src/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Because you write your CSS inside a JavaScript string you actually have to do do
1313
You can read more about this here:
1414
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#ES2018_revision_of_illegal_escape_sequences`
1515

16+
const UNDEFINED_AS_OBJECT_KEY_ERROR =
17+
"You have passed in falsy value as style object's key (can happen when in example you pass unexported component as computed key)."
18+
1619
let hyphenateRegex = /[A-Z]|^ms/g
1720
let animationRegex = /_EMO_([^_]+?)_([^]*?)_EMO_/g
1821

@@ -308,6 +311,12 @@ function createStringFromObject(
308311
break
309312
}
310313
default: {
314+
if (
315+
process.env.NODE_ENV !== 'production' &&
316+
key === 'undefined'
317+
) {
318+
console.error(UNDEFINED_AS_OBJECT_KEY_ERROR)
319+
}
311320
string += `${key}{${interpolated}}`
312321
}
313322
}

‎packages/styled/__tests__/warnings.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// @flow
22
import 'test-utils/legacy-env'
3+
import * as React from 'react'
34
import { css } from '@emotion/core'
45
import styled from '@emotion/styled'
6+
import { render } from '@testing-library/react'
57

68
// $FlowFixMe
79
console.error = jest.fn()
@@ -48,3 +50,17 @@ it('warns about illegal escape sequences inside non-first quasi of template lite
4850
]
4951
`)
5052
})
53+
54+
it("warns about undefined being passed as object style's key", () => {
55+
let ListItem
56+
// $FlowFixMe
57+
const List = styled.ul({ [ListItem]: { color: 'hotpink' } })
58+
59+
render(<List />)
60+
61+
expect((console.error: any).mock.calls[0]).toMatchInlineSnapshot(`
62+
Array [
63+
"You have passed in falsy value as style object's key (can happen when in example you pass unexported component as computed key).",
64+
]
65+
`)
66+
})

0 commit comments

Comments
 (0)
Please sign in to comment.