Skip to content
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

Using Object.is implementation when compare values inside React.PropTypes.oneOf #6132

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ var ReactPropTypes = {
shape: createShapeTypeChecker,
};

/**
* inlined Object.is polyfill to avoid requiring consumers ship their own
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
*/
/*eslint-disable no-self-compare*/
function is(x, y) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, just going to do this for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a weird place to define a polyfill. Seems like we should do something more analogous to https://github.com/facebook/react/blob/master/src/shared/stubs/Object.assign.js

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional and requested by me due to conversations had with @sebmarkbage a couple months ago, don't worry about it. We can deal with figuring out the polyfill story later.

// SameValue algorithm
if (x === y) { // Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
return x !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint emits warnings here

  99:12  warning  Comparing to itself is potentially pointless  no-self-compare
  99:23  warning  Comparing to itself is potentially pointless  no-self-compare

✖ 2 problems (0 errors, 2 warnings)

what should I do here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just disable that warning for the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done and rebased

}
}
/*eslint-enable no-self-compare*/

function createChainableTypeChecker(validate) {
function checkType(
isRequired,
Expand Down Expand Up @@ -218,7 +235,7 @@ function createEnumTypeChecker(expectedValues) {
function validate(props, propName, componentName, location, propFullName) {
var propValue = props[propName];
for (var i = 0; i < expectedValues.length; i++) {
if (propValue === expectedValues[i]) {
if (is(propValue, expectedValues[i])) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ describe('ReactPropTypes', function() {
);
});

it('should warn for invalid strings', function() {
it('should warn for invalid values', function() {
typeCheckFail(
PropTypes.oneOf(['red', 'blue']),
true,
Expand Down Expand Up @@ -606,6 +606,7 @@ describe('ReactPropTypes', function() {
it('should not warn for valid values', function() {
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'red');
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'blue');
typeCheckPass(PropTypes.oneOf(['red', 'blue', NaN]), NaN);
});

it('should be implicitly optional and not warn without values', function() {
Expand Down