Skip to content

Commit 6d6cc90

Browse files
committed
fix(compiler-sfc): fix local var access check for bindings in normal script
fix #4644
1 parent 2476eaa commit 6d6cc90

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

packages/compiler-sfc/__tests__/compileScript.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,19 @@ const emit = defineEmits(['a', 'b'])
11811181
defineEmits([bar])
11821182
</script>`)
11831183
).toThrow(`cannot reference locally declared variables`)
1184+
1185+
// #4644
1186+
expect(() =>
1187+
compile(`
1188+
<script>const bar = 1</script>
1189+
<script setup>
1190+
defineProps({
1191+
foo: {
1192+
default: () => bar
1193+
}
1194+
})
1195+
</script>`)
1196+
).not.toThrow(`cannot reference locally declared variables`)
11841197
})
11851198

11861199
test('should allow defineProps/Emit() referencing scope var', () => {

packages/compiler-sfc/src/compileScript.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ export function compileScript(
239239
const helperImports: Set<string> = new Set()
240240
const userImports: Record<string, ImportBinding> = Object.create(null)
241241
const userImportAlias: Record<string, string> = Object.create(null)
242+
const scriptBindings: Record<string, BindingTypes> = Object.create(null)
242243
const setupBindings: Record<string, BindingTypes> = Object.create(null)
243244

244245
let defaultExport: Node | undefined
@@ -739,15 +740,15 @@ export function compileScript(
739740
}
740741
}
741742
if (node.declaration) {
742-
walkDeclaration(node.declaration, setupBindings, userImportAlias)
743+
walkDeclaration(node.declaration, scriptBindings, userImportAlias)
743744
}
744745
} else if (
745746
(node.type === 'VariableDeclaration' ||
746747
node.type === 'FunctionDeclaration' ||
747748
node.type === 'ClassDeclaration') &&
748749
!node.declare
749750
) {
750-
walkDeclaration(node, setupBindings, userImportAlias)
751+
walkDeclaration(node, scriptBindings, userImportAlias)
751752
}
752753
}
753754

@@ -1070,6 +1071,9 @@ export function compileScript(
10701071
? BindingTypes.SETUP_CONST
10711072
: BindingTypes.SETUP_MAYBE_REF
10721073
}
1074+
for (const key in scriptBindings) {
1075+
bindingMetadata[key] = scriptBindings[key]
1076+
}
10731077
for (const key in setupBindings) {
10741078
bindingMetadata[key] = setupBindings[key]
10751079
}
@@ -1198,8 +1202,11 @@ export function compileScript(
11981202
returned = `() => {}`
11991203
}
12001204
} else {
1201-
// return bindings from setup
1202-
const allBindings: Record<string, any> = { ...setupBindings }
1205+
// return bindings from script and script setup
1206+
const allBindings: Record<string, any> = {
1207+
...scriptBindings,
1208+
...setupBindings
1209+
}
12031210
for (const key in userImports) {
12041211
if (!userImports[key].isType && userImports[key].isUsedInTemplate) {
12051212
allBindings[key] = true

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy