@samitouri / QOS-React / commits / f44a0778e3

Don't use `key` when defined on String, Number prototypes

Paul O’Shannessy committed Nov 23, 2015 at 15:33 UTC f44a0778e384927d6724650deb8558bf4b53d5e4
2 files changed +36 -1
src/shared/utils/__tests__/traverseAllChildren-test.js
+33
@@ -476,6 +476,39 @@ describe('traverseAllChildren', function() {
476 }
477 });
478
479 + it('should allow extension of native prototypes', function() {
480 + /*eslint-disable no-extend-native */
481 + String.prototype.key = 'react';
482 + Number.prototype.key = 'rocks';
483 + /*eslint-enable no-extend-native */
484 +
485 + var instance = (
486 + <div>
487 + {'a'}
488 + {13}
489 + </div>
490 + );
491 +
492 + var traverseFn = jasmine.createSpy();
493 +
494 + traverseAllChildren(instance.props.children, traverseFn, null);
495 + expect(traverseFn.calls.length).toBe(2);
496 +
497 + expect(traverseFn).toHaveBeenCalledWith(
498 + null,
499 + 'a',
500 + '.0'
501 + );
502 + expect(traverseFn).toHaveBeenCalledWith(
503 + null,
504 + 13,
505 + '.1'
506 + );
507 +
508 + delete String.prototype.key;
509 + delete Number.prototype.key;
510 + });
511 +
512 it('should throw on object', function() {
513 expect(function() {
514 traverseAllChildren({a: 1, b: 2}, function() {}, null);
src/shared/utils/traverseAllChildren.js
+3 -1
@@ -47,7 +47,9 @@ function userProvidedKeyEscaper(match) {
47 * @return {string}
48 */
49 function getComponentKey(component, index) {
50 - if (component && component.key != null) {
50 + // Do some typechecking here since we call this blindly. We want to ensure
51 + // that we don't block potential future ES APIs.
52 + if (component && typeof component === 'object' && component.key != null) {
53 // Explicit key
54 return wrapUserProvidedKey(component.key);
55 }