feat: add deprecation wanrnings

This commit is contained in:
satyajit.happy
2019-10-06 16:38:29 +02:00
parent db050915f1
commit 95476a4afa
2 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
import { NavigationStackOptions } from '../types';
type Validation = {
check: (o: NavigationStackOptions) => boolean;
deprecated: string;
updated: string;
};
let shownWarnings: string[] = [];
const validations: Validation[] = [
{
check: o => o.header === null,
deprecated: 'header: null',
updated: 'headerShown: false',
},
{
check: o => o.header != null && typeof o.header !== 'function',
deprecated: 'header: <SomeElement />',
updated: 'header: () => <SomeElement />',
},
{
check: o =>
o.headerTitle !== undefined &&
typeof o.header !== 'string' &&
typeof o.header !== 'function',
deprecated: 'headerTitle: <SomeElement />',
updated: 'headerTitle: () => <SomeElement />',
},
...['headerLeft', 'headerRight', 'headerBackground', 'backImage'].map(p => ({
check: (o: any) => o[p] !== undefined && typeof o[p] !== 'function',
deprecated: `${p}: <SomeElement />`,
updated: `${p}: () => <SomeElement />`,
})),
];
export default function validateDeprecatedOptions(
options: NavigationStackOptions
) {
const warnings: Validation[] = [];
// Validate options to show warnings for deprecations
validations.forEach(v => {
if (shownWarnings.includes(v.deprecated)) {
return;
}
if (v.check(options)) {
warnings.push(v);
shownWarnings.push(v.deprecated);
}
});
if (warnings.length) {
console.warn(
`Deprecation in 'navigationOptions':\n${warnings
.map(
v =>
`- '${v.deprecated}' will be removed in a future version. Use '${v.updated}' instead`
)
.join('\n')}`
);
}
}

View File

@@ -18,6 +18,7 @@ import {
ModalTransition,
} from '../../TransitionConfigs/TransitionPresets';
import { forNoAnimation } from '../../TransitionConfigs/HeaderStyleInterpolators';
import validateDeprecatedOptions from '../../utils/validateDeprecatedOptions';
import {
Layout,
HeaderMode,
@@ -344,6 +345,14 @@ export default class Stack extends React.Component<Props, State> {
? 1
: 0;
if (
process.env.NODE_ENV !== 'production' &&
scene.descriptor &&
scene.descriptor.options
) {
validateDeprecatedOptions(scene.descriptor.options);
}
const {
header,
headerShown,