Skip to content

Commit f7f4624

Browse files
authored
fix(compiler-sfc): fix binding type for constants when hoistStatic is disabled (#8029)
1 parent cac1512 commit f7f4624

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScriptHoistStatic.spec.ts.snap

+12
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,15 @@ return () => {}
130130
131131
}"
132132
`;
133+
134+
exports[`sfc hoist static > should not hoist when disabled 1`] = `
135+
"export default {
136+
setup(__props) {
137+
138+
const foo = 'bar'
139+
140+
return () => {}
141+
}
142+
143+
}"
144+
`;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('SFC compile <script setup>', () => {
3939
expect(bindings).toStrictEqual({
4040
x: BindingTypes.SETUP_MAYBE_REF,
4141
a: BindingTypes.SETUP_LET,
42-
b: BindingTypes.LITERAL_CONST,
42+
b: BindingTypes.SETUP_CONST,
4343
c: BindingTypes.SETUP_CONST,
4444
d: BindingTypes.SETUP_CONST,
4545
xx: BindingTypes.SETUP_MAYBE_REF,

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,22 @@ describe('sfc hoist static', () => {
183183
</script>
184184
`)
185185
expect(bindings).toStrictEqual({
186-
foo: BindingTypes.LITERAL_CONST
186+
foo: BindingTypes.SETUP_CONST
187+
})
188+
assertCode(content)
189+
})
190+
191+
test('should not hoist when disabled', () => {
192+
const { content, bindings } = compile(
193+
`
194+
<script setup>
195+
const foo = 'bar'
196+
</script>
197+
`,
198+
{ hoistStatic: false }
199+
)
200+
expect(bindings).toStrictEqual({
201+
foo: BindingTypes.SETUP_CONST
187202
})
188203
assertCode(content)
189204
})

packages/compiler-sfc/src/compileScript.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ export function compileScript(
803803
if (!node) return
804804
walkIdentifiers(node, id => {
805805
const binding = setupBindings[id.name]
806-
if (binding && (binding !== BindingTypes.LITERAL_CONST || !hoistStatic)) {
806+
if (binding && binding !== BindingTypes.LITERAL_CONST) {
807807
error(
808808
`\`${method}()\` in <script setup> cannot reference locally ` +
809809
`declared variables because it will be hoisted outside of the ` +
@@ -1258,7 +1258,13 @@ export function compileScript(
12581258
}
12591259
}
12601260
if (node.declaration) {
1261-
walkDeclaration(node.declaration, scriptBindings, vueImportAliases)
1261+
walkDeclaration(
1262+
'script',
1263+
node.declaration,
1264+
scriptBindings,
1265+
vueImportAliases,
1266+
hoistStatic
1267+
)
12621268
}
12631269
} else if (
12641270
(node.type === 'VariableDeclaration' ||
@@ -1267,7 +1273,13 @@ export function compileScript(
12671273
node.type === 'TSEnumDeclaration') &&
12681274
!node.declare
12691275
) {
1270-
walkDeclaration(node, scriptBindings, vueImportAliases)
1276+
walkDeclaration(
1277+
'script',
1278+
node,
1279+
scriptBindings,
1280+
vueImportAliases,
1281+
hoistStatic
1282+
)
12711283
}
12721284
}
12731285

@@ -1394,7 +1406,13 @@ export function compileScript(
13941406
node.type === 'TSEnumDeclaration') &&
13951407
!node.declare
13961408
) {
1397-
isAllLiteral = walkDeclaration(node, setupBindings, vueImportAliases)
1409+
isAllLiteral = walkDeclaration(
1410+
'scriptSetup',
1411+
node,
1412+
setupBindings,
1413+
vueImportAliases,
1414+
hoistStatic
1415+
)
13981416
}
13991417

14001418
// hoist literal constants
@@ -1891,9 +1909,11 @@ function registerBinding(
18911909
}
18921910

18931911
function walkDeclaration(
1912+
from: 'script' | 'scriptSetup',
18941913
node: Declaration,
18951914
bindings: Record<string, BindingTypes>,
1896-
userImportAliases: Record<string, string>
1915+
userImportAliases: Record<string, string>,
1916+
hoistStatic: boolean
18971917
): boolean {
18981918
let isAllLiteral = false
18991919

@@ -1918,7 +1938,10 @@ function walkDeclaration(
19181938
if (id.type === 'Identifier') {
19191939
let bindingType
19201940
const userReactiveBinding = userImportAliases['reactive']
1921-
if (isAllLiteral || (isConst && isStaticNode(init!))) {
1941+
if (
1942+
(hoistStatic || from === 'script') &&
1943+
(isAllLiteral || (isConst && isStaticNode(init!)))
1944+
) {
19221945
bindingType = BindingTypes.LITERAL_CONST
19231946
} else if (isCallOf(init, userReactiveBinding)) {
19241947
// treat reactive() calls as let since it's meant to be mutable

0 commit comments

Comments
 (0)