Skip to content

Commit da9bc50

Browse files
committed
fix: missing class property insert position
1 parent d72fb78 commit da9bc50

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/convertCssToJss.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {
55
StringLiteral,
66
NumericLiteral,
77
ArrayExpression,
8+
Property,
9+
ObjectMethod,
10+
SpreadProperty,
11+
SpreadElement,
812
} from 'jscodeshift'
913
import j from 'jscodeshift'
1014
import * as postcss from 'postcss'
@@ -228,20 +232,31 @@ export function convertSelectors(root: ObjectExpression): void {
228232
}
229233
}
230234
const addMissingClassProperties = (node: ObjectExpression): void => {
231-
if (node === root) insertMissingClassPropertyBefore = 0
232-
233-
for (const prop of node.properties) {
234-
if (prop.type !== 'ObjectProperty') continue
235+
function process(
236+
prop:
237+
| ObjectProperty
238+
| Property
239+
| ObjectMethod
240+
| SpreadProperty
241+
| SpreadElement
242+
): void {
243+
if (prop.type !== 'ObjectProperty') return
235244
const { value } = prop
236245
const key = getRawKey(prop.key)
237-
if (!key || value.type !== 'ObjectExpression') continue
246+
if (!key || value.type !== 'ObjectExpression') return
238247
const [localSelector] = splitGlobals(key)
239248
if (localSelector) {
240249
addMissingClassPropertiesSelector.processSync(localSelector)
241250
}
242-
243251
addMissingClassProperties(value)
244-
insertMissingClassPropertyBefore++
252+
}
253+
if (node === root) {
254+
for (let i = 0; i < node.properties.length; i++) {
255+
insertMissingClassPropertyBefore = i
256+
process(node.properties[i])
257+
}
258+
} else {
259+
for (const prop of node.properties) process(prop)
245260
}
246261
}
247262
const convertClassReferences = (node: ObjectExpression): void => {

test/css-to-jss/bigExample.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const styles = {
6161
color: 'red',
6262
},
6363
},
64+
baz: {},
6465
foo: {
6566
color: 'green',
6667
'& $barQux, & $glorm:after': {
@@ -101,6 +102,5 @@ const styles = {
101102
},
102103
},
103104
},
104-
baz: {},
105105
}
106106
`

test/css-to-jss/classSelectors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const styles = {
2929

3030
export const expected = `
3131
const styles = {
32+
baz: {},
3233
foo: {
3334
color: 'green',
3435
'& $barQux, & $glorm:after': {
@@ -38,7 +39,6 @@ const styles = {
3839
color: 'blue',
3940
}
4041
},
41-
baz: {},
4242
glorm: {
4343
color: 'green',
4444
},

0 commit comments

Comments
 (0)