@@ -433,6 +433,19 @@ const tests = {
433
433
});
434
434
` ,
435
435
} ,
436
+ {
437
+ // This is not ideal but warning would likely create
438
+ // too many false positives. We do, however, prevent
439
+ // direct assignments.
440
+ code : `
441
+ function MyComponent(props) {
442
+ let obj = {};
443
+ useEffect(() => {
444
+ obj.foo = true;
445
+ }, [obj]);
446
+ }
447
+ ` ,
448
+ } ,
436
449
] ,
437
450
invalid : [
438
451
{
@@ -891,7 +904,6 @@ const tests = {
891
904
] ,
892
905
} ,
893
906
{
894
- // only: true,
895
907
code : `
896
908
function MyComponent(props) {
897
909
useEffect(() => {
@@ -1608,6 +1620,128 @@ const tests = {
1608
1620
'Either include them or remove the dependency array.' ,
1609
1621
] ,
1610
1622
} ,
1623
+ {
1624
+ code : `
1625
+ function MyComponent(props) {
1626
+ let value;
1627
+ let value2;
1628
+ let value3;
1629
+ let asyncValue;
1630
+ useEffect(() => {
1631
+ value = 42;
1632
+ value2 = 100;
1633
+ value = 43;
1634
+ console.log(value2);
1635
+ console.log(value3);
1636
+ setTimeout(() => {
1637
+ asyncValue = 100;
1638
+ });
1639
+ }, []);
1640
+ }
1641
+ ` ,
1642
+ // This is a separate warning unrelated to others.
1643
+ // We could've made a separate rule for it but it's rare enough to name it.
1644
+ // No autofix suggestion because the intent isn't clear.
1645
+ output : `
1646
+ function MyComponent(props) {
1647
+ let value;
1648
+ let value2;
1649
+ let value3;
1650
+ let asyncValue;
1651
+ useEffect(() => {
1652
+ value = 42;
1653
+ value2 = 100;
1654
+ value = 43;
1655
+ console.log(value2);
1656
+ console.log(value3);
1657
+ setTimeout(() => {
1658
+ asyncValue = 100;
1659
+ });
1660
+ }, []);
1661
+ }
1662
+ ` ,
1663
+ errors : [
1664
+ // value
1665
+ `Assignments to the 'value' variable from inside a React useEffect Hook ` +
1666
+ `will not persist between re-renders. ` +
1667
+ `If it's only needed by this Hook, move the variable inside it. ` +
1668
+ `Alternatively, declare a ref with the useRef Hook, ` +
1669
+ `and keep the mutable value in its 'current' property.` ,
1670
+ // value2
1671
+ `Assignments to the 'value2' variable from inside a React useEffect Hook ` +
1672
+ `will not persist between re-renders. ` +
1673
+ `If it's only needed by this Hook, move the variable inside it. ` +
1674
+ `Alternatively, declare a ref with the useRef Hook, ` +
1675
+ `and keep the mutable value in its 'current' property.` ,
1676
+ // asyncValue
1677
+ `Assignments to the 'asyncValue' variable from inside a React useEffect Hook ` +
1678
+ `will not persist between re-renders. ` +
1679
+ `If it's only needed by this Hook, move the variable inside it. ` +
1680
+ `Alternatively, declare a ref with the useRef Hook, ` +
1681
+ `and keep the mutable value in its 'current' property.` ,
1682
+ ] ,
1683
+ } ,
1684
+ {
1685
+ code : `
1686
+ function MyComponent(props) {
1687
+ let value;
1688
+ let value2;
1689
+ let value3;
1690
+ let asyncValue;
1691
+ useEffect(() => {
1692
+ value = 42;
1693
+ value2 = 100;
1694
+ value = 43;
1695
+ console.log(value2);
1696
+ console.log(value3);
1697
+ setTimeout(() => {
1698
+ asyncValue = 100;
1699
+ });
1700
+ }, [value, value2, value3]);
1701
+ }
1702
+ ` ,
1703
+ // This is a separate warning unrelated to others.
1704
+ // We could've made a separate rule for it but it's rare enough to name it.
1705
+ // No autofix suggestion because the intent isn't clear.
1706
+ output : `
1707
+ function MyComponent(props) {
1708
+ let value;
1709
+ let value2;
1710
+ let value3;
1711
+ let asyncValue;
1712
+ useEffect(() => {
1713
+ value = 42;
1714
+ value2 = 100;
1715
+ value = 43;
1716
+ console.log(value2);
1717
+ console.log(value3);
1718
+ setTimeout(() => {
1719
+ asyncValue = 100;
1720
+ });
1721
+ }, [value, value2, value3]);
1722
+ }
1723
+ ` ,
1724
+ errors : [
1725
+ // value
1726
+ `Assignments to the 'value' variable from inside a React useEffect Hook ` +
1727
+ `will not persist between re-renders. ` +
1728
+ `If it's only needed by this Hook, move the variable inside it. ` +
1729
+ `Alternatively, declare a ref with the useRef Hook, ` +
1730
+ `and keep the mutable value in its 'current' property.` ,
1731
+ // value2
1732
+ `Assignments to the 'value2' variable from inside a React useEffect Hook ` +
1733
+ `will not persist between re-renders. ` +
1734
+ `If it's only needed by this Hook, move the variable inside it. ` +
1735
+ `Alternatively, declare a ref with the useRef Hook, ` +
1736
+ `and keep the mutable value in its 'current' property.` ,
1737
+ // asyncValue
1738
+ `Assignments to the 'asyncValue' variable from inside a React useEffect Hook ` +
1739
+ `will not persist between re-renders. ` +
1740
+ `If it's only needed by this Hook, move the variable inside it. ` +
1741
+ `Alternatively, declare a ref with the useRef Hook, ` +
1742
+ `and keep the mutable value in its 'current' property.` ,
1743
+ ] ,
1744
+ } ,
1611
1745
] ,
1612
1746
} ;
1613
1747
0 commit comments