Skip to content

Commit 0b96eaf

Browse files
authored
Merge pull request #386 from jpudysz/feature/ifelse
feat: support detecting dependencies in multiple ifelse statements
2 parents 2fb0b06 + 5b6522b commit 0b96eaf

File tree

3 files changed

+129
-23
lines changed

3 files changed

+129
-23
lines changed

plugin/__tests__/dependencies.spec.js

+95
Original file line numberDiff line numberDiff line change
@@ -606,5 +606,100 @@ pluginTester({
606606
)
607607
`
608608
},
609+
{
610+
title: 'Should correctly detect dependencies from if else statements',
611+
code: `
612+
import { View, Text } from 'react-native'
613+
import { StyleSheet } from 'react-native-unistyles'
614+
615+
export const Example = () => {
616+
return (
617+
<View style={styles.container(5)}>
618+
<Text>Hello world</Text>
619+
</View>
620+
)
621+
}
622+
623+
const styles = StyleSheet.create((theme, rt) => ({
624+
container: someRandomInt => {
625+
if (someRandomInt === 5) {
626+
return {
627+
backgroundColor: theme.colors.background
628+
}
629+
}
630+
631+
if (someRandomInt === 10) {
632+
return {
633+
backgroundColor: theme.colors.barbie,
634+
paddingBottom: rt.insets.bottom
635+
}
636+
}
637+
638+
if (someRandomInt === 15) {
639+
return {
640+
fontSize: rt.fontScale * 10
641+
}
642+
} else {
643+
return {
644+
backgroundColor: theme.colors.blood
645+
}
646+
}
647+
}
648+
}))
649+
`,
650+
output: `
651+
import { UnistylesShadowRegistry } from 'react-native-unistyles'
652+
import { View, Text } from 'react-native'
653+
import { StyleSheet } from 'react-native-unistyles'
654+
655+
export const Example = () => {
656+
return (
657+
<View
658+
style={[styles.container(5)]}
659+
ref={ref => {
660+
UnistylesShadowRegistry.add(ref, [styles.container], undefined, [[5]])
661+
return () => UnistylesShadowRegistry.remove(ref)
662+
}}
663+
>
664+
<Text>Hello world</Text>
665+
</View>
666+
)
667+
}
668+
669+
const styles = StyleSheet.create(
670+
(theme, rt) => ({
671+
container: someRandomInt => {
672+
if (someRandomInt === 5) {
673+
return {
674+
backgroundColor: theme.colors.background,
675+
uni__dependencies: [0]
676+
}
677+
}
678+
679+
if (someRandomInt === 10) {
680+
return {
681+
backgroundColor: theme.colors.barbie,
682+
paddingBottom: rt.insets.bottom,
683+
uni__dependencies: [0, 9]
684+
}
685+
}
686+
687+
if (someRandomInt === 15) {
688+
return {
689+
fontSize: rt.fontScale * 10,
690+
uni__dependencies: [11]
691+
}
692+
} else {
693+
return {
694+
backgroundColor: theme.colors.blood,
695+
uni__dependencies: [0]
696+
}
697+
}
698+
}
699+
}),
700+
664955283
701+
)
702+
`
703+
},
609704
]
610705
})

plugin/index.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const addUnistylesImport = require('./import')
22
const { getStyleMetadata, getStyleAttribute, styleAttributeToArray, handlePressable } = require('./style')
33
const { getRefProp, addRef, overrideRef, hasStringRef } = require('./ref')
4-
const { isUnistylesStyleSheet, analyzeDependencies, addStyleSheetTag, getUnistyle } = require('./stylesheet')
4+
const { isUnistylesStyleSheet, analyzeDependencies, addStyleSheetTag, getUnistyles } = require('./stylesheet')
55
const { isUsingVariants, extractVariants } = require('./variants')
66

77
const reactNativeComponentNames = [
@@ -174,13 +174,11 @@ module.exports = function ({ types: t }) {
174174
if (t.isObjectExpression(arg)) {
175175
arg.properties.forEach(property => {
176176
if (t.isObjectProperty(property)) {
177-
const propertyValue = getUnistyle(t, property)
177+
const propertyValues = getUnistyles(t, property)
178178

179-
if (!propertyValue) {
180-
return
181-
}
182-
183-
analyzeDependencies(t, state, property.key.name, propertyValue)
179+
propertyValues.forEach(propertyValue => {
180+
analyzeDependencies(t, state, property.key.name, propertyValue)
181+
})
184182
}
185183
})
186184
}
@@ -204,13 +202,11 @@ module.exports = function ({ types: t }) {
204202
if (t.isObjectExpression(body)) {
205203
body.properties.forEach(property => {
206204
if (t.isObjectProperty(property)) {
207-
const propertyValue = getUnistyle(t, property)
208-
209-
if (!propertyValue) {
210-
return
211-
}
205+
const propertyValues = getUnistyles(t, property)
212206

213-
analyzeDependencies(t, state, property.key.name, propertyValue, themeLocalName, miniRuntimeLocalName)
207+
propertyValues.forEach(propertyValue => {
208+
analyzeDependencies(t, state, property.key.name, propertyValue, themeLocalName, miniRuntimeLocalName)
209+
})
214210
}
215211
})
216212
}

plugin/stylesheet.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -168,30 +168,45 @@ function analyzeDependencies(t, state, name, unistyleObj, themeName, rtName) {
168168
}
169169
}
170170

171-
function getUnistyle(t, property) {
171+
function getUnistyles(t, property) {
172172
const propertyValue = t.isArrowFunctionExpression(property.value)
173173
? property.value.body
174174
: property.value
175175

176176
if (t.isObjectExpression(propertyValue)) {
177-
return propertyValue
177+
return [propertyValue]
178178
}
179179

180180
if (t.isBlockStatement(propertyValue)) {
181-
const returnStatement = propertyValue.body
182-
.find(value => t.isReturnStatement(value))
183-
184-
return returnStatement
185-
? returnStatement.argument
186-
: null
181+
// here we might have single return statement
182+
// or if-else statements with return statements
183+
return propertyValue.body
184+
.flatMap(value => {
185+
if (t.isReturnStatement(value)) {
186+
return [value]
187+
}
188+
189+
if (!t.isIfStatement(value)) {
190+
return []
191+
}
192+
193+
return [value.consequent, value.alternate]
194+
.filter(Boolean)
195+
.flatMap(value => {
196+
if (t.isBlockStatement(value)) {
197+
return value.body.filter(t.isReturnStatement)
198+
}
199+
})
200+
})
201+
.map(value => value.argument)
187202
}
188203

189-
return null
204+
return []
190205
}
191206

192207
module.exports = {
193208
isUnistylesStyleSheet,
194209
analyzeDependencies,
195210
addStyleSheetTag,
196-
getUnistyle
211+
getUnistyles
197212
}

0 commit comments

Comments
 (0)