Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

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 @@ -37,7 +37,6 @@ exports[`sfc hoist static > should hoist literal value 1`] = `
const nil = null
const bigint = 100n
const template = \`str\`
const regex = /.*/g

export default {
setup(__props) {
Expand Down Expand Up @@ -124,6 +123,8 @@ exports[`sfc hoist static > should not hoist a variable 1`] = `

let KEY1 = 'default value'
var KEY2 = 123
const regex = /.*/g
const undef = undefined

return () => {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('sfc hoist static', () => {
const nil = null
const bigint = 100n
const template = \`str\`
const regex = /.*/g
`.trim()
const { content, bindings } = compile(`
<script setup>
Expand All @@ -35,8 +34,7 @@ describe('sfc hoist static', () => {
boolean: BindingTypes.LITERAL_CONST,
nil: BindingTypes.LITERAL_CONST,
bigint: BindingTypes.LITERAL_CONST,
template: BindingTypes.LITERAL_CONST,
regex: BindingTypes.LITERAL_CONST
template: BindingTypes.LITERAL_CONST
})
assertCode(content)
})
Expand Down Expand Up @@ -90,6 +88,8 @@ describe('sfc hoist static', () => {
const code = `
let KEY1 = 'default value'
var KEY2 = 123
const regex = /.*/g
const undef = undefined
`.trim()
const { content, bindings } = compile(`
<script setup>
Expand All @@ -98,7 +98,9 @@ describe('sfc hoist static', () => {
`)
expect(bindings).toStrictEqual({
KEY1: BindingTypes.SETUP_LET,
KEY2: BindingTypes.SETUP_LET
KEY2: BindingTypes.SETUP_LET,
regex: BindingTypes.SETUP_CONST,
undef: BindingTypes.SETUP_MAYBE_REF
})
expect(content).toMatch(`setup(__props) {\n\n ${code}`)
assertCode(content)
Expand Down
17 changes: 9 additions & 8 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,8 @@ function canNeverBeRef(node: Node, userReactiveImport?: string): boolean {
}

function isStaticNode(node: Node): boolean {
node = unwrapTSNode(node)

switch (node.type) {
case 'UnaryExpression': // void 0, !true
return isStaticNode(node.argument)
Expand All @@ -1269,15 +1271,14 @@ function isStaticNode(node: Node): boolean {
return node.expressions.every(expr => isStaticNode(expr))

case 'ParenthesizedExpression': // (1)
case 'TSNonNullExpression': // 1!
case 'TSAsExpression': // 1 as number
case 'TSTypeAssertion': // (<number>2)
return isStaticNode(node.expression)

default:
if (isLiteralNode(node)) {
return true
}
return false
case 'StringLiteral':
case 'NumericLiteral':
case 'BooleanLiteral':
case 'NullLiteral':
case 'BigIntLiteral':
return true
}
return false
}