-
Notifications
You must be signed in to change notification settings - Fork 47.7k
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
Fix #3596: Add warning for components not following proper case convention #7089
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule ReactDOMUnknownComponentDevtool | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var ReactComponentTreeDevtool = require('ReactComponentTreeDevtool'); | ||
var warning = require('warning'); | ||
var ReactDOMFactories = require('ReactDOMFactories'); | ||
var didWarnAboutCase = false; | ||
|
||
function handleElement(debugID, element) { | ||
if (element == null || typeof element.type !== 'string' || | ||
element.type.match(/\-|\s+/) !== null || | ||
ReactDOMFactories.hasOwnProperty(element.type)) { | ||
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. ReactDOMFactories isn't really comprehensive and may go away in the near future, so I don't really trust this as a reliable source of information. 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. This implementation would require validating the tags against some kind of whitelist since 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. @aweary Agreed, as far as I know there's none. 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. If there isn't a secondary source to validate against to ensure that 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. @aweary completely agree with you, but other than keeping a list of elements, how else can we make sure there's no false positives? According to the the specs, SVG and MathML are elements allowed in HTML which belong to their own respective namespace, so createElement will return Seems to me the solution for now is, we have to list svg/mathml valid tags and make sure we "ignore" those as valid. In a perfect world we could get this data from W3C but I couldn't find anything useful to consume other than scraping their repo or MDN. They do have the useful tests that we can probably gather something from. However this is essentially keeping a list of elements (kind of like the same ReactDOMFactories is doing) which it was already said is not the best solution. What do you guys think would be the ideal solution? |
||
return; | ||
} | ||
|
||
var invalidElement = document.createElement(element.type) instanceof | ||
HTMLUnknownElement; | ||
|
||
if (!didWarnAboutCase && invalidElement) { | ||
warning(false, | ||
'Unknown element %s. Did you miss-spell an HTML tag name? ' + | ||
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.
Ironically, this is misspelled. You want "misspell". |
||
'If you are using a custom component, make sure your custom component ' + | ||
'starts with an upper-case letter.%s', | ||
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.
We use "uppercase" elsewhere so let's do the same here. |
||
element.type, | ||
ReactComponentTreeDevtool.getStackAddendumByID(debugID) | ||
); | ||
didWarnAboutCase = true; | ||
} | ||
} | ||
|
||
var ReactDOMUnknownComponentDevtool = { | ||
onBeforeMountComponent(debugID, element) { | ||
handleElement(debugID, element); | ||
}, | ||
onBeforeUpdateComponent(debugID, element) { | ||
handleElement(debugID, element); | ||
}, | ||
}; | ||
module.exports = ReactDOMUnknownComponentDevtool; |
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 think the warning would fire even without this class being defined, right? This is effectively just catching unknown elements?
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 think we can just delete this line.
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 added the
createClass
line to test that the warning was actually triggering but yes feel free to delete it.Correct as long as the
element.type
passes the criteria: