From 5de02b881b9a0c30e3bc5be352e54f4404ad72fa Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Tue, 9 May 2023 10:58:51 -0700 Subject: [PATCH] Support unused conditional/logical expressions assigned to variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../ReactiveScopes/BuildReactiveFunction.ts | 13 ---------- ...sed-logical-assigned-to-variable.expect.md | 26 +++++++++++++++++++ .../unused-logical-assigned-to-variable.js | 7 +++++ ...onal-method-assigned-to-variable.expect.md | 24 +++++++++++++++++ ...ed-optional-method-assigned-to-variable.js | 6 +++++ ...sed-ternary-assigned-to-variable.expect.md | 24 +++++++++++++++++ .../unused-ternary-assigned-to-variable.js | 6 +++++ 7 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/unused-logical-assigned-to-variable.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/unused-logical-assigned-to-variable.js create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/unused-optional-method-assigned-to-variable.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/unused-optional-method-assigned-to-variable.js create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/unused-ternary-assigned-to-variable.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/unused-ternary-assigned-to-variable.js diff --git a/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts b/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts index 3d5427f212..2935b6c164 100644 --- a/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts +++ b/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts @@ -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 }, diff --git a/compiler/forget/src/__tests__/fixtures/compiler/unused-logical-assigned-to-variable.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/unused-logical-assigned-to-variable.expect.md new file mode 100644 index 0000000000..f5a49dd240 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/unused-logical-assigned-to-variable.expect.md @@ -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; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/unused-logical-assigned-to-variable.js b/compiler/forget/src/__tests__/fixtures/compiler/unused-logical-assigned-to-variable.js new file mode 100644 index 0000000000..509d7069cd --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/unused-logical-assigned-to-variable.js @@ -0,0 +1,7 @@ +function Component(props) { + // unused! + const obj = makeObject(); + const obj2 = makeObject(); + const _ = (obj.a ?? obj2.b) || props.c; + return null; +} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/unused-optional-method-assigned-to-variable.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/unused-optional-method-assigned-to-variable.expect.md new file mode 100644 index 0000000000..ca0a6c9433 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/unused-optional-method-assigned-to-variable.expect.md @@ -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; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/unused-optional-method-assigned-to-variable.js b/compiler/forget/src/__tests__/fixtures/compiler/unused-optional-method-assigned-to-variable.js new file mode 100644 index 0000000000..250da966e1 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/unused-optional-method-assigned-to-variable.js @@ -0,0 +1,6 @@ +function Component(props) { + // unused! + const obj = makeObject(); + const _ = obj.a?.b?.(props.c); + return null; +} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/unused-ternary-assigned-to-variable.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/unused-ternary-assigned-to-variable.expect.md new file mode 100644 index 0000000000..374d8d9103 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/unused-ternary-assigned-to-variable.expect.md @@ -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; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/unused-ternary-assigned-to-variable.js b/compiler/forget/src/__tests__/fixtures/compiler/unused-ternary-assigned-to-variable.js new file mode 100644 index 0000000000..957a723f57 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/unused-ternary-assigned-to-variable.js @@ -0,0 +1,6 @@ +function Component(props) { + // unused! + const obj = makeObject(); + const _ = obj.a ? props.b : props.c; + return null; +}