diff --git a/compiler/forget/src/Inference/InlineUseMemo.ts b/compiler/forget/src/Inference/InlineUseMemo.ts index 582c0cfe4c..cc09373d1c 100644 --- a/compiler/forget/src/Inference/InlineUseMemo.ts +++ b/compiler/forget/src/Inference/InlineUseMemo.ts @@ -108,6 +108,13 @@ export function inlineUseMemo(fn: HIRFunction): void { ); } + if (body.loweredFunc.async || body.loweredFunc.generator) { + CompilerError.invariant( + "Did not expect useMemo callback to be async or a generator", + body.loc + ); + } + // We know this function is used for useMemo and can prune it later useMemoFunctions.add(lambda.identifier.id); diff --git a/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-async-callback.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-async-callback.expect.md new file mode 100644 index 0000000000..32323ebe93 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-async-callback.expect.md @@ -0,0 +1,22 @@ + +## Input + +```javascript +// @inlineUseMemo +function component(a, b) { + let x = useMemo(async () => { + await a; + }, []); + return x; +} + +``` + + +## Error + +``` +[ReactForget] Invariant: Did not expect useMemo callback to be async or a generator (3:5) +``` + + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-async-callback.js b/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-async-callback.js new file mode 100644 index 0000000000..abfaba0b4d --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-async-callback.js @@ -0,0 +1,7 @@ +// @inlineUseMemo +function component(a, b) { + let x = useMemo(async () => { + await a; + }, []); + return x; +} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.expect.md new file mode 100644 index 0000000000..0363040427 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.expect.md @@ -0,0 +1,32 @@ + +## Input + +```javascript +// @inlineUseMemo +function component(a, b) { + // we don't handle generators at all so this test isn't + // useful for now, but adding this test in case we do + // add support for generators in the future. + let x = useMemo(function* () { + yield a; + }, []); + return x; +} + +``` + + +## Error + +``` +[ReactForget] TodoError: (BuildHIR::lowerExpression) Handle YieldExpression expressions + 5 | // add support for generators in the future. + 6 | let x = useMemo(function* () { +> 7 | yield a; + | ^^^^^^^ + 8 | }, []); + 9 | return x; + 10 | } +``` + + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.js b/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.js new file mode 100644 index 0000000000..16844a352c --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.js @@ -0,0 +1,10 @@ +// @inlineUseMemo +function component(a, b) { + // we don't handle generators at all so this test isn't + // useful for now, but adding this test in case we do + // add support for generators in the future. + let x = useMemo(function* () { + yield a; + }, []); + return x; +}