@samitouri / QOS-React-2 / commits / 5de02b881b

Support unused conditional/logical expressions assigned to variable

I noticed some instances of this error when running forget on an internal product. I previously fixed the case if a logical/conditional used only for side effects (not assigned to a variable) but the new cases were assigned to an unused variable. I double-checked and we’ve actually fixed all the steps after these invariants so we can just remove them and support these cases.

Joe Savona committed May 9, 2023 at 10:58 UTC 5de02b881b9a0c30e3bc5be352e54f4404ad72fa
7 files changed +93 -13
compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts
-13
@@ -6,7 +6,6 @@
6 */
7
8 import invariant from "invariant";
9 -import { CompilerError } from "../CompilerError";
9 import {
10 BasicBlock,
11 BlockId,
@@ -826,12 +825,6 @@ class Driver {
825 testBlock.terminal.alternate,
826 terminal.loc
827 );
829 - if (leftFinal.place.identifier !== right.place.identifier) {
830 - CompilerError.todo(
831 - "TODO: Support LogicalExpression whose value is unused",
832 - leftFinal.place.loc
833 - );
834 - }
828 const value: ReactiveLogicalValue = {
829 kind: "LogicalExpression",
830 operator: terminal.operator,
@@ -869,12 +862,6 @@ class Driver {
862 alternate: alternate.value,
863 loc: terminal.loc,
864 };
872 - if (consequent.place.identifier !== alternate.place.identifier) {
873 - CompilerError.todo(
874 - "TODO: Support ConditionalExpression whose value is unused",
875 - consequent.place.loc
876 - );
877 - }
865
866 return {
867 place: { ...consequent.place },
compiler/forget/src/__tests__/fixtures/compiler/unused-logical-assigned-to-variable.expect.md new
+26
@@ -0,0 +1,26 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + // unused!
7 + const obj = makeObject();
8 + const obj2 = makeObject();
9 + const _ = (obj.a ?? obj2.b) || props.c;
10 + return null;
11 +}
12 +
13 +```
14 +
15 +## Code
16 +
17 +```javascript
18 +function Component(props) {
19 + const obj = makeObject();
20 + const obj2 = makeObject();
21 + (obj.a ?? obj2.b) || props.c;
22 + return null;
23 +}
24 +
25 +```
26 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/unused-logical-assigned-to-variable.js new
+7
@@ -0,0 +1,7 @@
1 +function Component(props) {
2 + // unused!
3 + const obj = makeObject();
4 + const obj2 = makeObject();
5 + const _ = (obj.a ?? obj2.b) || props.c;
6 + return null;
7 +}
compiler/forget/src/__tests__/fixtures/compiler/unused-optional-method-assigned-to-variable.expect.md new
+24
@@ -0,0 +1,24 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + // unused!
7 + const obj = makeObject();
8 + const _ = obj.a?.b?.(props.c);
9 + return null;
10 +}
11 +
12 +```
13 +
14 +## Code
15 +
16 +```javascript
17 +function Component(props) {
18 + const obj = makeObject();
19 + obj.a?.b?.(props.c);
20 + return null;
21 +}
22 +
23 +```
24 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/unused-optional-method-assigned-to-variable.js new
+6
@@ -0,0 +1,6 @@
1 +function Component(props) {
2 + // unused!
3 + const obj = makeObject();
4 + const _ = obj.a?.b?.(props.c);
5 + return null;
6 +}
compiler/forget/src/__tests__/fixtures/compiler/unused-ternary-assigned-to-variable.expect.md new
+24
@@ -0,0 +1,24 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + // unused!
7 + const obj = makeObject();
8 + const _ = obj.a ? props.b : props.c;
9 + return null;
10 +}
11 +
12 +```
13 +
14 +## Code
15 +
16 +```javascript
17 +function Component(props) {
18 + const obj = makeObject();
19 + obj.a ? props.b : props.c;
20 + return null;
21 +}
22 +
23 +```
24 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/unused-ternary-assigned-to-variable.js new
+6
@@ -0,0 +1,6 @@
1 +function Component(props) {
2 + // unused!
3 + const obj = makeObject();
4 + const _ = obj.a ? props.b : props.c;
5 + return null;
6 +}