Skip to content

Commit

Permalink
Improve error message for shorthand properties with missing units. (#841
Browse files Browse the repository at this point in the history
)

* log error message if no units in shorthand prop

* use jest.fn() in tests

* Add changeset
  • Loading branch information
nitin42 authored and Andarist committed Oct 22, 2019
1 parent dfab765 commit ae90f00
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/eight-rats-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@emotion/native': patch
'@emotion/primitives-core': patch
'@emotion/primitives': patch
---

Improve error message for shorthand properties with missing units.
15 changes: 15 additions & 0 deletions packages/native/test/native-styled.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const StyleSheet = reactNative.StyleSheet

jest.mock('react-native')

console.error = jest.fn()

const theme = { backgroundColor: 'magenta', display: 'flex' }

describe('Emotion native styled', () => {
Expand Down Expand Up @@ -136,4 +138,17 @@ describe('Emotion native styled', () => {

expect(tree).toMatchSnapshot()
})

it('Log error message if units are not specified when using shorthand properties', () => {
const Text = styled.Text`
margin: 20px;
padding: 20;
`

renderer.create(<Text>Hello World</Text>)

expect(console.error).toBeCalledWith(
"'padding' shorthand property requires units for example - padding: 20px or padding: 10px 20px 40px 50px"
)
})
})
22 changes: 21 additions & 1 deletion packages/primitives-core/src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,25 @@ function convertStyles(str: string) {

parsedString.forEach(convertPropertyValue, stylePairs)

return transform(stylePairs)
try {
return transform(stylePairs)
} catch (error) {
const msg = error.message

if (msg.includes('Failed to parse declaration')) {
const values = msg
.replace('Failed to parse declaration ', '')
.replace(/"/g, '')
.trim()
.split(':')

const errorMsg = `'${
values[0]
}' shorthand property requires units for example - ${
values[0]
}: 20px or ${values[0]}: 10px 20px 40px 50px`

console.error(errorMsg)
}
}
}

0 comments on commit ae90f00

Please sign in to comment.