Skip to content

Commit 0440496

Browse files
committed
Update static2dynamic code
1 parent d9887a5 commit 0440496

2 files changed

Lines changed: 344 additions & 123 deletions

File tree

src/__tests__/rehype-static-to-dynamic.test.ts renamed to src/__tests__/remark-static-to-dynamic.test.ts

Lines changed: 147 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import dedent from 'dedent';
22
import assert from 'node:assert';
33
import { describe, test } from 'node:test';
4-
import rehypeStaticToDynamic, {
5-
type RehypeStaticToDynamicMdxEsm as MdxEsm,
4+
import remarkStaticToDynamic, {
5+
type RemarkStaticToDynamicMdxEsm as MdxEsm,
66
} from '../plugins/remark-static-to-dynamic.ts';
77

88
type Root = {
@@ -105,7 +105,7 @@ function getAttributeValue(node: MdxElement, name: string): unknown {
105105
return node.attributes.find((attribute) => attribute.name === name)?.value;
106106
}
107107

108-
describe('rehype-static-to-dynamic', () => {
108+
describe('remark-static-to-dynamic', () => {
109109
test('basic screen transformation', async () => {
110110
const input = dedent /* javascript */ `
111111
import { createNativeStackNavigator } from '@react-navigation/native-stack';
@@ -126,7 +126,7 @@ describe('rehype-static-to-dynamic', () => {
126126
`;
127127

128128
const tree = createTestTree(input);
129-
const plugin = rehypeStaticToDynamic();
129+
const plugin = remarkStaticToDynamic();
130130
await plugin(tree);
131131

132132
const output = extractTransformedCode(tree);
@@ -177,7 +177,7 @@ describe('rehype-static-to-dynamic', () => {
177177
`;
178178

179179
const tree = createTestTree(input);
180-
const plugin = rehypeStaticToDynamic();
180+
const plugin = remarkStaticToDynamic();
181181
await plugin(tree);
182182

183183
const importNode = tree.children.find(isMdxEsmNode);
@@ -217,7 +217,7 @@ describe('rehype-static-to-dynamic', () => {
217217
"import Tabs from '@theme/Tabs'\nimport TabItem from '@theme/TabItem'",
218218
},
219219
]);
220-
const plugin = rehypeStaticToDynamic();
220+
const plugin = remarkStaticToDynamic();
221221
await plugin(tree);
222222

223223
const tabImportNodes = tree.children.filter(
@@ -229,6 +229,126 @@ describe('rehype-static-to-dynamic', () => {
229229
assert.strictEqual(tabImportNodes.length, 1);
230230
});
231231

232+
test('removes createXScreen-only import when unused after conversion', async () => {
233+
const input = dedent /* javascript */ `
234+
import { createStackNavigator } from '@react-navigation/stack';
235+
import { createNativeStackScreen } from '@react-navigation/native-stack';
236+
import { createStaticNavigation } from '@react-navigation/native';
237+
238+
const RootStack = createStackNavigator({
239+
screens: {
240+
Home: createNativeStackScreen({
241+
screen: HomeScreen,
242+
}),
243+
},
244+
});
245+
246+
const Navigation = createStaticNavigation(RootStack);
247+
248+
export default function App() {
249+
return <Navigation />;
250+
}
251+
`;
252+
253+
const tree = createTestTree(input);
254+
const plugin = remarkStaticToDynamic();
255+
await plugin(tree);
256+
257+
const output = extractTransformedCode(tree);
258+
259+
const expected = dedent /* javascript */ `
260+
import { createStackNavigator } from '@react-navigation/stack';
261+
import { NavigationContainer } from '@react-navigation/native';
262+
263+
const Stack = createStackNavigator();
264+
265+
function RootStack() {
266+
return (
267+
<Stack.Navigator>
268+
<Stack.Screen name="Home" component={HomeScreen} />
269+
</Stack.Navigator>
270+
);
271+
}
272+
273+
export default function App() {
274+
return (
275+
<NavigationContainer>
276+
<RootStack />
277+
</NavigationContainer>
278+
);
279+
}
280+
`;
281+
282+
assert.strictEqual(output, expected);
283+
});
284+
285+
test('keeps createXScreen import when still used after conversion', async () => {
286+
const input = dedent /* javascript */ `
287+
import { createNativeStackNavigator, createNativeStackScreen } from '@react-navigation/native-stack';
288+
import { createStaticNavigation } from '@react-navigation/native';
289+
290+
function getStandaloneScreen() {
291+
return createNativeStackScreen({
292+
screen: ProfileScreen,
293+
});
294+
}
295+
296+
const RootStack = createNativeStackNavigator({
297+
screens: {
298+
Home: createNativeStackScreen({
299+
screen: HomeScreen,
300+
}),
301+
},
302+
});
303+
304+
const Navigation = createStaticNavigation(RootStack);
305+
306+
export default function App() {
307+
return <Navigation />;
308+
}
309+
`;
310+
311+
const tree = createTestTree(input);
312+
const plugin = remarkStaticToDynamic();
313+
await plugin(tree);
314+
315+
const output = extractTransformedCode(tree);
316+
317+
const expected = dedent /* javascript */ `
318+
import {
319+
createNativeStackNavigator,
320+
createNativeStackScreen,
321+
} from '@react-navigation/native-stack';
322+
import { NavigationContainer } from '@react-navigation/native';
323+
324+
function getStandaloneScreen() {
325+
return createNativeStackScreen({
326+
screen: ProfileScreen,
327+
});
328+
}
329+
330+
const Stack = createNativeStackNavigator();
331+
332+
function RootStack() {
333+
return (
334+
<Stack.Navigator>
335+
<Stack.Screen name="Home" component={HomeScreen} />
336+
</Stack.Navigator>
337+
);
338+
}
339+
340+
export default function App() {
341+
return (
342+
<NavigationContainer>
343+
<RootStack />
344+
</NavigationContainer>
345+
);
346+
}
347+
`;
348+
349+
assert.strictEqual(output, expected);
350+
});
351+
232352
test('screen with options', async () => {
233353
const input = dedent /* javascript */ `
234354
import { createNativeStackNavigator, createNativeStackScreen } from '@react-navigation/native-stack';
@@ -251,7 +371,7 @@ describe('rehype-static-to-dynamic', () => {
251371
`;
252372

253373
const tree = createTestTree(input);
254-
const plugin = rehypeStaticToDynamic();
374+
const plugin = remarkStaticToDynamic();
255375
await plugin(tree);
256376

257377
const output = extractTransformedCode(tree);
@@ -312,7 +432,7 @@ describe('rehype-static-to-dynamic', () => {
312432
`;
313433

314434
const tree = createTestTree(input);
315-
const plugin = rehypeStaticToDynamic();
435+
const plugin = remarkStaticToDynamic();
316436
await plugin(tree);
317437

318438
const output = extractTransformedCode(tree);
@@ -375,7 +495,7 @@ describe('rehype-static-to-dynamic', () => {
375495
`;
376496

377497
const tree = createTestTree(input);
378-
const plugin = rehypeStaticToDynamic();
498+
const plugin = remarkStaticToDynamic();
379499
await plugin(tree);
380500

381501
const output = extractTransformedCode(tree);
@@ -441,7 +561,7 @@ describe('rehype-static-to-dynamic', () => {
441561
`;
442562

443563
const tree = createTestTree(input);
444-
const plugin = rehypeStaticToDynamic();
564+
const plugin = remarkStaticToDynamic();
445565
await plugin(tree);
446566

447567
const output = extractTransformedCode(tree);
@@ -505,7 +625,7 @@ describe('rehype-static-to-dynamic', () => {
505625
`;
506626

507627
const tree = createTestTree(input);
508-
const plugin = rehypeStaticToDynamic();
628+
const plugin = remarkStaticToDynamic();
509629
await plugin(tree);
510630

511631
const output = extractTransformedCode(tree);
@@ -568,7 +688,7 @@ describe('rehype-static-to-dynamic', () => {
568688
`;
569689

570690
const tree = createTestTree(input);
571-
const plugin = rehypeStaticToDynamic();
691+
const plugin = remarkStaticToDynamic();
572692
await plugin(tree);
573693

574694
const output = extractTransformedCode(tree);
@@ -637,7 +757,7 @@ describe('rehype-static-to-dynamic', () => {
637757
`;
638758

639759
const tree = createTestTree(input);
640-
const plugin = rehypeStaticToDynamic();
760+
const plugin = remarkStaticToDynamic();
641761
await plugin(tree);
642762

643763
const output = extractTransformedCode(tree);
@@ -703,7 +823,7 @@ describe('rehype-static-to-dynamic', () => {
703823
`;
704824

705825
const tree = createTestTree(input);
706-
const plugin = rehypeStaticToDynamic();
826+
const plugin = remarkStaticToDynamic();
707827
await plugin(tree);
708828

709829
const output = extractTransformedCode(tree);
@@ -764,7 +884,7 @@ describe('rehype-static-to-dynamic', () => {
764884
`;
765885

766886
const tree = createTestTree(input);
767-
const plugin = rehypeStaticToDynamic();
887+
const plugin = remarkStaticToDynamic();
768888
await plugin(tree);
769889

770890
const output = extractTransformedCode(tree);
@@ -823,7 +943,7 @@ describe('rehype-static-to-dynamic', () => {
823943
`;
824944

825945
const tree = createTestTree(input);
826-
const plugin = rehypeStaticToDynamic();
946+
const plugin = remarkStaticToDynamic();
827947
await plugin(tree);
828948

829949
const output = extractTransformedCode(tree);
@@ -889,7 +1009,7 @@ describe('rehype-static-to-dynamic', () => {
8891009
`;
8901010

8911011
const tree = createTestTree(input);
892-
const plugin = rehypeStaticToDynamic();
1012+
const plugin = remarkStaticToDynamic();
8931013
await plugin(tree);
8941014

8951015
const output = extractTransformedCode(tree);
@@ -954,7 +1074,7 @@ describe('rehype-static-to-dynamic', () => {
9541074
`;
9551075

9561076
const tree = createTestTree(input);
957-
const plugin = rehypeStaticToDynamic();
1077+
const plugin = remarkStaticToDynamic();
9581078
await plugin(tree);
9591079

9601080
const output = extractTransformedCode(tree);
@@ -1027,7 +1147,7 @@ describe('rehype-static-to-dynamic', () => {
10271147
`;
10281148

10291149
const tree = createTestTree(input);
1030-
const plugin = rehypeStaticToDynamic();
1150+
const plugin = remarkStaticToDynamic();
10311151
await plugin(tree);
10321152

10331153
const output = extractTransformedCode(tree);
@@ -1111,7 +1231,7 @@ describe('rehype-static-to-dynamic', () => {
11111231
`;
11121232

11131233
const tree = createTestTree(input);
1114-
const plugin = rehypeStaticToDynamic();
1234+
const plugin = remarkStaticToDynamic();
11151235
await plugin(tree);
11161236

11171237
const output = extractTransformedCode(tree);
@@ -1193,7 +1313,7 @@ describe('rehype-static-to-dynamic', () => {
11931313
`;
11941314

11951315
const tree = createTestTree(input);
1196-
const plugin = rehypeStaticToDynamic();
1316+
const plugin = remarkStaticToDynamic();
11971317
await plugin(tree);
11981318

11991319
const output = extractTransformedCode(tree);
@@ -1269,7 +1389,7 @@ describe('rehype-static-to-dynamic', () => {
12691389
`;
12701390

12711391
const tree = createTestTree(input);
1272-
const plugin = rehypeStaticToDynamic();
1392+
const plugin = remarkStaticToDynamic();
12731393
await plugin(tree);
12741394

12751395
const output = extractTransformedCode(tree);
@@ -1345,7 +1465,7 @@ describe('rehype-static-to-dynamic', () => {
13451465
`;
13461466

13471467
const tree = createTestTree(input);
1348-
const plugin = rehypeStaticToDynamic();
1468+
const plugin = remarkStaticToDynamic();
13491469
await plugin(tree);
13501470

13511471
const output = extractTransformedCode(tree);
@@ -1405,7 +1525,7 @@ describe('rehype-static-to-dynamic', () => {
14051525
`;
14061526

14071527
const tree = createTestTree(input);
1408-
const plugin = rehypeStaticToDynamic();
1528+
const plugin = remarkStaticToDynamic();
14091529
await plugin(tree);
14101530

14111531
const output = extractTransformedCode(tree);
@@ -1456,7 +1576,7 @@ describe('rehype-static-to-dynamic', () => {
14561576
`;
14571577

14581578
const tree = createTestTree(input);
1459-
const plugin = rehypeStaticToDynamic();
1579+
const plugin = remarkStaticToDynamic();
14601580

14611581
await assert.rejects(
14621582
plugin(tree),
@@ -1510,7 +1630,7 @@ describe('rehype-static-to-dynamic', () => {
15101630
`;
15111631

15121632
const tree = createTestTree(input);
1513-
const plugin = rehypeStaticToDynamic();
1633+
const plugin = remarkStaticToDynamic();
15141634
await plugin(tree);
15151635

15161636
const output = extractTransformedCode(tree);
@@ -1599,7 +1719,7 @@ describe('rehype-static-to-dynamic', () => {
15991719
`;
16001720

16011721
const tree = createTestTree(input);
1602-
const plugin = rehypeStaticToDynamic();
1722+
const plugin = remarkStaticToDynamic();
16031723
await plugin(tree);
16041724

16051725
const output = extractTransformedCode(tree);

0 commit comments

Comments
 (0)