From aa90f53b7fcd522e1e8ed2a6e41c0d580688a16f Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 22 Dec 2021 13:28:56 -0800 Subject: [PATCH] [New] add `PropTypes.bigint` Closes #355 --- README.md | 1 + __tests__/PropTypesDevelopmentReact15.js | 25 ++++++++++++++++++- .../PropTypesDevelopmentStandalone-test.js | 25 ++++++++++++++++++- __tests__/PropTypesProductionReact15-test.js | 25 ++++++++++++++++++- .../PropTypesProductionStandalone-test.js | 12 +++++++++ factoryWithThrowingShims.js | 1 + factoryWithTypeCheckers.js | 1 + 7 files changed, 87 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4e283bb..e54d435 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ MyComponent.propTypes = { // You can declare that a prop is a specific JS primitive. By default, these // are all optional. optionalArray: PropTypes.array, + optionalBigInt: PropTypes.bigint, optionalBool: PropTypes.bool, optionalFunc: PropTypes.func, optionalNumber: PropTypes.number, diff --git a/__tests__/PropTypesDevelopmentReact15.js b/__tests__/PropTypesDevelopmentReact15.js index 42ebafb..a617234 100644 --- a/__tests__/PropTypesDevelopmentReact15.js +++ b/__tests__/PropTypesDevelopmentReact15.js @@ -328,6 +328,9 @@ describe('PropTypesDevelopmentReact15', () => { it('should not warn for valid values', () => { typeCheckPass(PropTypes.array, []); + if (typeof BigInt === 'function') { + typeCheckPass(PropTypes.bigint, BigInt(0)); + } typeCheckPass(PropTypes.bool, false); typeCheckPass(PropTypes.func, function() {}); typeCheckPass(PropTypes.number, 0); @@ -348,6 +351,7 @@ describe('PropTypesDevelopmentReact15', () => { typeCheckFailRequiredValues(PropTypes.array.isRequired); typeCheckFailRequiredValues(PropTypes.symbol.isRequired); typeCheckFailRequiredValues(PropTypes.number.isRequired); + typeCheckFailRequiredValues(PropTypes.bigint.isRequired); typeCheckFailRequiredValues(PropTypes.bool.isRequired); typeCheckFailRequiredValues(PropTypes.func.isRequired); typeCheckFailRequiredValues(PropTypes.shape({}).isRequired); @@ -361,6 +365,15 @@ describe('PropTypesDevelopmentReact15', () => { expectWarningInDevelopment(PropTypes.array.isRequired, []); expectWarningInDevelopment(PropTypes.array.isRequired, null); expectWarningInDevelopment(PropTypes.array.isRequired, undefined); + expectWarningInDevelopment(PropTypes.bigint, function() {}); + expectWarningInDevelopment(PropTypes.bigint, 42); + if (typeof BigInt === 'function') { + expectWarningInDevelopment(PropTypes.bigint, BigInt(42)); + } + expectWarningInDevelopment(PropTypes.bigint.isRequired, function() {}); + expectWarningInDevelopment(PropTypes.bigint.isRequired, 42); + expectWarningInDevelopment(PropTypes.bigint.isRequired, null); + expectWarningInDevelopment(PropTypes.bigint.isRequired, undefined); expectWarningInDevelopment(PropTypes.bool, []); expectWarningInDevelopment(PropTypes.bool, true); expectWarningInDevelopment(PropTypes.bool.isRequired, []); @@ -436,6 +449,9 @@ describe('PropTypesDevelopmentReact15', () => { it('should support the arrayOf propTypes', () => { typeCheckPass(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]); + if (typeof BigInt === 'function') { + typeCheckPass(PropTypes.arrayOf(PropTypes.bigint), [BigInt(1), BigInt(2), BigInt(3)]); + } typeCheckPass(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']); typeCheckPass(PropTypes.arrayOf(PropTypes.oneOf(['a', 'b'])), ['a', 'b']); typeCheckPass(PropTypes.arrayOf(PropTypes.symbol), [Symbol(), Symbol()]); @@ -539,7 +555,6 @@ describe('PropTypesDevelopmentReact15', () => { }); describe('Component Type', () => { - it('should support components', () => { typeCheckPass(PropTypes.element,
); }); @@ -557,6 +572,14 @@ describe('PropTypesDevelopmentReact15', () => { 'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' + 'expected a single ReactElement.', ); + if (typeof BigInt === 'function') { + typeCheckFail( + PropTypes.element, + BigInt(123), + 'Invalid prop `testProp` of type `bigint` supplied to `testComponent`, ' + + 'expected a single ReactElement.', + ); + } typeCheckFail( PropTypes.element, 'foo', diff --git a/__tests__/PropTypesDevelopmentStandalone-test.js b/__tests__/PropTypesDevelopmentStandalone-test.js index 3c32fe1..ada4ea1 100644 --- a/__tests__/PropTypesDevelopmentStandalone-test.js +++ b/__tests__/PropTypesDevelopmentStandalone-test.js @@ -325,6 +325,9 @@ describe('PropTypesDevelopmentStandalone', () => { it('should not warn for valid values', () => { typeCheckPass(PropTypes.array, []); + if (typeof BigInt === 'function') { + typeCheckPass(PropTypes.bigint, BigInt(0)); + } typeCheckPass(PropTypes.bool, false); typeCheckPass(PropTypes.func, function() {}); typeCheckPass(PropTypes.number, 0); @@ -345,6 +348,7 @@ describe('PropTypesDevelopmentStandalone', () => { typeCheckFailRequiredValues(PropTypes.array.isRequired); typeCheckFailRequiredValues(PropTypes.symbol.isRequired); typeCheckFailRequiredValues(PropTypes.number.isRequired); + typeCheckFailRequiredValues(PropTypes.bigint.isRequired); typeCheckFailRequiredValues(PropTypes.bool.isRequired); typeCheckFailRequiredValues(PropTypes.func.isRequired); typeCheckFailRequiredValues(PropTypes.shape({}).isRequired); @@ -358,6 +362,15 @@ describe('PropTypesDevelopmentStandalone', () => { expectThrowsInDevelopment(PropTypes.array.isRequired, []); expectThrowsInDevelopment(PropTypes.array.isRequired, null); expectThrowsInDevelopment(PropTypes.array.isRequired, undefined); + expectThrowsInDevelopment(PropTypes.bigint, function() {}); + expectThrowsInDevelopment(PropTypes.bigint, 42); + if (typeof BigInt === 'function') { + expectThrowsInDevelopment(PropTypes.bigint, BigInt(42)); + } + expectThrowsInDevelopment(PropTypes.bigint.isRequired, function() {}); + expectThrowsInDevelopment(PropTypes.bigint.isRequired, 42); + expectThrowsInDevelopment(PropTypes.bigint.isRequired, null); + expectThrowsInDevelopment(PropTypes.bigint.isRequired, undefined); expectThrowsInDevelopment(PropTypes.bool, []); expectThrowsInDevelopment(PropTypes.bool, true); expectThrowsInDevelopment(PropTypes.bool.isRequired, []); @@ -433,6 +446,9 @@ describe('PropTypesDevelopmentStandalone', () => { it('should support the arrayOf propTypes', () => { typeCheckPass(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]); + if (typeof BigInt === 'function') { + typeCheckPass(PropTypes.arrayOf(PropTypes.bigint), [BigInt(1), BigInt(2), BigInt(3)]); + } typeCheckPass(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']); typeCheckPass(PropTypes.arrayOf(PropTypes.oneOf(['a', 'b'])), ['a', 'b']); typeCheckPass(PropTypes.arrayOf(PropTypes.symbol), [Symbol(), Symbol()]); @@ -536,7 +552,6 @@ describe('PropTypesDevelopmentStandalone', () => { }); describe('Component Type', () => { - it('should support components', () => { typeCheckPass(PropTypes.element,
); }); @@ -554,6 +569,14 @@ describe('PropTypesDevelopmentStandalone', () => { 'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' + 'expected a single ReactElement.', ); + if (typeof BigInt === 'function') { + typeCheckFail( + PropTypes.element, + BigInt(123), + 'Invalid prop `testProp` of type `bigint` supplied to `testComponent`, ' + + 'expected a single ReactElement.', + ); + } typeCheckFail( PropTypes.element, 'foo', diff --git a/__tests__/PropTypesProductionReact15-test.js b/__tests__/PropTypesProductionReact15-test.js index 66de1ad..63ba56d 100644 --- a/__tests__/PropTypesProductionReact15-test.js +++ b/__tests__/PropTypesProductionReact15-test.js @@ -104,6 +104,9 @@ describe('PropTypesProductionReact15', () => { it('should not warn for valid values', () => { expectNoop(PropTypes.array, []); + if (typeof BigInt === 'function') { + expectNoop(PropTypes.bigint, BigInt(0)); + } expectNoop(PropTypes.bool, false); expectNoop(PropTypes.func, function() {}); expectNoop(PropTypes.number, 0); @@ -124,6 +127,7 @@ describe('PropTypesProductionReact15', () => { expectNoop(PropTypes.array.isRequired); expectNoop(PropTypes.symbol.isRequired); expectNoop(PropTypes.number.isRequired); + expectNoop(PropTypes.bigint.isRequired); expectNoop(PropTypes.bool.isRequired); expectNoop(PropTypes.func.isRequired); expectNoop(PropTypes.shape({}).isRequired); @@ -137,6 +141,15 @@ describe('PropTypesProductionReact15', () => { expectNoop(PropTypes.array.isRequired, []); expectNoop(PropTypes.array.isRequired, null); expectNoop(PropTypes.array.isRequired, undefined); + expectNoop(PropTypes.bigint, function() {}); + expectNoop(PropTypes.bigint, 42); + if (typeof BigInt === 'function') { + expectNoop(PropTypes.bigint, BigInt(42)); + } + expectNoop(PropTypes.bigint.isRequired, function() {}); + expectNoop(PropTypes.bigint.isRequired, 42); + expectNoop(PropTypes.bigint.isRequired, null); + expectNoop(PropTypes.bigint.isRequired, undefined); expectNoop(PropTypes.bool, []); expectNoop(PropTypes.bool, true); expectNoop(PropTypes.bool.isRequired, []); @@ -212,6 +225,9 @@ describe('PropTypesProductionReact15', () => { it('should support the arrayOf propTypes', () => { expectNoop(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]); + if (typeof BigInt === 'function') { + expectNoop(PropTypes.arrayOf(PropTypes.bigint), [BigInt(1), BigInt(2), BigInt(3)]); + } expectNoop(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']); expectNoop(PropTypes.arrayOf(PropTypes.oneOf(['a', 'b'])), ['a', 'b']); expectNoop(PropTypes.arrayOf(PropTypes.symbol), [Symbol(), Symbol()]); @@ -315,7 +331,6 @@ describe('PropTypesProductionReact15', () => { }); describe('Component Type', () => { - it('should support components', () => { expectNoop(PropTypes.element,
); }); @@ -333,6 +348,14 @@ describe('PropTypesProductionReact15', () => { 'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' + 'expected a single ReactElement.', ); + if (typeof BigInt === 'function') { + expectNoop( + PropTypes.element, + BigInt(123), + 'Invalid prop `testProp` of type `bigint` supplied to `testComponent`, ' + + 'expected a single ReactElement.', + ); + } expectNoop( PropTypes.element, 'foo', diff --git a/__tests__/PropTypesProductionStandalone-test.js b/__tests__/PropTypesProductionStandalone-test.js index ea9fe1d..6584d47 100644 --- a/__tests__/PropTypesProductionStandalone-test.js +++ b/__tests__/PropTypesProductionStandalone-test.js @@ -76,6 +76,15 @@ describe('PropTypesProductionStandalone', function() { expectThrowsInProduction(PropTypes.array.isRequired, /please/); expectThrowsInProduction(PropTypes.array.isRequired, null); expectThrowsInProduction(PropTypes.array.isRequired, undefined); + expectThrowsInProduction(PropTypes.bigint, function() {}); + expectThrowsInProduction(PropTypes.bigint, 42); + if (typeof BigInt === 'function') { + expectThrowsInProduction(PropTypes.bigint, BigInt(42)); + } + expectThrowsInProduction(PropTypes.bigint.isRequired, function() {}); + expectThrowsInProduction(PropTypes.bigint.isRequired, 42); + expectThrowsInProduction(PropTypes.bigint.isRequired, null); + expectThrowsInProduction(PropTypes.bigint.isRequired, undefined); expectThrowsInProduction(PropTypes.bool, []); expectThrowsInProduction(PropTypes.bool.isRequired, []); expectThrowsInProduction(PropTypes.bool.isRequired, null); @@ -140,6 +149,9 @@ describe('PropTypesProductionStandalone', function() { it('should be a no-op', function() { expectThrowsInProduction(PropTypes.element, [
,
]); expectThrowsInProduction(PropTypes.element, 123); + if (typeof BigInt === 'function') { + expectThrowsInProduction(PropTypes.element, BigInt(123)); + } expectThrowsInProduction(PropTypes.element, 'foo'); expectThrowsInProduction(PropTypes.element, false); expectThrowsInProduction(PropTypes.element.isRequired, null); diff --git a/factoryWithThrowingShims.js b/factoryWithThrowingShims.js index e5b2f9c..ac88267 100644 --- a/factoryWithThrowingShims.js +++ b/factoryWithThrowingShims.js @@ -35,6 +35,7 @@ module.exports = function() { // Keep this list in sync with production version in `./factoryWithTypeCheckers.js`. var ReactPropTypes = { array: shim, + bigint: shim, bool: shim, func: shim, number: shim, diff --git a/factoryWithTypeCheckers.js b/factoryWithTypeCheckers.js index 09441a2..6108567 100644 --- a/factoryWithTypeCheckers.js +++ b/factoryWithTypeCheckers.js @@ -114,6 +114,7 @@ module.exports = function(isValidElement, throwOnDirectAccess) { // Keep this list in sync with production version in `./factoryWithThrowingShims.js`. var ReactPropTypes = { array: createPrimitiveTypeChecker('array'), + bigint: createPrimitiveTypeChecker('bigint'), bool: createPrimitiveTypeChecker('boolean'), func: createPrimitiveTypeChecker('function'), number: createPrimitiveTypeChecker('number'),