Skip to content

Commit c914764

Browse files
committed
fix: preserve floating-point numbers in default unit (or unitless)
1 parent 7bf40fc commit c914764

File tree

4 files changed

+94
-52
lines changed

4 files changed

+94
-52
lines changed

src/convertCssToJss.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as recast from 'recast'
1212
import camelCase from 'lodash/camelCase'
1313
import upperFirst from 'lodash/upperFirst'
1414
import defaultUnits from './defaultUnits'
15+
import unitlessProperties from './unitlessProperties'
1516
import {
1617
LiteralKind,
1718
IdentifierKind,
@@ -330,6 +331,8 @@ function convertDeclValue(value: string): string {
330331
return JSON.parse(value)
331332
}
332333

334+
const numberPattern = '\\d+(\\.\\d+)?|\\.\\d+'
335+
333336
function convertDecl(decl: postcss.Declaration): ObjectProperty {
334337
const key = convertProp(decl.prop)
335338
const declValue = convertDeclValue(decl.value)
@@ -339,9 +342,16 @@ function convertDecl(decl: postcss.Declaration): ObjectProperty {
339342
} else {
340343
const defaultUnit = defaultUnits[decl.prop]
341344
value = j.stringLiteral(declValue)
342-
if (defaultUnit != null) {
343-
const match = new RegExp(`^(\\d+)${defaultUnit}$`).exec(declValue)
345+
if (defaultUnit) {
346+
const match = new RegExp(`^(${numberPattern})${defaultUnit}$`).exec(
347+
declValue
348+
)
344349
if (match) value = j.numericLiteral(parseFloat(match[1]))
350+
} else if (
351+
unitlessProperties.has(decl.prop) &&
352+
new RegExp(`^(${numberPattern})$`).exec(declValue)
353+
) {
354+
value = j.numericLiteral(parseFloat(declValue))
345355
}
346356
}
347357
if (decl.important) {

src/defaultUnits.ts

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
const px = 'px'
22
const ms = 'ms'
33
const percent = '%'
4-
const unitless = ''
54

65
// copped from jss-plugin-default-unit
76
export default {
87
// Animation properties
98
'animation-delay': ms,
109
'animation-duration': ms,
11-
'animation-iteration-count': unitless,
1210

1311
// Background properties
1412
'background-position': px,
@@ -22,9 +20,6 @@ export default {
2220
'border-bottom-left-radius': px,
2321
'border-bottom-right-radius': px,
2422
'border-bottom-width': px,
25-
'border-image-outset': unitless,
26-
'border-image-slice': unitless,
27-
'border-image-width': unitless,
2823
'border-left': px,
2924
'border-left-width': px,
3025
'border-radius': px,
@@ -54,10 +49,6 @@ export default {
5449
'mask-position-x': px,
5550
'mask-position-y': px,
5651
'mask-size': px,
57-
'mask-border-outset': unitless,
58-
'mask-border-width': unitless,
59-
'mask-box-outset': unitless,
60-
'mask-box-width': unitless,
6152

6253
// Width and height properties
6354
height: px,
@@ -73,14 +64,6 @@ export default {
7364
top: px,
7465
right: px,
7566

76-
// Flexbox properties
77-
flex: unitless,
78-
'flex-grow': unitless,
79-
'flex-positive': unitless,
80-
'flex-shrink': unitless,
81-
'flex-negative': unitless,
82-
order: unitless,
83-
8467
// Shadow properties
8568
'box-shadow': px,
8669
'text-shadow': px,
@@ -90,15 +73,11 @@ export default {
9073
'column-rule': px,
9174
'column-rule-width': px,
9275
'column-width': px,
93-
'column-count': unitless,
9476

9577
// Font and text properties
9678
'font-size': px,
9779
'font-size-delta': px,
98-
'font-size-adjust': unitless,
99-
'font-weight': unitless,
10080
'letter-spacing': px,
101-
'line-height': unitless,
10281
'text-indent': px,
10382
'text-stroke': px,
10483
'text-stroke-width': px,
@@ -133,24 +112,8 @@ export default {
133112
'flex-basis': px,
134113

135114
// Some random properties
136-
'counter-increment': unitless,
137-
'counter-reset': unitless,
138-
opacity: unitless,
139-
orphans: unitless,
140-
'nav-index': unitless,
141115
'shape-margin': px,
142116
size: px,
143-
'tab-size': unitless,
144-
widows: unitless,
145-
'z-index': unitless,
146-
zoom: unitless,
147-
148-
// CSS2 Screen reader
149-
'pitch-range': unitless,
150-
richness: unitless,
151-
'speech-rate': unitless,
152-
stress: unitless,
153-
volume: unitless,
154117

155118
// Grid properties
156119
grid: px,
@@ -161,19 +124,6 @@ export default {
161124
'grid-template-columns': px,
162125
'grid-auto-rows': px,
163126
'grid-auto-columns': px,
164-
'grid-row': unitless,
165-
'grid-column': unitless,
166-
167-
// SVG properties
168-
'fill-opacity': unitless,
169-
'flood-opacity': unitless,
170-
'stop-opacity': unitless,
171-
'stroke-dasharray': unitless,
172-
'stroke-dashoffset': unitless,
173-
'stroke-miterlimit': unitless,
174-
'stroke-opacity': unitless,
175-
'stroke-width': unitless,
176-
'shape-image-threshold': unitless,
177127

178128
// Not existing properties.
179129
// Used to avoid issues with jss-plugin-expand integration.

src/unitlessProperties.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// copped from jss-plugin-default-unit
2+
export default new Set([
3+
// Animation properties
4+
'animation-iteration-count',
5+
6+
// Border Properties
7+
'border-image-outset',
8+
'border-image-slice',
9+
'border-image-width',
10+
11+
// Mask properties
12+
'mask-border-outset',
13+
'mask-border-width',
14+
'mask-box-outset',
15+
'mask-box-width',
16+
17+
// Flexbox properties
18+
'flex',
19+
'flex-grow',
20+
'flex-positive',
21+
'flex-shrink',
22+
'flex-negative',
23+
'order',
24+
25+
// Column properties
26+
'column-count',
27+
28+
// Font and text properties
29+
'font-size-adjust',
30+
'font-weight',
31+
'line-height',
32+
33+
// Some random properties
34+
'counter-increment',
35+
'counter-reset',
36+
'opacity',
37+
'orphans',
38+
'nav-index',
39+
'tab-size',
40+
'widows',
41+
'z-index',
42+
'zoom',
43+
44+
// CSS2 Screen reader
45+
'pitch-range',
46+
'richness',
47+
'speech-rate',
48+
'stress',
49+
'volume',
50+
51+
// Grid properties
52+
'grid-row',
53+
'grid-column',
54+
55+
// SVG properties
56+
'fill-opacity',
57+
'flood-opacity',
58+
'stop-opacity',
59+
'stroke-dasharray',
60+
'stroke-dashoffset',
61+
'stroke-miterlimit',
62+
'stroke-opacity',
63+
'stroke-width',
64+
'shape-image-threshold',
65+
]) as Set<string>

test/css-to-jss/unitlessFloat.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const input = `
2+
const styles = {
3+
/* selectionStart */
4+
.foo {
5+
font-size-adjust: 2.35;
6+
}
7+
/* selectionEnd */
8+
}
9+
`
10+
11+
export const expected = `
12+
const styles = {
13+
foo: {
14+
fontSizeAdjust: 2.35,
15+
}
16+
}
17+
`

0 commit comments

Comments
 (0)