Skip to content

Commit

Permalink
Deprecated: Log message maximum once per session (#7840)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored and gziolo committed Jul 10, 2018
1 parent afd13cc commit feb1440
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/deprecated/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Object map tracking messages which have been logged, for use in ensuring a
* message is only logged once.
*
* @type {Object}
*/
export const logged = Object.create( null );

/**
* Logs a message to notify developers about a deprecated feature.
*
Expand All @@ -17,6 +25,13 @@ export default function deprecated( feature, { version, alternative, plugin, lin
const hintMessage = hint ? ` Note: ${ hint }` : '';
const message = `${ feature } is deprecated and will be removed${ versionMessage }.${ useInsteadMessage }${ linkMessage }${ hintMessage }`;

// Skip if already logged.
if ( message in logged ) {
return;
}

// eslint-disable-next-line no-console
console.warn( message );

logged[ message ] = true;
}
17 changes: 16 additions & 1 deletion packages/deprecated/src/test/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/**
* Internal dependencies
*/
import deprecated from '../';
import deprecated, { logged } from '../';

describe( 'deprecated', () => {
afterEach( () => {
for ( const key in logged ) {
delete logged[ key ];
}
} );

it( 'should show a deprecation warning', () => {
deprecated( 'Eating meat' );

Expand Down Expand Up @@ -65,4 +71,13 @@ describe( 'deprecated', () => {
'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.'
);
} );

it( 'should show a message once', () => {
deprecated( 'Eating meat' );
deprecated( 'Eating meat' );

expect( console ).toHaveWarned();
// eslint-disable-next-line no-console
expect( console.warn ).toHaveBeenCalledTimes( 1 );
} );
} );

0 comments on commit feb1440

Please sign in to comment.