Minimal repros of product patterns
Joe Savona committed
Feb 16, 2023 at 08:57 UTC
93775440a635f0a461d40b888e6be7666c8d2385
4 files changed
+106
compiler/forget/src/__tests__/fixtures/hir/inadvertent-mutability-readonly-class.expect.md
new
+41
@@ -0,0 +1,41 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ const env = useRelayEnvironment();
7
+ // Note: this is a class has no mutable methods, ie it always treats `this` as readonly
8
+ const mutator = new Mutator(env);
9
+
10
+ useOtherHook();
11
+
12
+ // `x` should be independently memoizeable, since foo(x, mutator) cannot mutate
13
+ // the mutator.
14
+ const x = {};
15
+ foo(x, mutator);
16
+ return x;
17
+}
18
+
19
+class Mutator {}
20
+
21
+```
22
+
23
+## Code
24
+
25
+```javascript
26
+function Component(props) {
27
+ const env = useRelayEnvironment();
28
+
29
+ const mutator = new Mutator(env);
30
+
31
+ useOtherHook();
32
+
33
+ const x = {};
34
+ foo(x, mutator);
35
+ return x;
36
+}
37
+
38
+class Mutator {}
39
+
40
+```
41
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/inadvertent-mutability-readonly-class.js
new
+15
@@ -0,0 +1,15 @@
1
+function Component(props) {
2
+ const env = useRelayEnvironment();
3
+ // Note: this is a class has no mutable methods, ie it always treats `this` as readonly
4
+ const mutator = new Mutator(env);
5
+
6
+ useOtherHook();
7
+
8
+ // `x` should be independently memoizeable, since foo(x, mutator) cannot mutate
9
+ // the mutator.
10
+ const x = {};
11
+ foo(x, mutator);
12
+ return x;
13
+}
14
+
15
+class Mutator {}
compiler/forget/src/__tests__/fixtures/hir/inadvertent-mutability-readonly-lambda.expect.md
new
+37
@@ -0,0 +1,37 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ const [value, setValue] = useState(null);
7
+ // NOTE: this lambda does not capture any mutable values (only the state setter)
8
+ // and thus should be treated as readonly
9
+ const onChange = (e) => setX((value) => value + e.target.value);
10
+
11
+ useOtherHook();
12
+
13
+ // x should be independently memoizeable, since foo(x, onChange) cannot modify onChange
14
+ const x = {};
15
+ foo(x, onChange);
16
+ return x;
17
+}
18
+
19
+```
20
+
21
+## Code
22
+
23
+```javascript
24
+function Component(props) {
25
+ useState(null);
26
+
27
+ const onChange = (e) => setX((value) => value + e.target.value);
28
+
29
+ useOtherHook();
30
+
31
+ const x = {};
32
+ foo(x, onChange);
33
+ return x;
34
+}
35
+
36
+```
37
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/inadvertent-mutability-readonly-lambda.js
new
+13
@@ -0,0 +1,13 @@
1
+function Component(props) {
2
+ const [value, setValue] = useState(null);
3
+ // NOTE: this lambda does not capture any mutable values (only the state setter)
4
+ // and thus should be treated as readonly
5
+ const onChange = (e) => setX((value) => value + e.target.value);
6
+
7
+ useOtherHook();
8
+
9
+ // x should be independently memoizeable, since foo(x, onChange) cannot modify onChange
10
+ const x = {};
11
+ foo(x, onChange);
12
+ return x;
13
+}