From 08d9d6dd8d222ddaca86db6647f124bd7dbaa28f Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 19 Mar 2021 17:02:23 +0800 Subject: [PATCH 1/3] fix(compiler-sfc): should not rewrite scope variable --- .../__snapshots__/compileScript.spec.ts.snap | 26 ++++++++++++ .../__tests__/compileScript.spec.ts | 23 +++++++++++ packages/compiler-sfc/src/compileScript.ts | 40 ++++++++++++++----- 3 files changed, 79 insertions(+), 10 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 59ef9b1b7ee..2293228e7fa 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -508,6 +508,32 @@ return { n, a, b, c } }" `; +exports[`SFC compile `) + expect(content).toMatch('console.log(a)') + expect(content).toMatch('console.log(b.value)') + expect(content).toMatch('console.log(c)') + assertCode(content) + }) + test('object destructure', () => { const { content, bindings } = compile(``) expect(content).toMatch('console.log(a)') expect(content).toMatch('console.log(b.value)') expect(content).toMatch('console.log(c)') expect(content).toMatch('console.log($d)') + expect(content).toMatch('console.log(d.value)') + expect(content).toMatch('console.log(e)') assertCode(content) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 0bf5fc7b1d1..864fcbbbf7c 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1383,19 +1383,22 @@ function walkIdentifiers( ) { onIdentifier(node, parent!, parentStack) } - } else if (node.type === 'BlockStatement' && isFunction(parent!)) { - node.body.forEach(p => { - if (p.type === 'VariableDeclaration') { - ;(walk as any)(p, { - enter(child: Node, parent: Node) { - if (child.type === 'Identifier') { - markScopeIdentifier(node, child, knownIds) - } - } - }) - } - }) } else if (isFunction(node)) { + // #3445 + // should not rewrite local variables sharing a name with a top-level ref + if (node.body.type === 'BlockStatement') { + node.body.body.forEach(p => { + if (p.type === 'VariableDeclaration') { + ;(walk as any)(p, { + enter(child: Node) { + if (child.type === 'Identifier') { + markScopeIdentifier(node, child, knownIds) + } + } + }) + } + }) + } // walk function expressions and add its arguments to known identifiers // so that we don't prefix them node.params.forEach(p =>