@@ -2825,6 +2825,61 @@ ruleTester.run('no-unused-prop-types', rule, {
2825
2825
}
2826
2826
MyComponent.propTypes = { * other() {} };
2827
2827
`
2828
+ } , {
2829
+ // Sanity test coverage for new UNSAFE_componentWillReceiveProps lifecycles
2830
+ code : [ `
2831
+ class Hello extends Component {
2832
+ static propTypes = {
2833
+ something: PropTypes.bool
2834
+ };
2835
+ UNSAFE_componentWillReceiveProps (nextProps) {
2836
+ const {something} = nextProps;
2837
+ doSomething(something);
2838
+ }
2839
+ }
2840
+ ` ] . join ( '\n' ) ,
2841
+ settings : { react : { version : '16.3.0' } } ,
2842
+ parser : 'babel-eslint'
2843
+ } , {
2844
+ // Destructured props in the `UNSAFE_componentWillUpdate` method shouldn't throw errors
2845
+ code : [ `
2846
+ class Hello extends Component {
2847
+ static propTypes = {
2848
+ something: PropTypes.bool
2849
+ };
2850
+ UNSAFE_componentWillUpdate (nextProps, nextState) {
2851
+ const {something} = nextProps;
2852
+ return something;
2853
+ }
2854
+ }
2855
+ ` ] . join ( '\n' ) ,
2856
+ settings : { react : { version : '16.3.0' } } ,
2857
+ parser : 'babel-eslint'
2858
+ } , {
2859
+ // Simple test of new static getDerivedStateFromProps lifecycle
2860
+ code : [ `
2861
+ class MyComponent extends React.Component {
2862
+ static propTypes = {
2863
+ defaultValue: 'bar'
2864
+ };
2865
+ state = {
2866
+ currentValue: null
2867
+ };
2868
+ static getDerivedStateFromProps(nextProps, prevState) {
2869
+ if (prevState.currentValue === null) {
2870
+ return {
2871
+ currentValue: nextProps.defaultValue,
2872
+ }
2873
+ }
2874
+ return null;
2875
+ }
2876
+ render() {
2877
+ return <div>{ this.state.currentValue }</div>
2878
+ }
2879
+ }
2880
+ ` ] . join ( '\n' ) ,
2881
+ settings : { react : { version : '16.3.0' } } ,
2882
+ parser : 'babel-eslint'
2828
2883
}
2829
2884
] ,
2830
2885
@@ -4394,6 +4449,67 @@ ruleTester.run('no-unused-prop-types', rule, {
4394
4449
errors : [ {
4395
4450
message : '\'lastname\' PropType is defined but prop is never used'
4396
4451
} ]
4452
+ } , {
4453
+ code : [ `
4454
+ class Hello extends Component {
4455
+ static propTypes = {
4456
+ something: PropTypes.bool
4457
+ };
4458
+ UNSAFE_componentWillReceiveProps (nextProps) {
4459
+ const {something} = nextProps;
4460
+ doSomething(something);
4461
+ }
4462
+ }
4463
+ ` ] . join ( '\n' ) ,
4464
+ settings : { react : { version : '16.2.0' } } ,
4465
+ parser : 'babel-eslint' ,
4466
+ errors : [ {
4467
+ message : '\'something\' PropType is defined but prop is never used'
4468
+ } ]
4469
+ } , {
4470
+ code : [ `
4471
+ class Hello extends Component {
4472
+ static propTypes = {
4473
+ something: PropTypes.bool
4474
+ };
4475
+ UNSAFE_componentWillUpdate (nextProps, nextState) {
4476
+ const {something} = nextProps;
4477
+ return something;
4478
+ }
4479
+ }
4480
+ ` ] . join ( '\n' ) ,
4481
+ settings : { react : { version : '16.2.0' } } ,
4482
+ parser : 'babel-eslint' ,
4483
+ errors : [ {
4484
+ message : '\'something\' PropType is defined but prop is never used'
4485
+ } ]
4486
+ } , {
4487
+ code : [ `
4488
+ class MyComponent extends React.Component {
4489
+ static propTypes = {
4490
+ defaultValue: 'bar'
4491
+ };
4492
+ state = {
4493
+ currentValue: null
4494
+ };
4495
+ static getDerivedStateFromProps(nextProps, prevState) {
4496
+ if (prevState.currentValue === null) {
4497
+ return {
4498
+ currentValue: nextProps.defaultValue,
4499
+ }
4500
+ }
4501
+ return null;
4502
+ }
4503
+ render() {
4504
+ return <div>{ this.state.currentValue }</div>
4505
+ }
4506
+ }
4507
+ ` ] . join ( '\n' ) ,
4508
+ settings : { react : { version : '16.2.0' } } ,
4509
+ parser : 'babel-eslint' ,
4510
+ errors : [ {
4511
+ message : '\'defaultValue\' PropType is defined but prop is never used'
4512
+ } ]
4397
4513
}
4398
4514
4399
4515
/* , {
0 commit comments