From e4a7b7e073302bf970f1f1112b46a36873f7fc4a Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Thu, 5 Oct 2017 12:59:13 +0200 Subject: [PATCH] remove ineffective invariant usages, fixes #2258 (#2693) * remove ineffective invariant usages * fix flow err * fix tests * fix tests * use throw --- src/createNavigationContainer.js | 20 ++++++++--------- src/routers/TabRouter.js | 11 +++++----- src/routers/createConfigGetter.js | 9 ++++---- src/routers/getScreenForRouteName.js | 17 ++++++++------- src/routers/validateRouteConfigMap.js | 31 ++++++++++++--------------- src/routers/validateScreenOptions.js | 10 +++------ 6 files changed, 47 insertions(+), 51 deletions(-) diff --git a/src/createNavigationContainer.js b/src/createNavigationContainer.js index 89d2df4..a98a733 100644 --- a/src/createNavigationContainer.js +++ b/src/createNavigationContainer.js @@ -1,7 +1,6 @@ /* @flow */ import React from 'react'; -import invariant from './utils/invariant'; import { BackHandler, Linking } from './PlatformHelpers'; import NavigationActions from './NavigationActions'; import addNavigationHelpers from './addNavigationHelpers'; @@ -74,15 +73,16 @@ export default function createNavigationContainer( const keys = Object.keys(containerProps); - invariant( - keys.length === 0, - 'This navigator has both navigation and container props, so it is ' + - `unclear if it should own its own state. Remove props: "${keys.join( - ', ' - )}" ` + - 'if the navigator should get its state from the navigation prop. If the ' + - 'navigator should maintain its own state, do not pass a navigation prop.' - ); + if (keys.length !== 0) { + throw new Error( + 'This navigator has both navigation and container props, so it is ' + + `unclear if it should own its own state. Remove props: "${keys.join( + ', ' + )}" ` + + 'if the navigator should get its state from the navigation prop. If the ' + + 'navigator should maintain its own state, do not pass a navigation prop.' + ); + } } _urlToPathAndParams(url: string) { diff --git a/src/routers/TabRouter.js b/src/routers/TabRouter.js index f121aa3..3bb29fe 100644 --- a/src/routers/TabRouter.js +++ b/src/routers/TabRouter.js @@ -45,11 +45,12 @@ export default ( tabRouters[routeName] = routeConfig.screen.router; } }); - invariant( - initialRouteIndex !== -1, - `Invalid initialRouteName '${initialRouteName}' for TabRouter. ` + - `Should be one of ${order.map((n: *) => `"${n}"`).join(', ')}` - ); + if (initialRouteIndex === -1) { + throw new Error( + `Invalid initialRouteName '${initialRouteName}' for TabRouter. ` + + `Should be one of ${order.map((n: *) => `"${n}"`).join(', ')}` + ); + } return { getStateForAction( action: NavigationAction | { action: NavigationAction }, diff --git a/src/routers/createConfigGetter.js b/src/routers/createConfigGetter.js index 58199c0..587d849 100644 --- a/src/routers/createConfigGetter.js +++ b/src/routers/createConfigGetter.js @@ -63,10 +63,11 @@ export default ( let outputConfig = {}; if (Component.router) { - invariant( - route && routes && index != null, - `Expect nav state to have routes and index, ${JSON.stringify(route)}` - ); + if (!route || !routes || index == null) { + throw new Error( + `Expect nav state to have routes and index, ${JSON.stringify(route)}` + ); + } const childRoute = routes[index]; const childNavigation = addNavigationHelpers({ state: childRoute, diff --git a/src/routers/getScreenForRouteName.js b/src/routers/getScreenForRouteName.js index 29e9a30..385c903 100644 --- a/src/routers/getScreenForRouteName.js +++ b/src/routers/getScreenForRouteName.js @@ -17,13 +17,14 @@ export default function getScreenForRouteName( // eslint-disable-line consistent ): NavigationComponent { const routeConfig = routeConfigs[routeName]; - invariant( - routeConfig, - `There is no route defined for key ${routeName}.\n` + - `Must be one of: ${Object.keys(routeConfigs) - .map((a: string) => `'${a}'`) - .join(',')}` - ); + if (!routeConfig) { + throw new Error( + `There is no route defined for key ${routeName}.\n` + + `Must be one of: ${Object.keys(routeConfigs) + .map((a: string) => `'${a}'`) + .join(',')}` + ); + } if (routeConfig.screen) { return routeConfig.screen; @@ -41,5 +42,5 @@ export default function getScreenForRouteName( // eslint-disable-line consistent return screen; } - invariant(false, `Route ${routeName} must define a screen or a getScreen.`); + throw new Error(`Route ${routeName} must define a screen or a getScreen.`); } diff --git a/src/routers/validateRouteConfigMap.js b/src/routers/validateRouteConfigMap.js index 9175606..a816bf2 100644 --- a/src/routers/validateRouteConfigMap.js +++ b/src/routers/validateRouteConfigMap.js @@ -18,28 +18,25 @@ function validateRouteConfigMap(routeConfigs: NavigationRouteConfigMap) { routeNames.forEach((routeName: string) => { const routeConfig = routeConfigs[routeName]; - invariant( - routeConfig.screen || routeConfig.getScreen, - `Route '${routeName}' should declare a screen. ` + - 'For example:\n\n' + - "import MyScreen from './MyScreen';\n" + - '...\n' + - `${routeName}: {\n` + - ' screen: MyScreen,\n' + - '}' - ); - - if (routeConfig.screen && routeConfig.getScreen) { - invariant( - false, + if (!routeConfig.screen && !routeConfig.getScreen) { + throw new Error( + `Route '${routeName}' should declare a screen. ` + + 'For example:\n\n' + + "import MyScreen from './MyScreen';\n" + + '...\n' + + `${routeName}: {\n` + + ' screen: MyScreen,\n' + + '}' + ); + } else if (routeConfig.screen && routeConfig.getScreen) { + throw new Error( `Route '${routeName}' should declare a screen or ` + 'a getScreen, not both.' ); } - if (routeConfig.screen) { - invariant( - typeof routeConfig.screen === 'function', + if (routeConfig.screen && typeof routeConfig.screen !== 'function') { + throw new Error( `The component for route '${routeName}' must be a ` + 'React component. For example:\n\n' + "import MyScreen from './MyScreen';\n" + diff --git a/src/routers/validateScreenOptions.js b/src/routers/validateScreenOptions.js index c47f591..e1716c5 100644 --- a/src/routers/validateScreenOptions.js +++ b/src/routers/validateScreenOptions.js @@ -1,5 +1,4 @@ /* @flow */ -import invariant from '../utils/invariant'; import type { NavigationRoute } from '../TypeDefinition'; @@ -15,8 +14,7 @@ export default (screenOptions: *, route: NavigationRoute) => { const deprecatedKey = keys.find((key: *) => deprecatedKeys.includes(key)); if (typeof screenOptions.title === 'function') { - invariant( - false, + throw new Error( [ `\`title\` cannot be defined as a function in navigation options for \`${route.routeName}\` screen. \n`, 'Try replacing the following:', @@ -33,8 +31,7 @@ export default (screenOptions: *, route: NavigationRoute) => { } if (deprecatedKey && typeof screenOptions[deprecatedKey] === 'function') { - invariant( - false, + throw new Error( [ `\`${deprecatedKey}\` cannot be defined as a function in navigation options for \`${route.routeName}\` screen. \n`, 'Try replacing the following:', @@ -53,8 +50,7 @@ export default (screenOptions: *, route: NavigationRoute) => { } if (deprecatedKey && typeof screenOptions[deprecatedKey] === 'object') { - invariant( - false, + throw new Error( [ `Invalid key \`${deprecatedKey}\` defined in navigation options for \`${route.routeName}\` screen.`, '\n',