Skip to content

Commit

Permalink
Support adding a human-readable deprecation hint
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpayton authored May 17, 2018
1 parent 70c662a commit 3fd06df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions utils/deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
* @param {?string} options.alternative Feature to use instead
* @param {?string} options.plugin Plugin name if it's a plugin feature
* @param {?string} options.link Link to documentation
* @param {?string} options.hint Additional message to help transition away from the deprecated feature.
*/
export function deprecated( feature, { version, alternative, plugin, link } = {} ) {
export function deprecated( feature, { version, alternative, plugin, link, hint } = {} ) {
const pluginMessage = plugin ? ` from ${ plugin }` : '';
const versionMessage = version ? `${ pluginMessage } in ${ version }` : '';
const useInsteadMessage = alternative ? ` Please use ${ alternative } instead.` : '';
const linkMessage = link ? ` See: ${ link }` : '';
const message = `${ feature } is deprecated and will be removed${ versionMessage }.${ useInsteadMessage }${ linkMessage }`;
const hintMessage = hint ? ` Note: ${ hint }` : '';
const message = `${ feature } is deprecated and will be removed${ versionMessage }.${ useInsteadMessage }${ linkMessage }${ hintMessage }`;

// eslint-disable-next-line no-console
console.warn( message );
Expand Down
13 changes: 13 additions & 0 deletions utils/test/deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,17 @@ describe( 'deprecated', () => {
'Eating meat is deprecated and will be removed from the earth in the future. Please use vegetables instead. See: https://en.wikipedia.org/wiki/Vegetarianism'
);
} );

it( 'should show a deprecation warning with a hint', () => {
deprecated( 'Eating meat', {
version: 'the future',
alternative: 'vegetables',
plugin: 'the earth',
hint: 'You may find it beneficial to transition gradually.',
} );

expect( console ).toHaveWarnedWith(
'Eating meat is deprecated and will be removed from the earth in the future. Please use vegetables instead. Note: You may find it beneficial to transition gradually.'
);
} );
} );

0 comments on commit 3fd06df

Please sign in to comment.