Skip to content

Commit

Permalink
fix(hoist-static): don't hoist regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed May 23, 2023
1 parent 5806748 commit 39c72ff
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/twenty-rice-fix.md
@@ -0,0 +1,6 @@
---
'@vue-macros/hoist-static': minor
'@vue-macros/common': patch
---

don't hoist regexp
12 changes: 10 additions & 2 deletions packages/common/src/ast.ts
Expand Up @@ -73,12 +73,16 @@ export function checkInvalidScopeReference(
export function isStaticExpression(
node: Node,
options: Partial<
Record<'object' | 'fn' | 'objectMethod' | 'array' | 'unary', boolean> & {
Record<
'object' | 'fn' | 'objectMethod' | 'array' | 'unary' | 'regex',
boolean
> & {
magicComment?: string
}
> = {}
): boolean {
const { magicComment, fn, object, objectMethod, array, unary } = options
const { magicComment, fn, object, objectMethod, array, unary, regex } =
options

// magic comment
if (
Expand Down Expand Up @@ -148,7 +152,11 @@ export function isStaticExpression(
case 'TSNonNullExpression': // 1!
case 'TSAsExpression': // 1 as number
case 'TSTypeAssertion': // (<number>2)
case 'TSSatisfiesExpression': // 1 satisfies number
return isStaticExpression(node.expression, options)

case 'RegExpLiteral':
return !!regex
}

if (isLiteralType(node)) return true
Expand Down
Expand Up @@ -23,6 +23,7 @@ const d = 1 || 2
const e = !1
const f = 1 ? 2 : 3
const g = 1 as number
const h = <number>2
</script>
<script setup lang=\\"ts\\">
Expand All @@ -32,12 +33,13 @@ const g = 1 as number
// const h = <number>2
const i = {
a: 'hello',
}
const j = [1, 2, 3]
const k = Symbol()
const l = /a/
</script>
<template>
Expand Down
3 changes: 2 additions & 1 deletion packages/hoist-static/tests/fixtures/basic.vue
Expand Up @@ -6,12 +6,13 @@ const d = 1 || 2
const e = !1
const f = 1 ? 2 : 3
const g = 1 as number
// const h = <number>2
const h = <number>2
const i = {
a: 'hello',
}
const j = [1, 2, 3]
const k = Symbol()
const l = /a/
</script>

<template>
Expand Down

0 comments on commit 39c72ff

Please sign in to comment.