Skip to content

Commit 707f23e

Browse files
committed
Preserve interpolation setup order
1 parent dd83f6a commit 707f23e

5 files changed

Lines changed: 94 additions & 4 deletions

File tree

packages/babel-plugin-rn-stylename-inline/__tests__/__snapshots__/index.spec.js.snap

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,10 @@ exports[`@cssxjs/babel-plugin-rn-stylename-inline Local css interpolation: Local
942942
import React from 'react'
943943
import { css } from 'cssxjs'
944944
import { View } from 'react-native'
945+
import { useThemeColor } from './theme'
945946
946-
export default function Card ({ color, pad }) {
947+
export default function Card ({ pad }) {
948+
const color = useThemeColor('primary')
947949
return <View styleName='root' />
948950
949951
css\`
@@ -959,6 +961,7 @@ export default function Card ({ color, pad }) {
959961
import React from "react";
960962
import { css } from "cssxjs";
961963
import { View } from "react-native";
964+
import { useThemeColor } from "./theme";
962965
const _localCssInstance = {
963966
version: 1,
964967
id: "cssx_fjh55d",
@@ -1007,7 +1010,8 @@ const _localCssInstance = {
10071010
diagnostics: [],
10081011
__hash__: -1763352586,
10091012
};
1010-
export default function Card({ color, pad }) {
1013+
export default function Card({ pad }) {
1014+
const color = useThemeColor("primary");
10111015
const __CSS_LOCAL__ = {
10121016
sheet: _localCssInstance,
10131017
values: [color, pad],

packages/babel-plugin-rn-stylename-inline/__tests__/index.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ pluginTester({
202202
import React from 'react'
203203
import { css } from 'cssxjs'
204204
import { View } from 'react-native'
205+
import { useThemeColor } from './theme'
205206
206-
export default function Card ({ color, pad }) {
207+
export default function Card ({ pad }) {
208+
const color = useThemeColor('primary')
207209
return <View styleName='root' />
208210
209211
css\`

packages/babel-plugin-rn-stylename-inline/index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const getVisitor = ({ $program, usedCompilers }) => ({
6969

7070
// 2. reassign this unique identifier or local dynamic layer to a constant LOCAL_NAME
7171
// in the scope of current function
72-
$function.get('body').unshiftContainer('body', buildConst({
72+
insertLocalCss($function, $this, buildConst({
7373
variable: t.identifier(LOCAL_NAME),
7474
value: localValue
7575
}))
@@ -105,6 +105,36 @@ function insertAfterImports ($program, expressionStatement) {
105105
}
106106
}
107107

108+
function insertLocalCss ($function, $template, statement) {
109+
const $body = $function.get('body')
110+
if (!$body.isBlockStatement()) {
111+
$body.replaceWith(t.blockStatement([
112+
t.returnStatement($body.node)
113+
]))
114+
}
115+
116+
const $statement = $template.getStatementParent()
117+
const $functionBody = $function.get('body')
118+
119+
if ($statement?.parentPath === $functionBody) {
120+
// Local style templates usually live after the JSX return. Execute the
121+
// generated layer before that return, but after user setup code/hooks.
122+
const $target = findPreviousReturn($statement) || $statement
123+
$target.insertBefore(statement)
124+
return
125+
}
126+
127+
$functionBody.unshiftContainer('body', statement)
128+
}
129+
130+
function findPreviousReturn ($statement) {
131+
let $current = $statement.getPrevSibling()
132+
while ($current?.node) {
133+
if ($current.isReturnStatement()) return $current
134+
$current = $current.getPrevSibling()
135+
}
136+
}
137+
108138
function shouldProcess ($template, usedCompilers) {
109139
if (!$template.get('tag').isIdentifier()) return
110140
if (!usedCompilers.has($template.node.tag.name)) return

packages/babel-plugin-rn-stylename-to-style/__tests__/__snapshots__/index.spec.js.snap

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,47 @@ export const Test = (_props) => {
664664
};
665665
666666
667+
`;
668+
669+
exports[`@startupjs/babel-plugin-rn-stylename-to-style Local css interpolation after hook: Local css interpolation after hook 1`] = `
670+
671+
import { useThemeColor } from './theme'
672+
import { View } from 'react-native'
673+
674+
function Card ({ pad }) {
675+
const color = useThemeColor('primary')
676+
const __CSS_LOCAL__ = {
677+
sheet: _localCssInstance,
678+
values: [color, pad]
679+
}
680+
return <View styleName='root' />
681+
}
682+
683+
↓ ↓ ↓ ↓ ↓ ↓
684+
685+
import { useThemeColor } from "./theme";
686+
import { View } from "react-native";
687+
import {
688+
runtime as _runtime,
689+
useCssxLayer as _useCssxLayer,
690+
} from "cssxjs/runtime";
691+
const _cssx = _runtime;
692+
function Card({ pad }) {
693+
const color = useThemeColor("primary");
694+
const __CSS_LOCAL__ = {
695+
sheet: _localCssInstance,
696+
values: [color, pad],
697+
};
698+
const _local = _useCssxLayer(
699+
typeof __CSS_LOCAL__ !== "undefined" && __CSS_LOCAL__
700+
);
701+
const _global = _useCssxLayer(
702+
typeof __CSS_GLOBAL__ !== "undefined" && __CSS_GLOBAL__
703+
);
704+
return <View {..._cssx("root", {}, _global, _local, {})} />;
705+
}
706+
707+
667708
`;
668709
669710
exports[`@startupjs/babel-plugin-rn-stylename-to-style No styles file: No styles file 1`] = `

packages/babel-plugin-rn-stylename-to-style/__tests__/index.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ pluginTester({
119119
)
120120
}
121121
`,
122+
'Local css interpolation after hook': /* js */`
123+
import { useThemeColor } from './theme'
124+
import { View } from 'react-native'
125+
126+
function Card ({ pad }) {
127+
const color = useThemeColor('primary')
128+
const __CSS_LOCAL__ = {
129+
sheet: _localCssInstance,
130+
values: [color, pad]
131+
}
132+
return <View styleName='root' />
133+
}
134+
`,
122135
'Puts compiled attribute to the end of attributes list': /* js */`
123136
import './index.styl'
124137
function Test ({ style, active, submit, disabled }) {

0 commit comments

Comments
 (0)