fix(inject-testid): skip functions with 'widget'/'worklet' directives#127
Open
joernroeder wants to merge 1 commit into
Open
fix(inject-testid): skip functions with 'widget'/'worklet' directives#127joernroeder wants to merge 1 commit into
joernroeder wants to merge 1 commit into
Conversation
The testID/displayName injector assigns `Component.displayName = "..."` to PascalCase functions that return JSX. When another Babel plugin rewrites such a function into a non-object — e.g. expo-widgets' `'widget'` directive serializes the layout function into a string literal — the injected assignment runs against a string at module-eval time and throws: TypeError: Cannot create property 'displayName' on string The throw aborts module initialization and can cascade into unrelated downstream errors. Tag the component scope when the function body carries a `'widget'` or `'worklet'` directive, and skip both displayName and testID injection for those functions. Applied to both babel-plugin variants and the transformer (`injectTestIds`), with regression tests. Fixes ohah#126 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
babel-plugin-inject-testidinjectsComponent.displayName = "Component"onto every PascalCase function that returns JSX. When another Babel plugin in the pipeline rewrites such a function into a non-object, that assignment throws at runtime.The concrete trigger is
expo-widgets: its'widget'directive (viababel-preset-expo) serializes the layout function into a string literal. The injected assignment then runs against a string at module-eval time:Because it fires during module initialization, it aborts the module and can cascade into confusing downstream errors (in the original report, the app root failed with
Cannot read property 'ErrorBoundary' of undefined).Fix
Tag the component scope when the function body carries a
'widget'or'worklet'directive, and skip bothdisplayNameandtestIDinjection for those functions (their bodies are serialized/relocated, so injectedtestIDs wouldn't behave as expected anyway).Applied consistently to all three implementations:
babel-plugin-inject-testid.cjs(runtime artifact)src/babel-plugin-inject-testid.ts(babel-plugin source)src/babel/inject-testid.ts(injectTestIdstransformer)Tests
Added regression tests to
src/__tests__/babel-inject-testid.test.ts:'widget'directive function → notestID, nodisplayName'worklet'directive function → notestID, nodisplayNamebun test src/__tests__/babel-inject-testid.test.ts→ 18 pass.oxlint,oxfmt --check, andtsc --noEmitall clean.Fixes #126