Skip to content

Commit a5c2ac9

Browse files
committed
Merge pull request react-bootstrap#1127 from taion/deprecation-warning-once
Only issue each deprecation warning once
2 parents 7e8ed85 + 36b29e4 commit a5c2ac9

4 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/CollapsibleMixin.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import React from 'react';
22
import TransitionEvents from './utils/TransitionEvents';
33
import deprecationWarning from './utils/deprecationWarning';
44

5-
let warned = false;
6-
75
const CollapsibleMixin = {
86

97
propTypes: {
@@ -25,10 +23,7 @@ const CollapsibleMixin = {
2523
},
2624

2725
componentWillMount(){
28-
if ( !warned ){
29-
deprecationWarning('CollapsibleMixin', 'Collapse Component');
30-
warned = true;
31-
}
26+
deprecationWarning('CollapsibleMixin', 'Collapse Component');
3227
},
3328

3429
componentWillUpdate(nextProps, nextState){

src/FadeMixin.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@ function getElementsAndSelf (root, classes){
1717
return els;
1818
}
1919

20-
let warned = false;
21-
2220
export default {
2321
componentWillMount(){
24-
if ( !warned ){
25-
deprecationWarning('FadeMixin', 'Fade Component');
26-
warned = true;
27-
}
22+
deprecationWarning('FadeMixin', 'Fade Component');
2823
},
2924

3025
_fadeIn() {

src/utils/deprecationWarning.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import warning from 'react/lib/warning';
22

3+
const warned = {};
4+
35
export default function deprecationWarning(oldname, newname, link) {
6+
const warnKey = `${oldname}\n${newname}`;
7+
if (warned[warnKey]) {
8+
return;
9+
}
10+
411
let message = `${oldname} is deprecated. Use ${newname} instead.`;
512

613
if (link) {
714
message += `\nYou can read more about it at ${link}`;
815
}
916

1017
warning(false, message);
18+
warned[warnKey] = true;
1119
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import deprecationWarning from '../../src/utils/deprecationWarning';
2+
3+
describe('deprecationWarning', function () {
4+
it('warns exactly once', function () {
5+
// console.warn has already been stubbed out by test setup.
6+
7+
deprecationWarning('foo', 'bar');
8+
expect(console.warn).to.have.been.calledOnce;
9+
10+
deprecationWarning('foo', 'bar');
11+
expect(console.warn).to.have.been.calledOnce;
12+
13+
// Reset the stub to avoid unhandled warnings.
14+
console.warn.reset();
15+
});
16+
});

0 commit comments

Comments
 (0)