Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): binding type when disabling hoistStatic #8029

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,15 @@ return () => {}
}"
`;
exports[`sfc hoist static > should not hoist when disabled 1`] = `
"export default {
setup(__props) {
const foo = 'bar'
return () => {}
}
}"
`;
2 changes: 1 addition & 1 deletion packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('SFC compile <script setup>', () => {
expect(bindings).toStrictEqual({
x: BindingTypes.SETUP_MAYBE_REF,
a: BindingTypes.SETUP_LET,
b: BindingTypes.LITERAL_CONST,
b: BindingTypes.SETUP_CONST,
c: BindingTypes.SETUP_CONST,
d: BindingTypes.SETUP_CONST,
xx: BindingTypes.SETUP_MAYBE_REF,
Expand Down
17 changes: 16 additions & 1 deletion packages/compiler-sfc/__tests__/compileScriptHoistStatic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,22 @@ describe('sfc hoist static', () => {
</script>
`)
expect(bindings).toStrictEqual({
foo: BindingTypes.LITERAL_CONST
foo: BindingTypes.SETUP_CONST
})
assertCode(content)
})

test('should not hoist when disabled', () => {
const { content, bindings } = compile(
`
<script setup>
const foo = 'bar'
</script>
`,
{ hoistStatic: false }
)
expect(bindings).toStrictEqual({
foo: BindingTypes.SETUP_CONST
})
assertCode(content)
})
Expand Down
35 changes: 29 additions & 6 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ export function compileScript(
if (!node) return
walkIdentifiers(node, id => {
const binding = setupBindings[id.name]
if (binding && (binding !== BindingTypes.LITERAL_CONST || !hoistStatic)) {
if (binding && binding !== BindingTypes.LITERAL_CONST) {
error(
`\`${method}()\` in <script setup> cannot reference locally ` +
`declared variables because it will be hoisted outside of the ` +
Expand Down Expand Up @@ -1258,7 +1258,13 @@ export function compileScript(
}
}
if (node.declaration) {
walkDeclaration(node.declaration, scriptBindings, vueImportAliases)
walkDeclaration(
'script',
node.declaration,
scriptBindings,
vueImportAliases,
hoistStatic
)
}
} else if (
(node.type === 'VariableDeclaration' ||
Expand All @@ -1267,7 +1273,13 @@ export function compileScript(
node.type === 'TSEnumDeclaration') &&
!node.declare
) {
walkDeclaration(node, scriptBindings, vueImportAliases)
walkDeclaration(
'script',
node,
scriptBindings,
vueImportAliases,
hoistStatic
)
}
}

Expand Down Expand Up @@ -1394,7 +1406,13 @@ export function compileScript(
node.type === 'TSEnumDeclaration') &&
!node.declare
) {
isAllLiteral = walkDeclaration(node, setupBindings, vueImportAliases)
isAllLiteral = walkDeclaration(
'scriptSetup',
node,
setupBindings,
vueImportAliases,
hoistStatic
)
}

// hoist literal constants
Expand Down Expand Up @@ -1891,9 +1909,11 @@ function registerBinding(
}

function walkDeclaration(
from: 'script' | 'scriptSetup',
node: Declaration,
bindings: Record<string, BindingTypes>,
userImportAliases: Record<string, string>
userImportAliases: Record<string, string>,
hoistStatic: boolean
): boolean {
let isAllLiteral = false

Expand All @@ -1918,7 +1938,10 @@ function walkDeclaration(
if (id.type === 'Identifier') {
let bindingType
const userReactiveBinding = userImportAliases['reactive']
if (isAllLiteral || (isConst && isStaticNode(init!))) {
if (
(hoistStatic || from === 'script') &&
(isAllLiteral || (isConst && isStaticNode(init!)))
) {
bindingType = BindingTypes.LITERAL_CONST
} else if (isCallOf(init, userReactiveBinding)) {
// treat reactive() calls as let since it's meant to be mutable
Expand Down