From 1f14f194396bf9296a1046d3f680d6d318cd0e40 Mon Sep 17 00:00:00 2001 From: edison Date: Thu, 14 Apr 2022 08:06:58 +0800 Subject: [PATCH] fix(reactivity-transform): should not rewrite catch param (#5711) fix #5709 --- .../__snapshots__/reactivityTransform.spec.ts.snap | 5 +++++ .../__tests__/reactivityTransform.spec.ts | 6 ++++++ .../reactivity-transform/src/reactivityTransform.ts | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/packages/reactivity-transform/__tests__/__snapshots__/reactivityTransform.spec.ts.snap b/packages/reactivity-transform/__tests__/__snapshots__/reactivityTransform.spec.ts.snap index 73b5db93..99be2421 100644 --- a/packages/reactivity-transform/__tests__/__snapshots__/reactivityTransform.spec.ts.snap +++ b/packages/reactivity-transform/__tests__/__snapshots__/reactivityTransform.spec.ts.snap @@ -216,6 +216,11 @@ exports[`should not rewrite scope variable 1`] = ` console.log(d.value) console.log(e) } + let err = _ref(null) + try { + } catch (err) { + console.log(err) + } " `; diff --git a/packages/reactivity-transform/__tests__/reactivityTransform.spec.ts b/packages/reactivity-transform/__tests__/reactivityTransform.spec.ts index 9f24d266..ece6e351 100644 --- a/packages/reactivity-transform/__tests__/reactivityTransform.spec.ts +++ b/packages/reactivity-transform/__tests__/reactivityTransform.spec.ts @@ -222,12 +222,18 @@ test('should not rewrite scope variable', () => { console.log(d) console.log(e) } + let err = $ref(null) + try { + } catch (err) { + console.log(err) + } `) expect(code).toMatch('console.log(a)') expect(code).toMatch('console.log(b.value)') expect(code).toMatch('console.log(c)') expect(code).toMatch('console.log(d.value)') expect(code).toMatch('console.log(e)') + expect(code).toMatch('console.log(err)') assertCode(code) }) diff --git a/packages/reactivity-transform/src/reactivityTransform.ts b/packages/reactivity-transform/src/reactivityTransform.ts index a67d8432..809e3e4e 100644 --- a/packages/reactivity-transform/src/reactivityTransform.ts +++ b/packages/reactivity-transform/src/reactivityTransform.ts @@ -566,6 +566,16 @@ export function transformAST( return } + // catch param + if (node.type === 'CatchClause') { + scopeStack.push((currentScope = {})) + if (node.param && node.param.type === 'Identifier') { + registerBinding(node.param) + } + walkScope(node.body) + return + } + // non-function block scopes if (node.type === 'BlockStatement' && !isFunctionType(parent!)) { scopeStack.push((currentScope = {}))