@@ -1384,17 +1384,16 @@ const tests = {
1384
1384
errors : [
1385
1385
{
1386
1386
message :
1387
- // TODO: Ideally this would suggest props.foo?.bar.baz instead.
1388
- "React Hook useCallback has a missing dependency: 'props.foo.bar.baz'. " +
1387
+ "React Hook useCallback has a missing dependency: 'props.foo?.bar.baz'. " +
1389
1388
'Either include it or remove the dependency array.' ,
1390
1389
suggestions : [
1391
1390
{
1392
- desc : 'Update the dependencies array to be: [props.foo.bar.baz]' ,
1391
+ desc : 'Update the dependencies array to be: [props.foo? .bar.baz]' ,
1393
1392
output : normalizeIndent `
1394
1393
function MyComponent(props) {
1395
1394
useCallback(() => {
1396
1395
console.log(props.foo?.bar.baz);
1397
- }, [props.foo.bar.baz]);
1396
+ }, [props.foo? .bar.baz]);
1398
1397
}
1399
1398
` ,
1400
1399
} ,
@@ -1413,17 +1412,17 @@ const tests = {
1413
1412
errors : [
1414
1413
{
1415
1414
message :
1416
- // TODO: Ideally this would suggest props.foo?.bar?.baz instead.
1417
- "React Hook useCallback has a missing dependency: 'props.foo.bar.baz'. " +
1415
+ "React Hook useCallback has a missing dependency: 'props.foo?.bar?.baz'. " +
1418
1416
'Either include it or remove the dependency array.' ,
1419
1417
suggestions : [
1420
1418
{
1421
- desc : 'Update the dependencies array to be: [props.foo.bar.baz]' ,
1419
+ desc :
1420
+ 'Update the dependencies array to be: [props.foo?.bar?.baz]' ,
1422
1421
output : normalizeIndent `
1423
1422
function MyComponent(props) {
1424
1423
useCallback(() => {
1425
1424
console.log(props.foo?.bar?.baz);
1426
- }, [props.foo.bar.baz]);
1425
+ }, [props.foo? .bar? .baz]);
1427
1426
}
1428
1427
` ,
1429
1428
} ,
@@ -1442,17 +1441,16 @@ const tests = {
1442
1441
errors : [
1443
1442
{
1444
1443
message :
1445
- // TODO: Ideally this would suggest props.foo?.bar instead.
1446
- "React Hook useCallback has a missing dependency: 'props.foo.bar'. " +
1444
+ "React Hook useCallback has a missing dependency: 'props.foo?.bar'. " +
1447
1445
'Either include it or remove the dependency array.' ,
1448
1446
suggestions : [
1449
1447
{
1450
- desc : 'Update the dependencies array to be: [props.foo.bar]' ,
1448
+ desc : 'Update the dependencies array to be: [props.foo? .bar]' ,
1451
1449
output : normalizeIndent `
1452
1450
function MyComponent(props) {
1453
1451
useCallback(() => {
1454
1452
console.log(props.foo?.bar.toString());
1455
- }, [props.foo.bar]);
1453
+ }, [props.foo? .bar]);
1456
1454
}
1457
1455
` ,
1458
1456
} ,
@@ -2045,18 +2043,18 @@ const tests = {
2045
2043
errors : [
2046
2044
{
2047
2045
message :
2048
- "React Hook useEffect has a missing dependency: 'history.foo'. " +
2046
+ "React Hook useEffect has a missing dependency: 'history? .foo'. " +
2049
2047
'Either include it or remove the dependency array.' ,
2050
2048
suggestions : [
2051
2049
{
2052
- desc : 'Update the dependencies array to be: [history.foo]' ,
2050
+ desc : 'Update the dependencies array to be: [history? .foo]' ,
2053
2051
output : normalizeIndent `
2054
2052
function MyComponent({ history }) {
2055
2053
useEffect(() => {
2056
2054
return [
2057
2055
history?.foo
2058
2056
];
2059
- }, [history.foo]);
2057
+ }, [history? .foo]);
2060
2058
}
2061
2059
` ,
2062
2060
} ,
@@ -6986,7 +6984,6 @@ const testsTypescript = {
6986
6984
] ,
6987
6985
} ,
6988
6986
{
6989
- // https://github.com/facebook/react/issues/19243
6990
6987
code : normalizeIndent `
6991
6988
function MyComponent() {
6992
6989
const pizza = {};
@@ -7000,22 +6997,122 @@ const testsTypescript = {
7000
6997
errors : [
7001
6998
{
7002
6999
message :
7003
- "React Hook useEffect has missing dependencies: 'pizza.crust' and 'pizza.toppings'. " +
7000
+ "React Hook useEffect has missing dependencies: 'pizza.crust' and 'pizza? .toppings'. " +
7004
7001
'Either include them or remove the dependency array.' ,
7005
7002
suggestions : [
7006
7003
{
7007
- // TODO the description and suggestions should probably also
7008
- // preserve the optional chaining.
7009
7004
desc :
7010
- 'Update the dependencies array to be: [pizza.crust, pizza.toppings]' ,
7005
+ 'Update the dependencies array to be: [pizza.crust, pizza? .toppings]' ,
7011
7006
output : normalizeIndent `
7012
7007
function MyComponent() {
7013
7008
const pizza = {};
7014
7009
7015
7010
useEffect(() => ({
7016
7011
crust: pizza.crust,
7017
7012
toppings: pizza?.toppings,
7018
- }), [pizza.crust, pizza.toppings]);
7013
+ }), [pizza.crust, pizza?.toppings]);
7014
+ }
7015
+ ` ,
7016
+ } ,
7017
+ ] ,
7018
+ } ,
7019
+ ] ,
7020
+ } ,
7021
+ {
7022
+ code : normalizeIndent `
7023
+ function MyComponent() {
7024
+ const pizza = {};
7025
+
7026
+ useEffect(() => ({
7027
+ crust: pizza?.crust,
7028
+ density: pizza.crust.density,
7029
+ }), []);
7030
+ }
7031
+ ` ,
7032
+ errors : [
7033
+ {
7034
+ message :
7035
+ "React Hook useEffect has a missing dependency: 'pizza.crust'. " +
7036
+ 'Either include it or remove the dependency array.' ,
7037
+ suggestions : [
7038
+ {
7039
+ desc : 'Update the dependencies array to be: [pizza.crust]' ,
7040
+ output : normalizeIndent `
7041
+ function MyComponent() {
7042
+ const pizza = {};
7043
+
7044
+ useEffect(() => ({
7045
+ crust: pizza?.crust,
7046
+ density: pizza.crust.density,
7047
+ }), [pizza.crust]);
7048
+ }
7049
+ ` ,
7050
+ } ,
7051
+ ] ,
7052
+ } ,
7053
+ ] ,
7054
+ } ,
7055
+ {
7056
+ code : normalizeIndent `
7057
+ function MyComponent() {
7058
+ const pizza = {};
7059
+
7060
+ useEffect(() => ({
7061
+ crust: pizza.crust,
7062
+ density: pizza?.crust.density,
7063
+ }), []);
7064
+ }
7065
+ ` ,
7066
+ errors : [
7067
+ {
7068
+ message :
7069
+ "React Hook useEffect has a missing dependency: 'pizza.crust'. " +
7070
+ 'Either include it or remove the dependency array.' ,
7071
+ suggestions : [
7072
+ {
7073
+ desc : 'Update the dependencies array to be: [pizza.crust]' ,
7074
+ output : normalizeIndent `
7075
+ function MyComponent() {
7076
+ const pizza = {};
7077
+
7078
+ useEffect(() => ({
7079
+ crust: pizza.crust,
7080
+ density: pizza?.crust.density,
7081
+ }), [pizza.crust]);
7082
+ }
7083
+ ` ,
7084
+ } ,
7085
+ ] ,
7086
+ } ,
7087
+ ] ,
7088
+ } ,
7089
+ {
7090
+ code : normalizeIndent `
7091
+ function MyComponent() {
7092
+ const pizza = {};
7093
+
7094
+ useEffect(() => ({
7095
+ crust: pizza?.crust,
7096
+ density: pizza?.crust.density,
7097
+ }), []);
7098
+ }
7099
+ ` ,
7100
+ errors : [
7101
+ {
7102
+ message :
7103
+ "React Hook useEffect has a missing dependency: 'pizza?.crust'. " +
7104
+ 'Either include it or remove the dependency array.' ,
7105
+ suggestions : [
7106
+ {
7107
+ desc : 'Update the dependencies array to be: [pizza?.crust]' ,
7108
+ output : normalizeIndent `
7109
+ function MyComponent() {
7110
+ const pizza = {};
7111
+
7112
+ useEffect(() => ({
7113
+ crust: pizza?.crust,
7114
+ density: pizza?.crust.density,
7115
+ }), [pizza?.crust]);
7019
7116
}
7020
7117
` ,
7021
7118
} ,
0 commit comments