@@ -803,7 +803,7 @@ export function compileScript(
803
803
if ( ! node ) return
804
804
walkIdentifiers ( node , id => {
805
805
const binding = setupBindings [ id . name ]
806
- if ( binding && ( binding !== BindingTypes . LITERAL_CONST || ! hoistStatic ) ) {
806
+ if ( binding && binding !== BindingTypes . LITERAL_CONST ) {
807
807
error (
808
808
`\`${ method } ()\` in <script setup> cannot reference locally ` +
809
809
`declared variables because it will be hoisted outside of the ` +
@@ -1258,7 +1258,13 @@ export function compileScript(
1258
1258
}
1259
1259
}
1260
1260
if ( node . declaration ) {
1261
- walkDeclaration ( node . declaration , scriptBindings , vueImportAliases )
1261
+ walkDeclaration (
1262
+ 'script' ,
1263
+ node . declaration ,
1264
+ scriptBindings ,
1265
+ vueImportAliases ,
1266
+ hoistStatic
1267
+ )
1262
1268
}
1263
1269
} else if (
1264
1270
( node . type === 'VariableDeclaration' ||
@@ -1267,7 +1273,13 @@ export function compileScript(
1267
1273
node . type === 'TSEnumDeclaration' ) &&
1268
1274
! node . declare
1269
1275
) {
1270
- walkDeclaration ( node , scriptBindings , vueImportAliases )
1276
+ walkDeclaration (
1277
+ 'script' ,
1278
+ node ,
1279
+ scriptBindings ,
1280
+ vueImportAliases ,
1281
+ hoistStatic
1282
+ )
1271
1283
}
1272
1284
}
1273
1285
@@ -1394,7 +1406,13 @@ export function compileScript(
1394
1406
node . type === 'TSEnumDeclaration' ) &&
1395
1407
! node . declare
1396
1408
) {
1397
- isAllLiteral = walkDeclaration ( node , setupBindings , vueImportAliases )
1409
+ isAllLiteral = walkDeclaration (
1410
+ 'scriptSetup' ,
1411
+ node ,
1412
+ setupBindings ,
1413
+ vueImportAliases ,
1414
+ hoistStatic
1415
+ )
1398
1416
}
1399
1417
1400
1418
// hoist literal constants
@@ -1891,9 +1909,11 @@ function registerBinding(
1891
1909
}
1892
1910
1893
1911
function walkDeclaration (
1912
+ from : 'script' | 'scriptSetup' ,
1894
1913
node : Declaration ,
1895
1914
bindings : Record < string , BindingTypes > ,
1896
- userImportAliases : Record < string , string >
1915
+ userImportAliases : Record < string , string > ,
1916
+ hoistStatic : boolean
1897
1917
) : boolean {
1898
1918
let isAllLiteral = false
1899
1919
@@ -1918,7 +1938,10 @@ function walkDeclaration(
1918
1938
if ( id . type === 'Identifier' ) {
1919
1939
let bindingType
1920
1940
const userReactiveBinding = userImportAliases [ 'reactive' ]
1921
- if ( isAllLiteral || ( isConst && isStaticNode ( init ! ) ) ) {
1941
+ if (
1942
+ ( hoistStatic || from === 'script' ) &&
1943
+ ( isAllLiteral || ( isConst && isStaticNode ( init ! ) ) )
1944
+ ) {
1922
1945
bindingType = BindingTypes . LITERAL_CONST
1923
1946
} else if ( isCallOf ( init , userReactiveBinding ) ) {
1924
1947
// treat reactive() calls as let since it's meant to be mutable
0 commit comments