From 4523b4504d553b847f2dadc3d9b8870db2ffec4a Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 2 Apr 2021 14:21:13 -0400 Subject: [PATCH 1/4] Exclude Babel Runtime error from our own output Former-commit-id: 9740e5ea973de7fb5693def26e4cbb350fe2407b --- scripts/mangleErrors.js.REMOVED.git-id | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/mangleErrors.js.REMOVED.git-id b/scripts/mangleErrors.js.REMOVED.git-id index f632c9b068f..79ccdb8ee53 100644 --- a/scripts/mangleErrors.js.REMOVED.git-id +++ b/scripts/mangleErrors.js.REMOVED.git-id @@ -1 +1 @@ -d8b8502a52b248ac7e5ebc22f33a4763e05af8b2 \ No newline at end of file +2c4ab333936364bf77130db1435ed44fbb4ccbce \ No newline at end of file From 636232bede8c4e9ff508b76b4bec405dc5f68c98 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 2 Apr 2021 14:50:21 -0400 Subject: [PATCH 2/4] Add CodeSandbox-CI config Former-commit-id: b5cefa2490ba6713ce2f978d0bb028af1f8089d5 --- .codesandbox/ci.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .codesandbox/ci.json diff --git a/.codesandbox/ci.json b/.codesandbox/ci.json new file mode 100644 index 00000000000..f86122cef48 --- /dev/null +++ b/.codesandbox/ci.json @@ -0,0 +1,7 @@ +{ + "sandboxes": [ + "vanilla", + "vanilla-ts" + ], + "node": "14" +} From ba572ce47ee5e85b294428154da443fcfdd2ae3c Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 2 Apr 2021 14:53:25 -0400 Subject: [PATCH 3/4] Rewrite Redux error messages - Added miniature port of `kindOf` for better `typeof` descriptions - Used `kindOf` for all runtime type check errors - Inlined "slice reducer returned undefined" error message - Rewrote "actions must be plain objects" error message to show the actual variable type and added tests - Reworded a bunch of error messages - Added some additional docs links - Updated tests that check error messages Former-commit-id: 3807c07013fdc61b1b58407beadc45687a7931a3 --- errors.json | 26 ++++---- src/bindActionCreators.ts | 7 ++- src/combineReducers.ts.REMOVED.git-id | 2 +- src/createStore.ts.REMOVED.git-id | 2 +- src/utils/kindOf.ts | 66 +++++++++++++++++++++ test/bindActionCreators.spec.ts | 12 ++-- test/combineReducers.spec.ts.REMOVED.git-id | 2 +- test/createStore.spec.ts.REMOVED.git-id | 2 +- 8 files changed, 93 insertions(+), 26 deletions(-) create mode 100644 src/utils/kindOf.ts diff --git a/errors.json b/errors.json index 49e6c98a69a..3130695822a 100644 --- a/errors.json +++ b/errors.json @@ -1,19 +1,19 @@ { - "0": "It looks like you are passing several store enhancers to createStore(). This is not supported. Instead, compose them together to a single function.", - "1": "Expected the enhancer to be a function.", - "2": "Expected the reducer to be a function.", + "0": "It looks like you are passing several store enhancers to createStore(). This is not supported. Instead, compose them together to a single function. See https://redux.js.org/tutorials/fundamentals/part-4-store#creating-a-store-with-enhancers for an example.", + "1": "Expected the enhancer to be a function. Instead, received: ''", + "2": "Expected the root reducer to be a function. Instead, received: ''", "3": "You may not call store.getState() while the reducer is executing. The reducer has already received the state as an argument. Pass it down from the top reducer instead of reading it from the store.", - "4": "Expected the listener to be a function.", + "4": "Expected the listener to be a function. Instead, received: ''", "5": "You may not call store.subscribe() while the reducer is executing. If you would like to be notified after the store has been updated, subscribe from a component and invoke store.getState() in the callback to access the latest state. See https://redux.js.org/api/store#subscribelistener for more details.", "6": "You may not unsubscribe from a store listener while the reducer is executing. See https://redux.js.org/api/store#subscribelistener for more details.", - "7": "Actions must be plain objects. Use custom middleware for async actions.", - "8": "Actions may not have an undefined \"type\" property. Have you misspelled a constant?", + "7": "Actions must be plain objects. Instead, the actual type was: ''. You may need to add middleware to your store setup to handle dispatching other values, such as 'redux-thunk' to handle dispatching functions. See https://redux.js.org/tutorials/fundamentals/part-4-store#middleware and https://redux.js.org/tutorials/fundamentals/part-6-async-logic#using-the-redux-thunk-middleware for examples.", + "8": "Actions may not have an undefined \"type\" property. You may have misspelled an action type string constant.", "9": "Reducers may not dispatch actions.", - "10": "Expected the nextReducer to be a function.", - "11": "Expected the observer to be an object.", - "12": "bindActionCreators expected an object or a function, instead received . Did you write \"import ActionCreators from\" instead of \"import * as ActionCreators from\"?", - "13": "Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch.", - "14": "Reducer \"\" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.", - "15": "Reducer \"\" returned undefined when probed with a random type. Don't try to handle or other actions in \"redux/*\" namespace. They are considered private. Instead, you must return the current state for any unknown actions, unless it is undefined, in which case you must return the initial state, regardless of the action type. The initial state may not be undefined, but can be null.", - "16": "Super expression must either be null or a function" + "10": "Expected the nextReducer to be a function. Instead, received: '", + "11": "Expected the observer to be an object. Instead, received: ''", + "12": "The slice reducer for key \"\" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.", + "13": "The slice reducer for key \"\" returned undefined when probed with a random type. Don't try to handle or other actions in \"redux/*\" namespace. They are considered private. Instead, you must return the current state for any unknown actions, unless it is undefined, in which case you must return the initial state, regardless of the action type. The initial state may not be undefined, but can be null.", + "14": "When called with an action of type , the slice reducer for key \"\" returned undefined. To ignore an action, you must explicitly return the previous state. If you want this reducer to hold no value, you can return null instead of undefined.", + "15": "Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch.", + "16": "bindActionCreators expected an object or a function, but instead received: ''. Did you write \"import ActionCreators from\" instead of \"import * as ActionCreators from\"?" } \ No newline at end of file diff --git a/src/bindActionCreators.ts b/src/bindActionCreators.ts index 620cf9f6a48..022d15bb0ea 100644 --- a/src/bindActionCreators.ts +++ b/src/bindActionCreators.ts @@ -4,6 +4,7 @@ import { ActionCreator, ActionCreatorsMapObject } from './types/actions' +import { kindOf } from './utils/kindOf' function bindActionCreator( actionCreator: ActionCreator, @@ -64,9 +65,9 @@ export default function bindActionCreators( if (typeof actionCreators !== 'object' || actionCreators === null) { throw new Error( - `bindActionCreators expected an object or a function, instead received ${ - actionCreators === null ? 'null' : typeof actionCreators - }. ` + + `bindActionCreators expected an object or a function, but instead received: '${kindOf( + actionCreators + )}'. ` + `Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?` ) } diff --git a/src/combineReducers.ts.REMOVED.git-id b/src/combineReducers.ts.REMOVED.git-id index c94c50e67cd..96e34164c81 100644 --- a/src/combineReducers.ts.REMOVED.git-id +++ b/src/combineReducers.ts.REMOVED.git-id @@ -1 +1 @@ -0da94c9b85327e579d0082c6c26137f4d973c0c2 \ No newline at end of file +8ab020ebc16b7bbb88896f9a7ab48fc37b3f6408 \ No newline at end of file diff --git a/src/createStore.ts.REMOVED.git-id b/src/createStore.ts.REMOVED.git-id index 38b50a7a361..ed82e88f5ec 100644 --- a/src/createStore.ts.REMOVED.git-id +++ b/src/createStore.ts.REMOVED.git-id @@ -1 +1 @@ -3a980bba37bacd3f9ac670ba9db2fcda6c5cdb83 \ No newline at end of file +d1bc6abaa2ca81980ad24406d28277816cd3cc45 \ No newline at end of file diff --git a/src/utils/kindOf.ts b/src/utils/kindOf.ts new file mode 100644 index 00000000000..2c600bc702f --- /dev/null +++ b/src/utils/kindOf.ts @@ -0,0 +1,66 @@ +export function kindOf(val: any): string { + let typeOfVal: string = typeof val + + if (process.env.NODE_ENV !== 'production') { + // Inlined / shortened version of `kindOf` from https://github.com/jonschlinkert/kind-of + function miniKindOf(val: any) { + if (val === void 0) return 'undefined' + if (val === null) return 'null' + + const type = typeof val + switch (type) { + case 'boolean': + case 'string': + case 'number': + case 'symbol': + case 'function': { + return type + } + } + + if (Array.isArray(val)) return 'array' + if (isDate(val)) return 'date' + if (isError(val)) return 'error' + + const constructorName = ctorName(val) + switch (constructorName) { + case 'Symbol': + case 'Promise': + case 'WeakMap': + case 'WeakSet': + case 'Map': + case 'Set': + return constructorName + } + + // other + return type.slice(8, -1).toLowerCase().replace(/\s/g, '') + } + + function ctorName(val: any): string | null { + return typeof val.constructor === 'function' ? val.constructor.name : null + } + + function isError(val: any) { + return ( + val instanceof Error || + (typeof val.message === 'string' && + val.constructor && + typeof val.constructor.stackTraceLimit === 'number') + ) + } + + function isDate(val: any) { + if (val instanceof Date) return true + return ( + typeof val.toDateString === 'function' && + typeof val.getDate === 'function' && + typeof val.setDate === 'function' + ) + } + + typeOfVal = miniKindOf(val) + } + + return typeOfVal +} diff --git a/test/bindActionCreators.spec.ts b/test/bindActionCreators.spec.ts index 5efecb6902f..2becf689a87 100644 --- a/test/bindActionCreators.spec.ts +++ b/test/bindActionCreators.spec.ts @@ -77,8 +77,8 @@ describe('bindActionCreators', () => { expect(() => { bindActionCreators(undefined, store.dispatch) }).toThrow( - 'bindActionCreators expected an object or a function, instead received undefined. ' + - 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?' + `bindActionCreators expected an object or a function, but instead received: 'undefined'. ` + + `Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?` ) }) @@ -86,8 +86,8 @@ describe('bindActionCreators', () => { expect(() => { bindActionCreators(null, store.dispatch) }).toThrow( - 'bindActionCreators expected an object or a function, instead received null. ' + - 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?' + `bindActionCreators expected an object or a function, but instead received: 'null'. ` + + `Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?` ) }) @@ -98,8 +98,8 @@ describe('bindActionCreators', () => { store.dispatch ) }).toThrow( - 'bindActionCreators expected an object or a function, instead received string. ' + - 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?' + `bindActionCreators expected an object or a function, but instead received: 'string'. ` + + `Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?` ) }) }) diff --git a/test/combineReducers.spec.ts.REMOVED.git-id b/test/combineReducers.spec.ts.REMOVED.git-id index 48cfb5fa561..7b91b7e03b1 100644 --- a/test/combineReducers.spec.ts.REMOVED.git-id +++ b/test/combineReducers.spec.ts.REMOVED.git-id @@ -1 +1 @@ -87dc94fb1b1c28380de657c4df0f41d14a1c9f91 \ No newline at end of file +59786a1c8429828c8303f9b859622982997363bc \ No newline at end of file diff --git a/test/createStore.spec.ts.REMOVED.git-id b/test/createStore.spec.ts.REMOVED.git-id index 84fe2f388ba..fa4928464da 100644 --- a/test/createStore.spec.ts.REMOVED.git-id +++ b/test/createStore.spec.ts.REMOVED.git-id @@ -1 +1 @@ -e422c73c434f0bb6184a6f9043b3a205796bcf08 \ No newline at end of file +c9e1143bb76552060055fe1ce29b57a8670465b1 \ No newline at end of file From 62066361509096f3b7893af6948ac387428861c1 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 2 Apr 2021 15:42:43 -0400 Subject: [PATCH 4/4] Add quotes around INIT action type in error Former-commit-id: f843f6e3843aa221f5a83ad9861bd02734900fa7 --- errors.json | 2 +- src/combineReducers.ts.REMOVED.git-id | 2 +- src/createStore.ts.REMOVED.git-id | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/errors.json b/errors.json index 3130695822a..2fac82d961e 100644 --- a/errors.json +++ b/errors.json @@ -12,7 +12,7 @@ "10": "Expected the nextReducer to be a function. Instead, received: '", "11": "Expected the observer to be an object. Instead, received: ''", "12": "The slice reducer for key \"\" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.", - "13": "The slice reducer for key \"\" returned undefined when probed with a random type. Don't try to handle or other actions in \"redux/*\" namespace. They are considered private. Instead, you must return the current state for any unknown actions, unless it is undefined, in which case you must return the initial state, regardless of the action type. The initial state may not be undefined, but can be null.", + "13": "The slice reducer for key \"\" returned undefined when probed with a random type. Don't try to handle '' or other actions in \"redux/*\" namespace. They are considered private. Instead, you must return the current state for any unknown actions, unless it is undefined, in which case you must return the initial state, regardless of the action type. The initial state may not be undefined, but can be null.", "14": "When called with an action of type , the slice reducer for key \"\" returned undefined. To ignore an action, you must explicitly return the previous state. If you want this reducer to hold no value, you can return null instead of undefined.", "15": "Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch.", "16": "bindActionCreators expected an object or a function, but instead received: ''. Did you write \"import ActionCreators from\" instead of \"import * as ActionCreators from\"?" diff --git a/src/combineReducers.ts.REMOVED.git-id b/src/combineReducers.ts.REMOVED.git-id index 96e34164c81..25072ef1ad3 100644 --- a/src/combineReducers.ts.REMOVED.git-id +++ b/src/combineReducers.ts.REMOVED.git-id @@ -1 +1 @@ -8ab020ebc16b7bbb88896f9a7ab48fc37b3f6408 \ No newline at end of file +2de2e55bd890e8f8a7ee4f2f1215b7a787e6e4ec \ No newline at end of file diff --git a/src/createStore.ts.REMOVED.git-id b/src/createStore.ts.REMOVED.git-id index ed82e88f5ec..6cd9f431e45 100644 --- a/src/createStore.ts.REMOVED.git-id +++ b/src/createStore.ts.REMOVED.git-id @@ -1 +1 @@ -d1bc6abaa2ca81980ad24406d28277816cd3cc45 \ No newline at end of file +73b27c1be652de0e722106efe07f93f5c418d83b \ No newline at end of file