Skip to content

Commit f48d866

Browse files
committed
Warn on extraeous units
1 parent 2567fde commit f48d866

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

Diff for: src/__tests__/index.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import transformCss, { getStylesForProperty } from '..'
22

33
it('transforms numbers', () => {
4-
expect(transformCss([['zIndex', '0']])).toEqual({ zIndex: 0 })
4+
expect(transformCss([['z-index', '0']])).toEqual({ zIndex: 0 })
55
})
66

77
it('warns if missing units on unspecialized transform', () => {
@@ -28,6 +28,30 @@ it('does not warn for unitless 0 length on unspecialized transform', () => {
2828
consoleSpy.mockRestore()
2929
})
3030

31+
it('warns if adding etraneous units on unspecialized transform', () => {
32+
const consoleSpy = jest
33+
.spyOn(global.console, 'warn')
34+
.mockImplementation(() => {
35+
// Silence the warning from the test output
36+
})
37+
38+
transformCss([['opacity', '1px']])
39+
expect(consoleSpy).toHaveBeenCalledWith(
40+
'Expected style "opacity: 1px" to be unitless'
41+
)
42+
43+
consoleSpy.mockRestore()
44+
})
45+
46+
it('does not warn for unitless 0 length on unitless transform', () => {
47+
const consoleSpy = jest.spyOn(global.console, 'warn')
48+
49+
transformCss([['opacity', '0']])
50+
expect(consoleSpy).not.toHaveBeenCalled()
51+
52+
consoleSpy.mockRestore()
53+
})
54+
3155
it('allows pixels in unspecialized transform', () => {
3256
expect(transformCss([['top', '0px']])).toEqual({ top: 0 })
3357
})

Diff for: src/index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ const undefinedRe = /^undefined$/i
1515
// Undocumented export
1616
export const transformRawValue = (propName, value) => {
1717
if (process.env.NODE_ENV !== 'production') {
18-
if (
19-
!devPropertiesWithoutUnitsRegExp.test(propName) &&
20-
numberOnlyRe.test(value)
21-
) {
18+
const needsUnit = !devPropertiesWithoutUnitsRegExp.test(propName)
19+
const isNumberWithoutUnit = numberOnlyRe.test(value)
20+
if (needsUnit && isNumberWithoutUnit) {
2221
// eslint-disable-next-line no-console
2322
console.warn(`Expected style "${propName}: ${value}" to contain units`)
2423
}
24+
if (!needsUnit && value !== '0' && !isNumberWithoutUnit) {
25+
// eslint-disable-next-line no-console
26+
console.warn(`Expected style "${propName}: ${value}" to be unitless`)
27+
}
2528
}
2629

2730
const numberMatch = value.match(numberOrLengthRe)

0 commit comments

Comments
 (0)