Skip to content

Commit 3f42be3

Browse files
authored
Merge pull request #387 from jpudysz/feature/arrays
feat: correctly detect dependencies in arrays
2 parents 0b96eaf + c28dd24 commit 3f42be3

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

plugin/__tests__/dependencies.spec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,5 +701,62 @@ pluginTester({
701701
)
702702
`
703703
},
704+
{
705+
title: 'Should correctly detect dependency in square brackets',
706+
code: `
707+
import { View, Text } from 'react-native'
708+
import { StyleSheet } from 'react-native-unistyles'
709+
710+
export const Example = () => {
711+
return (
712+
<View style={styles.container}>
713+
<Text>Hello world</Text>
714+
</View>
715+
)
716+
}
717+
718+
const styles = StyleSheet.create((theme, rt) => ({
719+
container: {
720+
backgroundColor: theme.palette.purple[500]
721+
},
722+
container2: {
723+
paddingBottom: theme.spacing[rt.breakpoint]
724+
}
725+
}))
726+
`,
727+
output: `
728+
import { UnistylesShadowRegistry } from 'react-native-unistyles'
729+
import { View, Text } from 'react-native'
730+
import { StyleSheet } from 'react-native-unistyles'
731+
732+
export const Example = () => {
733+
return (
734+
<View
735+
style={[styles.container]}
736+
ref={ref => {
737+
UnistylesShadowRegistry.add(ref, [styles.container], undefined, [[]])
738+
return () => UnistylesShadowRegistry.remove(ref)
739+
}}
740+
>
741+
<Text>Hello world</Text>
742+
</View>
743+
)
744+
}
745+
746+
const styles = StyleSheet.create(
747+
(theme, rt) => ({
748+
container: {
749+
backgroundColor: theme.palette.purple[500],
750+
uni__dependencies: [0]
751+
},
752+
container2: {
753+
paddingBottom: theme.spacing[rt.breakpoint],
754+
uni__dependencies: [0, 3]
755+
}
756+
}),
757+
664955283
758+
)
759+
`
760+
},
704761
]
705762
})

plugin/common.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
function getIdentifierNameFromExpression(t, memberExpression) {
22
if (t.isMemberExpression(memberExpression)) {
33
if (memberExpression.computed) {
4-
return getIdentifierNameFromExpression(t, memberExpression.property).flat()
4+
return [
5+
getIdentifierNameFromExpression(t, memberExpression.property),
6+
getIdentifierNameFromExpression(t, memberExpression.object)
7+
].flat()
58
}
69

710
const object = memberExpression.object
@@ -15,11 +18,6 @@ function getIdentifierNameFromExpression(t, memberExpression) {
1518
if (t.isMemberExpression(object)) {
1619
return getIdentifierNameFromExpression(t, object).flat()
1720
}
18-
19-
// If the object is a computed property, it may also be an Identifier
20-
if (t.isMemberExpression(object) && t.isIdentifier(object.property)) {
21-
return [object.object.name]
22-
}
2321
}
2422

2523
if (t.isBinaryExpression(memberExpression)) {

0 commit comments

Comments
 (0)