Skip to content

Commit 6841f94

Browse files
committed
Fix Babel plugin crash when handling unresolved component names
1 parent 5300009 commit 6841f94

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

packages/react-native-babel-plugin/src/actions/rum/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ function jsxChildToRuntimeCall(
237237
: t.memberExpression(obj, t.identifier(prop)),
238238
(null as unknown) as Babel.types.Expression
239239
);
240+
} else if (elementName) {
241+
nameNode = t.identifier(elementName);
240242
} else {
241-
nameNode = t.identifier(elementName || 'unknown');
243+
nameNode = addNamed(programPath, 'Fragment', 'react/jsx-runtime');
242244
}
243245

244246
// Convert props to an object

packages/react-native-babel-plugin/test/plugin.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,4 +1643,39 @@ describe('Babel plugin: wrap interaction handlers for RUM ( with memoization )',
16431643
}"
16441644
`);
16451645
});
1646+
1647+
it('should use Fragment instead of unknown for unresolved component names in getContent', () => {
1648+
const options: Partial<PluginOptions> = {
1649+
components: {
1650+
useContent: true,
1651+
useNamePrefix: true,
1652+
tracked: [
1653+
{
1654+
name: 'Card',
1655+
handlers: [{ event: 'onPress', action: 'TAP' }]
1656+
}
1657+
]
1658+
}
1659+
};
1660+
1661+
const input = `
1662+
import { View, Text } from 'react-native';
1663+
function Card({ children, onPress }: any) {
1664+
return <View onPress={onPress}>{children}</View>;
1665+
}
1666+
Card.Title = ({ children }: any) => <Text>{children}</Text>;
1667+
1668+
function Screen() {
1669+
return (
1670+
<Card onPress={() => {}}>
1671+
<Card.Title>Welcome</Card.Title>
1672+
</Card>
1673+
);
1674+
}
1675+
`;
1676+
1677+
const output = transformCode(input, options);
1678+
expect(output).not.toContain('(unknown,');
1679+
expect(output).toContain('_Fragment');
1680+
});
16461681
});

0 commit comments

Comments
 (0)