Skip to content

Commit d32a535

Browse files
committed
chore: allow and filter multiple expressions
1 parent 8d49b77 commit d32a535

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

cxx/core/RNStyle.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ jsi::Function createAddVariantsProxyFunction(jsi::Runtime& rt, std::shared_ptr<S
2323
jsi::Object toRNStyle(jsi::Runtime& rt, std::shared_ptr<StyleSheet> stylesheet, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime, Variants&& variants) {
2424
jsi::Object rnStyle = jsi::Object(rt);
2525

26-
rnStyle.setProperty(rt, helpers::UNISTYLES_ID.c_str(), jsi::Value(stylesheet->tag));
26+
helpers::defineHiddenProperty(rt, rnStyle, helpers::UNISTYLES_ID.c_str(), jsi::Value(stylesheet->tag));
27+
helpers::defineHiddenProperty(rt, rnStyle, helpers::STYLE_VARIANTS.c_str(), helpers::variantsToValue(rt, variants));
28+
2729
rnStyle.setProperty(rt, helpers::ADD_VARIANTS_FN.c_str(), createAddVariantsProxyFunction(rt, stylesheet, unistylesRuntime));
28-
rnStyle.setProperty(rt, helpers::STYLE_VARIANTS.c_str(), helpers::variantsToValue(rt, variants));
2930

3031
for (auto& pair: stylesheet->unistyles) {
3132
auto [propertyName, unistyle] = pair;

plugin/variants.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
function extractVariants(t, path, state) {
2-
const maybeVariants = path.node.body.find(node => (
2+
const maybeVariants = path.node.body.filter(node => (
33
t.isExpressionStatement(node) &&
44
t.isCallExpression(node.expression) &&
55
t.isMemberExpression(node.expression.callee)
66
))
77

8-
if (!maybeVariants) {
8+
if (maybeVariants.length === 0) {
99
return
1010
}
1111

12-
const calleeName = maybeVariants.expression.callee.object.name
13-
const isVariant = (
14-
t.isIdentifier(maybeVariants.expression.callee.object, { name: calleeName }) &&
15-
t.isIdentifier(maybeVariants.expression.callee.property, { name: 'useVariants' }) &&
16-
t.isObjectExpression(maybeVariants.expression.arguments[0])
17-
)
12+
const targetVariant = maybeVariants.find(variant => {
13+
const calleeName = variant.expression.callee.object.name
14+
15+
return (
16+
t.isIdentifier(variant.expression.callee.object, { name: calleeName }) &&
17+
t.isIdentifier(variant.expression.callee.property, { name: 'useVariants' }) &&
18+
t.isObjectExpression(variant.expression.arguments[0])
19+
)
20+
})
1821

19-
if (!isVariant) {
22+
if (!targetVariant) {
2023
return
2124
}
2225

23-
const node = maybeVariants.expression
26+
const calleeName = targetVariant.expression.callee.object.name
27+
const node = targetVariant.expression
2428
const newUniqueName = path.scope.generateUidIdentifier(calleeName)
2529

2630
// Create shadow declaration eg. const _styles = styles
@@ -39,7 +43,7 @@ function extractVariants(t, path, state) {
3943

4044
// Find the current node's index, we will move everything after to new block
4145
const pathIndex = path.node.body
42-
.findIndex(bodyPath => bodyPath === maybeVariants)
46+
.findIndex(bodyPath => bodyPath === targetVariant)
4347
const rest = path.node.body.slice(pathIndex + 1)
4448

4549
// move rest to new block (scope)

0 commit comments

Comments
 (0)