-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed invalid prop types error message to be more specific #11308
Merged
Merged
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
2653595
Modified tests and corrected error message. #3
NicBonetto 8de9a20
Fixed syntax issues. #3
NicBonetto 4ce5b49
Modified test. #3
NicBonetto 3d4724e
Prettified. #3
NicBonetto 449b98d
Changed warning message to handle true and false boolean values. #3
NicBonetto bb7978c
Changed test to contain undefined instead of value. #3
NicBonetto 05ecf0a
Simplified branch structure. #3
NicBonetto e18a235
Refactored branching logic. #3
NicBonetto 9fc2b7f
Refactored falsy warning message and tests. #3
NicBonetto ede38f3
Changed condition to attribute name. #3
NicBonetto ae6fcbf
Refactored falsy and truthy warning messages with tests updated. #3
NicBonetto aada64c
Added missing character. #3
NicBonetto de26d9d
Fixed warning message. #3
NicBonetto c7ee43d
Cleared extra whitespace. #3
NicBonetto 2c19ea3
Refactored warning messages to be clear. #3
NicBonetto 37207a0
Prettified. #3
NicBonetto 12db856
Grammar fix
gaearon 69cbdfc
Tweak unrelated warning
gaearon c841d41
Tweak the message for more clarity
gaearon da20477
Add a special message for false event handlers
gaearon 5b994bf
Add missing whitespace
gaearon 425c00b
Revert size changes
gaearon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,15 +180,32 @@ if (__DEV__) { | |
return true; | ||
} | ||
|
||
if (typeof value === 'boolean') { | ||
warning( | ||
DOMProperty.shouldAttributeAcceptBooleanValue(name), | ||
'Received `%s` for non-boolean attribute `%s`. If this is expected, cast ' + | ||
'the value to a string.%s', | ||
value, | ||
name, | ||
getStackAddendum(), | ||
); | ||
if ( | ||
typeof value === 'boolean' && | ||
!DOMProperty.shouldAttributeAcceptBooleanValue(name) | ||
) { | ||
if (value === true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: we already know it's a boolean so can just write |
||
warning( | ||
false, | ||
'Received `%s` for non-boolean attribute `%s`. If this is expected, cast ' + | ||
'the value to a string.%s', | ||
value, | ||
name, | ||
getStackAddendum(), | ||
); | ||
} else { | ||
warning( | ||
false, | ||
'Received `%s` for non-boolean attribute `%s`. If you mean to conditionally ' + | ||
'pass an attribute, use a ternary expression: {`%s` ? value : undefined} ' + | ||
'instead of {`%s` && value}.%s', | ||
value, | ||
name, | ||
name, | ||
name, | ||
getStackAddendum(), | ||
); | ||
} | ||
warnedProperties[name] = true; | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this removed? |
||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure to read the messages you assert in tests. It looks like you were trying to adjust tests to match the wrong behavior in the code, rather than the other way around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my understanding was that the value being passed was the condition. So
value
should hold the attribute name instead ofcondition
correct? It didn't make sense thatwhatever
(holding the valuefalse
) could be an attribute. Thank you for your patience and help through this issue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m sorry that I snapped. I had a bad day and shouldn’t have spoken to you with this tone. I apologize. In this test,
whatever
is the attribute name. Not the value. You can see if from the first sentence:Therefore, a condition like
{whatever ? value : undefined}
is not relevant to the user code. The user has something likewhatever={condition && value}
withcondition
beingfalse
. We want to tell the user to turn it into something likewhatever={condition ? value : undefined}
.So I think ideally the messages would be:
true
:false
:In these examples,
whatever
should of course refer to the attribute name, not literally "whatever". I only usedwhatever
because that's what the test already uses.I hope this is helpful and clarifies things!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to apologize, your message did not come off badly. I only apologized for making you revise this pr so many times because I did not grasp the whole picture. Yes, this is extremely helpful. Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you prefer:
or
I feel the second option is a bit more verbose and easier to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about the second one but it will throw for null/undefined. Might be unexpected. First one seems safer. Although on the other hand you probably don't want
"undefined"
or"null"
as strings either.I wish there was some concise way to express "stringify unless it's null/undefined".
I guess I like that
value.toString()
throws it it doesn't exist. If you mess it up you'll immediately realize it by the crash, instead of quietly using the wrong value. So let's suggest that.