From 52875a72fef2722347e4aa37ed5da30d7bd4f31e Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Wed, 6 Mar 2024 21:50:03 -0800 Subject: [PATCH] Invariant if codegen tries to emit a temporary as an identifier MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For T181507827 — adds an invariant in codegen when emitting identifiers to ensure that we only create babel Identifier nodes for nodes that the compiler has explicitly promoted to valid, named identifiers. This means that we'll fail for unnamed temporaries (previously caught), as well as promoted temporaries that somehow didn't get renamed by RenameVariables (newly caught). --- .../ReactiveScopes/CodegenReactiveFunction.ts | 15 ++++++----- ...catch-within-function-expression.expect.md | 25 +++++++++++++++++++ ...do-try-catch-within-function-expression.js | 10 ++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-try-catch-within-function-expression.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-try-catch-within-function-expression.js diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts index 77b9414706..1f75d32067 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -2060,11 +2060,14 @@ function codegenPlace(cx: Context, place: Place): t.Expression | t.JSXText { } function convertIdentifier(identifier: Identifier): t.Identifier { - CompilerError.invariant(identifier.name !== null, { - reason: `Expected temporaries to be promoted to named identifiers in an earlier pass`, - loc: GeneratedSource, - description: `identifier ${identifier.id} is unnamed`, - suggestions: null, - }); + CompilerError.invariant( + identifier.name !== null && identifier.name.kind === "named", + { + reason: `Expected temporaries to be promoted to named identifiers in an earlier pass`, + loc: GeneratedSource, + description: `identifier ${identifier.id} is unnamed`, + suggestions: null, + } + ); return t.identifier(identifier.name.value); } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-try-catch-within-function-expression.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-try-catch-within-function-expression.expect.md new file mode 100644 index 0000000000..b9e0184b64 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-try-catch-within-function-expression.expect.md @@ -0,0 +1,25 @@ + +## Input + +```javascript +function Component(props) { + const callback = () => { + try { + return []; + } catch (e) { + return; + } + }; + return callback(); +} + +``` + + +## Error + +``` +[ReactForget] Invariant: Expected temporaries to be promoted to named identifiers in an earlier pass. identifier 16 is unnamed +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-try-catch-within-function-expression.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-try-catch-within-function-expression.js new file mode 100644 index 0000000000..69c394c826 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-try-catch-within-function-expression.js @@ -0,0 +1,10 @@ +function Component(props) { + const callback = () => { + try { + return []; + } catch (e) { + return; + } + }; + return callback(); +}