Skip to content

Commit

Permalink
Add 'since' option to deprecated function (#30017)
Browse files Browse the repository at this point in the history
* Add 'since' option to deprecated function

See #23373.

* Fixes formatting issues
  • Loading branch information
Mamaduka authored Mar 19, 2021
1 parent b2c26f3 commit a4b2b42
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
6 changes: 4 additions & 2 deletions packages/deprecated/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ _Usage_
import deprecated from '@wordpress/deprecated';

deprecated( 'Eating meat', {
version: 'the future',
since: '2019.01.01'
version: '2020.01.01',
alternative: 'vegetables',
plugin: 'the earth',
hint: 'You may find it beneficial to transition gradually.',
} );

// Logs: '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.'
// Logs: 'Eating meat is deprecated since version 2019.01.01 and will be removed from the earth in version 2020.01.01. Please use vegetables instead. Note: You may find it beneficial to transition gradually.'
```

_Parameters_

- _feature_ `string`: Name of the deprecated feature.
- _options_ `[Object]`: Personalisation options
- _options.since_ `[string]`: Version in which the feature was deprecated.
- _options.version_ `[string]`: Version in which the feature will be removed.
- _options.alternative_ `[string]`: Feature to use instead
- _options.plugin_ `[string]`: Plugin name if it's a plugin feature
Expand Down
12 changes: 8 additions & 4 deletions packages/deprecated/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const logged = Object.create( null );
*
* @param {string} feature Name of the deprecated feature.
* @param {Object} [options] Personalisation options
* @param {string} [options.since] Version in which the feature was deprecated.
* @param {string} [options.version] Version in which the feature will be removed.
* @param {string} [options.alternative] Feature to use instead
* @param {string} [options.plugin] Plugin name if it's a plugin feature
Expand All @@ -27,19 +28,21 @@ export const logged = Object.create( null );
* import deprecated from '@wordpress/deprecated';
*
* deprecated( 'Eating meat', {
* version: 'the future',
* since: '2019.01.01'
* version: '2020.01.01',
* alternative: 'vegetables',
* plugin: 'the earth',
* hint: 'You may find it beneficial to transition gradually.',
* } );
*
* // Logs: '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.'
* // Logs: 'Eating meat is deprecated since version 2019.01.01 and will be removed from the earth in version 2020.01.01. Please use vegetables instead. Note: You may find it beneficial to transition gradually.'
* ```
*/
export default function deprecated( feature, options = {} ) {
const { version, alternative, plugin, link, hint } = options;
const { since, version, alternative, plugin, link, hint } = options;

const pluginMessage = plugin ? ` from ${ plugin }` : '';
const sinceMessage = since ? ` since version ${ since }` : '';
const versionMessage = version
? ` and will be removed${ pluginMessage } in version ${ version }`
: '';
Expand All @@ -48,7 +51,7 @@ export default function deprecated( feature, options = {} ) {
: '';
const linkMessage = link ? ` See: ${ link }` : '';
const hintMessage = hint ? ` Note: ${ hint }` : '';
const message = `${ feature } is deprecated${ versionMessage }.${ useInsteadMessage }${ linkMessage }${ hintMessage }`;
const message = `${ feature } is deprecated${ sinceMessage }${ versionMessage }.${ useInsteadMessage }${ linkMessage }${ hintMessage }`;

// Skip if already logged.
if ( message in logged ) {
Expand All @@ -60,6 +63,7 @@ export default function deprecated( feature, options = {} ) {
*
* @param {string} feature Name of the deprecated feature.
* @param {?Object} options Personalisation options
* @param {string} options.since Version in which the feature was deprecated.
* @param {?string} options.version Version in which the feature will be removed.
* @param {?string} options.alternative Feature to use instead
* @param {?string} options.plugin Plugin name if it's a plugin feature
Expand Down
14 changes: 12 additions & 2 deletions packages/deprecated/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ describe( 'deprecated', () => {
expect( console ).toHaveWarnedWith( 'Eating meat is deprecated.' );
} );

it( 'should show a deprecation warning with a since', () => {
deprecated( 'Eating meat', { since: '2019.01.01' } );

expect( console ).toHaveWarnedWith(
'Eating meat is deprecated since version 2019.01.01.'
);
} );

it( 'should show a deprecation warning with a version', () => {
deprecated( 'Eating meat', { version: '2020.01.01' } );

Expand All @@ -31,12 +39,13 @@ describe( 'deprecated', () => {

it( 'should show a deprecation warning with an alternative', () => {
deprecated( 'Eating meat', {
since: '2019.01.01',
version: '2020.01.01',
alternative: 'vegetables',
} );

expect( console ).toHaveWarnedWith(
'Eating meat is deprecated and will be removed in version 2020.01.01. Please use vegetables instead.'
'Eating meat is deprecated since version 2019.01.01 and will be removed in version 2020.01.01. Please use vegetables instead.'
);
} );

Expand Down Expand Up @@ -67,14 +76,15 @@ describe( 'deprecated', () => {

it( 'should show a deprecation warning with a hint', () => {
deprecated( 'Eating meat', {
since: '2019.01.01',
version: '2020.01.01',
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 version 2020.01.01. Please use vegetables instead. Note: You may find it beneficial to transition gradually.'
'Eating meat is deprecated since version 2019.01.01 and will be removed from the earth in version 2020.01.01. Please use vegetables instead. Note: You may find it beneficial to transition gradually.'
);
} );

Expand Down

0 comments on commit a4b2b42

Please sign in to comment.