-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Warn when "false" or "true" is the value of a boolean DOM prop #13372
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
Changes from 1 commit
1447508
b8f83cd
f3248f2
f70a0eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2506,6 +2506,32 @@ describe('ReactDOMComponent', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('Boolean attributes', function() { | ||
| it('warns on the ambiguous string value "false"', function() { | ||
| let el; | ||
| expect(() => { | ||
| el = ReactTestUtils.renderIntoDocument(<div hidden="false" />); | ||
| }).toWarnDev( | ||
| 'Received the string `false` for the boolean attribute `hidden`. ' + | ||
| 'Did you mean hidden={false}?', | ||
| ); | ||
|
|
||
| expect(el.getAttribute('hidden')).toBe(''); | ||
| }); | ||
|
|
||
| it('warns on the ambiguous string value "False"', function() { | ||
| let el; | ||
| expect(() => { | ||
| el = ReactTestUtils.renderIntoDocument(<div hidden="False" />); | ||
|
||
| }).toWarnDev( | ||
| 'Received the string `False` for the boolean attribute `hidden`. ' + | ||
| 'Did you mean hidden={false}?', | ||
| ); | ||
|
|
||
| expect(el.getAttribute('hidden')).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Hyphenated SVG elements', function() { | ||
| it('the font-face element is not a custom element', function() { | ||
| let el; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import warning from 'shared/warning'; | |
|
|
||
| import { | ||
| ATTRIBUTE_NAME_CHAR, | ||
| BOOLEAN, | ||
| RESERVED, | ||
| shouldRemoveAttributeWithWarning, | ||
| getPropertyInfo, | ||
|
|
@@ -226,6 +227,25 @@ if (__DEV__) { | |
| return false; | ||
| } | ||
|
|
||
| // Warn when passing the string 'false' into a boolean prop | ||
| if ( | ||
| propertyInfo !== null && | ||
| propertyInfo.type === BOOLEAN && | ||
| typeof value === 'string' && | ||
| value.toLowerCase() === 'false' | ||
| ) { | ||
|
Collaborator
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. Let’s make |
||
| warning( | ||
| false, | ||
| 'Received the string `%s` for the boolean attribute `%s`. ' + | ||
| 'Did you mean %s={false}?', | ||
| value, | ||
| name, | ||
| name, | ||
| ); | ||
| warnedProperties[name] = true; | ||
| return true; | ||
| } | ||
|
|
||
| return true; | ||
| }; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.