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.
This commit is contained in:
Joe Savona
2023-05-09 10:58:51 -07:00
parent 50c3bd9d98
commit 5de02b881b
7 changed files with 93 additions and 13 deletions
@@ -6,7 +6,6 @@
*/
import invariant from "invariant";
import { CompilerError } from "../CompilerError";
import {
BasicBlock,
BlockId,
@@ -826,12 +825,6 @@ class Driver {
testBlock.terminal.alternate,
terminal.loc
);
if (leftFinal.place.identifier !== right.place.identifier) {
CompilerError.todo(
"TODO: Support LogicalExpression whose value is unused",
leftFinal.place.loc
);
}
const value: ReactiveLogicalValue = {
kind: "LogicalExpression",
operator: terminal.operator,
@@ -869,12 +862,6 @@ class Driver {
alternate: alternate.value,
loc: terminal.loc,
};
if (consequent.place.identifier !== alternate.place.identifier) {
CompilerError.todo(
"TODO: Support ConditionalExpression whose value is unused",
consequent.place.loc
);
}
return {
place: { ...consequent.place },
@@ -0,0 +1,26 @@
## Input
```javascript
function Component(props) {
// unused!
const obj = makeObject();
const obj2 = makeObject();
const _ = (obj.a ?? obj2.b) || props.c;
return null;
}
```
## Code
```javascript
function Component(props) {
const obj = makeObject();
const obj2 = makeObject();
(obj.a ?? obj2.b) || props.c;
return null;
}
```
@@ -0,0 +1,7 @@
function Component(props) {
// unused!
const obj = makeObject();
const obj2 = makeObject();
const _ = (obj.a ?? obj2.b) || props.c;
return null;
}
@@ -0,0 +1,24 @@
## Input
```javascript
function Component(props) {
// unused!
const obj = makeObject();
const _ = obj.a?.b?.(props.c);
return null;
}
```
## Code
```javascript
function Component(props) {
const obj = makeObject();
obj.a?.b?.(props.c);
return null;
}
```
@@ -0,0 +1,6 @@
function Component(props) {
// unused!
const obj = makeObject();
const _ = obj.a?.b?.(props.c);
return null;
}
@@ -0,0 +1,24 @@
## Input
```javascript
function Component(props) {
// unused!
const obj = makeObject();
const _ = obj.a ? props.b : props.c;
return null;
}
```
## Code
```javascript
function Component(props) {
const obj = makeObject();
obj.a ? props.b : props.c;
return null;
}
```
@@ -0,0 +1,6 @@
function Component(props) {
// unused!
const obj = makeObject();
const _ = obj.a ? props.b : props.c;
return null;
}