remove ineffective invariant usages, fixes #2258 (#2693)

* remove ineffective invariant usages

* fix flow err

* fix tests

* fix tests

* use throw
This commit is contained in:
Vojtech Novak 2017-10-05 12:59:13 +02:00 committed by Lorenzo Sciandra
parent c08be7fb43
commit e4a7b7e073
6 changed files with 47 additions and 51 deletions

View File

@ -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<S: *, O>(
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) {

View File

@ -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 },

View File

@ -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,

View File

@ -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.`);
}

View File

@ -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" +

View File

@ -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',