-
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
Add new primitive PropType Symbol
#6377
Conversation
With the edition of ECMA-262, we now have a new primative type called Symbol. This primative type should be added to the PropTypes, as users will eventually be using Symbols in their props for describing unique and immutable data, such as identifiers.
I believe that I covered all the typical use cases for Symbols. If you think that I missed one, feel free to contribute.
This relates to issue #4917 |
If the user is using a Babel polyfill, would a component defined with |
@gaearon I'd imagine that |
I don't think showing a warning to users of polyfill is desirable. |
We could do what babel compiles 'use strict';
// refactored for readability
var nativeSymbolSupported = typeof Symbol === "function" && typeof Symbol.iterator === "symbol";
var _typeof = nativeSymbolSupported
? function (obj) { return typeof obj; }
: function (obj) {
if (obj && typeof Symbol === "function" && obj.constructor === Symbol) {
return "symbol"
}
else {
typeof obj;
}
};
(typeof x === 'undefined' ? 'undefined' : _typeof(x)) === 'symbol'; |
@brigand I like that approach much better 👍 We could further simplify and extend this to // Equivalent of `typeof` but with special handling for array and regexp.
function getPropType(propValue) {
var propType = typeof propValue;
// ...
if (propType === 'function' && propValue instanceof Symbol) {
return 'symbol';
}
return propType;
} |
@puradox That's definitely a good idea! But how can you be sure that What do you think so? |
@RaitoBezarius You bring up a good point with So the check in if (propType === 'function' && typeof Symbol === 'function' && propValue instanceof Symbol) {
return 'symbol';
} Does this address your concern? |
Awesome, according to core-js polyfill: https://github.com/zloirock/core-js/blob/master/modules/es6.symbol.js#L68 -- the test works well. (because core-js is always in the global namespace, I think?) For this polyfill: https://github.com/medikoo/es6-symbol → https://github.com/medikoo/es6-symbol/blob/master/is-symbol.js It sounds like that it's not that spec-compliant because it should be For this polyfill: https://github.com/component/symbol It should solve the problem of testing when no Symbol is available in the global namespace for a strange reason (polyfill is "sandboxed" or not leaked, etc...) Would you like me to write some parts of the code and let you cherry-pick them on your branch so that we can complete this PR ? |
@RaitoBezarius Yeah, it would be great if you could write some parts. |
Most ES6 polyfills will add support by implementing `Symbol` as a function. This causes `typeof` to return `function` rather than `symbol` for polyfilled clients.
@puradox updated the pull request. |
1) If it is a native Symbol 2) If it is spec-compliant 3) Try to match non-spec compliant polyfill by a instanceof check on Symbol if it exists in the global namespace
Improve symbol check for (local) polyfills
@gaearon What are you feelings on adding Symbols polyfills as @puradox Will push some fix for the lint issues (my bad, some habits sticks when switching between projects :D), as for the test which fails, I'll merge back the current master of React inside my fork and try with this, will open two PR (one for the lint, one for the unit test fix). Please, if you have some time, review the code so we can move forward and get this merged 🎉 ! |
…s and trailing comma
Fix code-style issue with React guidelines
We will review this after 15.0 is out. Feel free to ping me if we forget to (but we shouldn't). |
Can we get someone with write access to rerun the Travis-CI tests?
|
Rerunning now |
There seems to be a bug with medikoo/es6-symbol where the global state affects the implementation of the polyfill. I found this by running the individual test file alone then running all the tests using `grunt test`. I found that it passes when ran alone and failed when ran collectively. I did not find this buggy behavior with zloirock/core-js's implementation of a polyfill for Symbol. Thus, removing I will keep the more popular polyfill (core-js) and remove the buggy polyfill (es6-symbol). If you are reading this and can think of a confounding factor that is causing this bug, please let me know and we can try to work together to add support for medikoo/es6-symbol.
@puradox updated the pull request. |
"coveralls": "^2.11.6", | ||
"del": "^2.0.2", | ||
"derequire": "^2.0.3", | ||
"es6-symbol": "^3.0.2", |
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.
Is this necessary? Doesn’t appear to be used.
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.
It's a leftover from a @puradox fix on tests — I will clean up this!
On Sat, Apr 9, 2016, 00:26 Dan Abramov notifications@github.com wrote:
In package.json
#6377 (comment):"coveralls": "^2.11.6", "del": "^2.0.2", "derequire": "^2.0.3",
- "es6-symbol": "^3.0.2",
Is this necessary? Doesn’t appear to be used.
—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
https://github.com/facebook/react/pull/6377/files/ad94295f139a8190286852e14a5c731065f5f0ba#r59097655
Ryan Lahfa, de mon téléphone.
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.
Thank you @RaitoBezarius! I forgot to remove this dependency when removing unit test support for es6-symbol.
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 have pushed a fix on your fork to remove the dependency, so that we can
unblock the code review.
Kind regards, Ryan Lahfa
Remove unused ES6 Symbol dependency
@puradox updated the pull request. |
Merging since this was accepted. |
Add new primitive PropType `Symbol` (cherry picked from commit 7bf96c0)
Add new primitive PropType `Symbol` (cherry picked from commit 7bf96c0)
React has introduced a new primitive PropType "symbol". facebook/react#6377 This change adds support for that propType so that it is not reported as "custom".
React has introduced a new primitive PropType "symbol". facebook/react#6377 This change adds support for that propType so that it is not reported as "custom".
React has introduced a new primitive PropType "symbol". facebook/react#6377 This change adds support for that propType so that it is not reported as "custom".
React has introduced a new primitive PropType "symbol". facebook/react#6377 This change adds support for that propType so that it is not reported as "custom".
With the edition of ECMA-262, we now have a new primative type called
Symbol
. This primitive type should be added to the PropTypes, as users will eventually be using Symbols in their props for describing unique and immutable data, such as identifiers.