From 39c72ff0f351b9b2d7eb5ad22e2a8b98f7a263a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Tue, 23 May 2023 16:55:02 +0800 Subject: [PATCH] fix(hoist-static): don't hoist regexp --- .changeset/twenty-rice-fix.md | 6 ++++++ packages/common/src/ast.ts | 12 ++++++++++-- .../tests/__snapshots__/fixtures.test.ts.snap | 4 +++- packages/hoist-static/tests/fixtures/basic.vue | 3 ++- 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .changeset/twenty-rice-fix.md diff --git a/.changeset/twenty-rice-fix.md b/.changeset/twenty-rice-fix.md new file mode 100644 index 000000000..82923529e --- /dev/null +++ b/.changeset/twenty-rice-fix.md @@ -0,0 +1,6 @@ +--- +'@vue-macros/hoist-static': minor +'@vue-macros/common': patch +--- + +don't hoist regexp diff --git a/packages/common/src/ast.ts b/packages/common/src/ast.ts index f8c1b85ee..78c3db4cc 100644 --- a/packages/common/src/ast.ts +++ b/packages/common/src/ast.ts @@ -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 ( @@ -148,7 +152,11 @@ export function isStaticExpression( case 'TSNonNullExpression': // 1! case 'TSAsExpression': // 1 as number case 'TSTypeAssertion': // (2) + case 'TSSatisfiesExpression': // 1 satisfies number return isStaticExpression(node.expression, options) + + case 'RegExpLiteral': + return !!regex } if (isLiteralType(node)) return true diff --git a/packages/hoist-static/tests/__snapshots__/fixtures.test.ts.snap b/packages/hoist-static/tests/__snapshots__/fixtures.test.ts.snap index d64008cd5..3e466d2cd 100644 --- a/packages/hoist-static/tests/__snapshots__/fixtures.test.ts.snap +++ b/packages/hoist-static/tests/__snapshots__/fixtures.test.ts.snap @@ -23,6 +23,7 @@ const d = 1 || 2 const e = !1 const f = 1 ? 2 : 3 const g = 1 as number +const h = 2