fix: handle boolean shorthand in nativeStyleMapping#349
Open
telanflow wants to merge 1 commit into
Open
Conversation
A nativeStyleMapping entry value may be `true` (the same-name shorthand
permitted by the NativeStyleMapping type: `true | DotNotation`), but the
runtime called path.split(".") unconditionally, throwing "true.split is
not a function" once the mapped style was present.
This crashed shipped components that use the shorthand - TextInput
(textAlign) and ImageBackground (backgroundColor) - as soon as a class
like text-right or bg-* was applied; the style was only skipped when
absent (the styleValue === undefined guard), making it look intermittent.
Map `true` onto a prop with the same name, and add a regression test.
Closes nativewind#348
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
nativeStyleMapping()insrc/native/styles/index.tscallspath.split(".")on every mapping value, but a value may be the boolean shorthandtrue— the same-name mapping permitted by theNativeStyleMappingtype (true | DotNotation). When the mapped style is actually present,(true).splitthrowsTypeError: undefined is not a function, crashing render.Fixes #348 (same root cause as #232 / #288).
Affected built-in components
Components that ship the boolean shorthand crash as soon as the mapped style is applied:
components/TextInput.tsx→{ textAlign: true }— triggered bytext-right/text-center/text-leftcomponents/ImageBackground.tsx→{ backgroundColor: true }— triggered by anybg-*(
ActivityIndicator/Buttonuse{ color: "color" }, a string path, so they're unaffected.)It only crashes when the mapped style is present: when absent,
styleValue === undefinedshort-circuits viacontinue, which is why the crash looks intermittent.Fix
Treat
trueas the same-name shorthand (map onto a prop namedkey):Test
Added a regression test in
src/__tests__/native/components.test.tsx(using thetarget: "style"+{ backgroundColor: true }shorthand). It fails onmainwithTypeError: path.split is not a functionand passes with this change.yarn test,yarn typecheck, andyarn lintall pass.