Skip to content

Commit 8ec73a3

Browse files
authored
fix(compiler-sfc): don't hoist regexp literial (#8300)
1 parent b36addd commit 8ec73a3

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

packages/compiler-sfc/__tests__/compileScript/__snapshots__/hoistStatic.spec.ts.snap

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ exports[`sfc hoist static > should hoist literal value 1`] = `
3737
const nil = null
3838
const bigint = 100n
3939
const template = \`str\`
40-
const regex = /.*/g
4140
4241
export default {
4342
setup(__props) {
@@ -124,6 +123,8 @@ exports[`sfc hoist static > should not hoist a variable 1`] = `
124123
125124
let KEY1 = 'default value'
126125
var KEY2 = 123
126+
const regex = /.*/g
127+
const undef = undefined
127128
128129
return () => {}
129130
}

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ describe('sfc hoist static', () => {
1919
const nil = null
2020
const bigint = 100n
2121
const template = \`str\`
22-
const regex = /.*/g
2322
`.trim()
2423
const { content, bindings } = compile(`
2524
<script setup>
@@ -35,8 +34,7 @@ describe('sfc hoist static', () => {
3534
boolean: BindingTypes.LITERAL_CONST,
3635
nil: BindingTypes.LITERAL_CONST,
3736
bigint: BindingTypes.LITERAL_CONST,
38-
template: BindingTypes.LITERAL_CONST,
39-
regex: BindingTypes.LITERAL_CONST
37+
template: BindingTypes.LITERAL_CONST
4038
})
4139
assertCode(content)
4240
})
@@ -90,6 +88,8 @@ describe('sfc hoist static', () => {
9088
const code = `
9189
let KEY1 = 'default value'
9290
var KEY2 = 123
91+
const regex = /.*/g
92+
const undef = undefined
9393
`.trim()
9494
const { content, bindings } = compile(`
9595
<script setup>
@@ -98,7 +98,9 @@ describe('sfc hoist static', () => {
9898
`)
9999
expect(bindings).toStrictEqual({
100100
KEY1: BindingTypes.SETUP_LET,
101-
KEY2: BindingTypes.SETUP_LET
101+
KEY2: BindingTypes.SETUP_LET,
102+
regex: BindingTypes.SETUP_CONST,
103+
undef: BindingTypes.SETUP_MAYBE_REF
102104
})
103105
expect(content).toMatch(`setup(__props) {\n\n ${code}`)
104106
assertCode(content)

packages/compiler-sfc/src/compileScript.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,8 @@ function canNeverBeRef(node: Node, userReactiveImport?: string): boolean {
12471247
}
12481248

12491249
function isStaticNode(node: Node): boolean {
1250+
node = unwrapTSNode(node)
1251+
12501252
switch (node.type) {
12511253
case 'UnaryExpression': // void 0, !true
12521254
return isStaticNode(node.argument)
@@ -1269,15 +1271,14 @@ function isStaticNode(node: Node): boolean {
12691271
return node.expressions.every(expr => isStaticNode(expr))
12701272

12711273
case 'ParenthesizedExpression': // (1)
1272-
case 'TSNonNullExpression': // 1!
1273-
case 'TSAsExpression': // 1 as number
1274-
case 'TSTypeAssertion': // (<number>2)
12751274
return isStaticNode(node.expression)
12761275

1277-
default:
1278-
if (isLiteralNode(node)) {
1279-
return true
1280-
}
1281-
return false
1276+
case 'StringLiteral':
1277+
case 'NumericLiteral':
1278+
case 'BooleanLiteral':
1279+
case 'NullLiteral':
1280+
case 'BigIntLiteral':
1281+
return true
12821282
}
1283+
return false
12831284
}

0 commit comments

Comments
 (0)