Skip to content

Commit 051e927

Browse files
committed
Fix incorrect line highlight for comments
1 parent 0f32cf9 commit 051e927

File tree

3 files changed

+118
-4
lines changed

3 files changed

+118
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ i18n/*
1717
!.yarn/releases
1818
!.yarn/sdks
1919
!.yarn/versions
20+
21+
# AI
22+
.claude/

__tests__/rehype-static-to-dynamic.test.mjs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,117 @@ describe('rehype-static-to-dynamic', () => {
939939
assert.strictEqual(output, expected);
940940
});
941941

942+
test('nested navigator with highlight on screen property', async () => {
943+
const input = dedent /* javascript */ `
944+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
945+
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
946+
import { createStaticNavigation } from '@react-navigation/native';
947+
948+
const SearchStack = createNativeStackNavigator({
949+
screens: {
950+
FruitsList: {
951+
screen: FruitsListScreen,
952+
options: {
953+
title: 'Search',
954+
},
955+
},
956+
},
957+
});
958+
959+
const HomeTabs = createBottomTabNavigator({
960+
screens: {
961+
Home: {
962+
screen: HomeScreen,
963+
options: {
964+
tabBarIcon: {
965+
type: 'sfSymbol',
966+
name: 'house',
967+
},
968+
},
969+
},
970+
Search: {
971+
// highlight-next-line
972+
screen: SearchStack,
973+
options: {
974+
tabBarSystemItem: 'search',
975+
},
976+
},
977+
},
978+
});
979+
980+
const Navigation = createStaticNavigation(HomeTabs);
981+
982+
export default function App() {
983+
return <Navigation />;
984+
}
985+
`;
986+
987+
const tree = createTestTree(input);
988+
const plugin = rehypeStaticToDynamic();
989+
await plugin(tree);
990+
991+
const output = extractTransformedCode(tree);
992+
993+
const expected = dedent /* javascript */ `
994+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
995+
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
996+
import { NavigationContainer } from '@react-navigation/native';
997+
998+
const Stack = createNativeStackNavigator();
999+
1000+
function SearchStack() {
1001+
return (
1002+
<Stack.Navigator>
1003+
<Stack.Screen
1004+
name="FruitsList"
1005+
component={FruitsListScreen}
1006+
options={{
1007+
title: 'Search',
1008+
}}
1009+
/>
1010+
</Stack.Navigator>
1011+
);
1012+
}
1013+
1014+
const Tab = createBottomTabNavigator();
1015+
1016+
function HomeTabs() {
1017+
return (
1018+
<Tab.Navigator>
1019+
<Tab.Screen
1020+
name="Home"
1021+
component={HomeScreen}
1022+
options={{
1023+
tabBarIcon: {
1024+
type: 'sfSymbol',
1025+
name: 'house',
1026+
},
1027+
}}
1028+
/>
1029+
<Tab.Screen
1030+
name="Search"
1031+
// highlight-next-line
1032+
component={SearchStack}
1033+
options={{
1034+
tabBarSystemItem: 'search',
1035+
}}
1036+
/>
1037+
</Tab.Navigator>
1038+
);
1039+
}
1040+
1041+
export default function App() {
1042+
return (
1043+
<NavigationContainer>
1044+
<HomeTabs />
1045+
</NavigationContainer>
1046+
);
1047+
}
1048+
`;
1049+
1050+
assert.strictEqual(output, expected);
1051+
});
1052+
9421053
test('mixed screen definitions', async () => {
9431054
const input = dedent /* javascript */ `
9441055
import { createNativeStackNavigator, createNativeStackScreen } from '@react-navigation/native-stack';

src/plugins/rehype-static-to-dynamic.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,16 +748,16 @@ function attachCommentsToNode(node, comments, isTrailing = false) {
748748
}
749749

750750
/**
751-
* Find a property line within a context (searches nearby lines for context marker)
751+
* Find a property line within a context (searches lines before for context marker)
752752
*/
753753
function findPropertyLine(lines, propMatcher, contextMatcher, searchRange) {
754754
for (let i = 0; i < lines.length; i++) {
755755
if (propMatcher(lines[i])) {
756-
// Check if this is within the right context by searching nearby lines
756+
// Check if this is within the right context by searching lines before only
757+
// We only look backwards because name= comes before component= in the output JSX
757758
const startIdx = Math.max(0, i - searchRange);
758-
const endIdx = Math.min(i + searchRange, lines.length);
759759

760-
for (let j = startIdx; j < endIdx; j++) {
760+
for (let j = startIdx; j <= i; j++) {
761761
if (contextMatcher(lines[j])) {
762762
return i;
763763
}

0 commit comments

Comments
 (0)