Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning for invalid content values in objects #633

Merged
merged 1 commit into from
Apr 21, 2018
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
35 changes: 34 additions & 1 deletion packages/create-emotion/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const processStyleName: (styleName: string) => string = memoize(
(styleName: string) => styleName.replace(hyphenateRegex, '-$&').toLowerCase()
)

export const processStyleValue = (key: string, value: string): string => {
export let processStyleValue = (key: string, value: string): string => {
if (value == null || typeof value === 'boolean') {
return ''
}
Expand All @@ -24,6 +24,39 @@ export const processStyleValue = (key: string, value: string): string => {
return value
}

if (process.env.NODE_ENV !== 'production') {
let contentValuePattern = /(attr|calc|counters?|url)\(/
let contentValues = [
'normal',
'none',
'counter',
'open-quote',
'close-quote',
'no-open-quote',
'no-close-quote',
'initial',
'inherit',
'unset'
]
let oldProcessStyleValue = processStyleValue
processStyleValue = (key: string, value: string) => {
if (key === 'content') {
if (
typeof value !== 'string' ||
(contentValues.indexOf(value) === -1 &&
!contentValuePattern.test(value) &&
(value.charAt(0) !== value.charAt(value.length - 1) ||
(value.charAt(0) !== '"' && value.charAt(0) !== "'")))
) {
console.error(
`You seem to be using a value for 'content' without quotes, try replacing it with \`content: '"${value}"'\``
)
}
}
return oldProcessStyleValue(key, value)
}
}

export type ClassNameArg =
| string
| boolean
Expand Down
45 changes: 45 additions & 0 deletions packages/emotion/test/__snapshots__/warnings.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`does not warn when valid values are passed for the content property 1`] = `
.emotion-0 {
content: normal;
content: none;
content: counter;
content: open-quote;
content: close-quote;
content: no-open-quote;
content: no-close-quote;
content: initial;
content: inherit;
content: "some thing";
content: 'another thing';
content: url("http://www.example.com/test.png");
content: counter(chapter_counter);
content: counters(section_counter,".");
content: attr(value string);
}

<div
className="emotion-0"
/>
`;

exports[`does warn when invalid values are passed for the content property 1`] = `
.emotion-0 {
content: this is not valid;
}

<div
className="emotion-0"
/>
`;

exports[`does warn when invalid values are passed for the content property 2`] = `
.emotion-0 {
content: px;
}

<div
className="emotion-0"
/>
`;
45 changes: 45 additions & 0 deletions packages/emotion/test/warnings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @flow
import { css } from 'emotion'
import * as React from 'react'
import renderer from 'react-test-renderer'

const validValues = [
'normal',
'none',
'counter',
'open-quote',
'close-quote',
'no-open-quote',
'no-close-quote',
'initial',
'inherit',
'"some thing"',
"'another thing'",
'url("http://www.example.com/test.png")',
'counter(chapter_counter)',
'counters(section_counter, ".")',
'attr(value string)'
]

it('does not warn when valid values are passed for the content property', () => {
// $FlowFixMe
console.error = jest.fn()
const cls = css(validValues.map(value => ({ content: value })))
expect(console.error).not.toBeCalled()
expect(renderer.create(<div className={cls} />).toJSON()).toMatchSnapshot()
})

const invalidValues = ['this is not valid', '']

it('does warn when invalid values are passed for the content property', () => {
// $FlowFixMe
console.error = jest.fn()
invalidValues.forEach(value => {
expect(
renderer.create(<div className={css({ content: value })} />).toJSON()
).toMatchSnapshot()
expect(console.error).toBeCalledWith(
`You seem to be using a value for 'content' without quotes, try replacing it with \`content: '"${value}"'\``
)
})
})