diff --git a/packages/compiler-core/src/babelUtils.ts b/packages/compiler-core/src/babelUtils.ts
index 6faa89e261e..fe0902809b9 100644
--- a/packages/compiler-core/src/babelUtils.ts
+++ b/packages/compiler-core/src/babelUtils.ts
@@ -4,7 +4,8 @@ import {
Node,
Function,
ObjectProperty,
- BlockStatement
+ BlockStatement,
+ Program
} from '@babel/types'
import { walk } from 'estree-walker'
@@ -149,16 +150,23 @@ export function walkFunctionParams(
}
export function walkBlockDeclarations(
- block: BlockStatement,
+ block: BlockStatement | Program,
onIdent: (node: Identifier) => void
) {
for (const stmt of block.body) {
if (stmt.type === 'VariableDeclaration') {
+ if (stmt.declare) continue
for (const decl of stmt.declarations) {
for (const id of extractIdentifiers(decl.id)) {
onIdent(id)
}
}
+ } else if (
+ stmt.type === 'FunctionDeclaration' ||
+ stmt.type === 'ClassDeclaration'
+ ) {
+ if (stmt.declare || !stmt.id) continue
+ onIdent(stmt.id)
}
}
}
diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap
deleted file mode 100644
index ed4605c1662..00000000000
--- a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap
+++ /dev/null
@@ -1,43 +0,0 @@
-// Jest Snapshot v1, https://goo.gl/fbAQLP
-
-exports[``)
+ expect(content).not.toMatch(`$ref(0)`)
+ expect(content).toMatch(`import { ref as _ref } from 'vue'`)
+ expect(content).toMatch(`let count = _ref(0)`)
+ expect(content).toMatch(`count.value++`)
+ expect(content).toMatch(`return ({ count })`)
+ assertCode(content)
+ })
+
+ test('usage with normal
+ `)
+ // should dedupe helper imports
+ expect(content).toMatch(`import { ref as _ref } from 'vue'`)
+
+ expect(content).toMatch(`let a = _ref(0)`)
+ expect(content).toMatch(`let b = _ref(0)`)
+
+ // root level ref binding declared in