Skip to content

Commit

Permalink
feat: convert ternary to jsx if else
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz committed Dec 16, 2024
1 parent a3eadc7 commit cad94e7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
54 changes: 30 additions & 24 deletions plugin/__tests__/variants.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,16 +505,19 @@ pluginTester({
size: 'small'
}
styles.useVariants(__uni__variants)
return condition ? (
<Variants variants={__uni__variants}>
<Text style={[styles.container]}>Hello world</Text>
</Variants>
) : (
<Variants variants={__uni__variants}>
<Text style={[styles.container]}>Hello world</Text>
</Variants>
)
if (condition) {
return (
<Variants variants={__uni__variants}>
<Text style={[styles.container]}>Hello world</Text>
</Variants>
)
} else {
return (
<Variants variants={__uni__variants}>
<Text style={[styles.container]}>Hello world</Text>
</Variants>
)
}
}
const styles = StyleSheet.create(
Expand Down Expand Up @@ -798,20 +801,23 @@ pluginTester({
error: !!error
}
styles.useVariants(__uni__variants)
return children ? (
<Variants variants={__uni__variants}>
<Text ref={ref} style={[styles.label]} {...props}>
{children}
</Text>
</Variants>
) : (
<Variants variants={__uni__variants}>
<Text ref={ref} style={[styles.label]} {...props}>
{name}
</Text>
</Variants>
)
if (children) {
return (
<Variants variants={__uni__variants}>
<Text ref={ref} style={[styles.label]} {...props}>
{children}
</Text>
</Variants>
)
} else {
return (
<Variants variants={__uni__variants}>
<Text ref={ref} style={[styles.label]} {...props}>
{name}
</Text>
</Variants>
)
}
}
const styles = StyleSheet.create(theme => ({}), 895830154)
Expand Down
9 changes: 7 additions & 2 deletions plugin/variants.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ function addJSXVariants(t, path) {
const jsxElement = path.node.argument

if (t.isConditionalExpression(jsxElement)) {
jsxElement.alternate = wrapVariants(t, jsxElement.alternate)
jsxElement.consequent = wrapVariants(t, jsxElement.consequent)
const ifStatement = t.ifStatement(
jsxElement.test,
t.blockStatement([t.returnStatement(jsxElement.consequent)]),
t.blockStatement([t.returnStatement(jsxElement.alternate)])
)

path.replaceWith(ifStatement)

return
}
Expand Down

0 comments on commit cad94e7

Please sign in to comment.