From: 三咲智子 Kevin Deng Date: Mon, 11 Dec 2023 02:46:28 +0000 (+0800) Subject: refactor(compiler-core): reuse unwrapTS utility function (#9795) X-Git-Tag: v3.4.0-beta.1~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf7743533857b6fc07720ae14d876e211d43589e;p=thirdparty%2Fvuejs%2Fcore.git refactor(compiler-core): reuse unwrapTS utility function (#9795) --- diff --git a/packages/compiler-core/src/babelUtils.ts b/packages/compiler-core/src/babelUtils.ts index f3ef5df29d..a9c1ebe9c3 100644 --- a/packages/compiler-core/src/babelUtils.ts +++ b/packages/compiler-core/src/babelUtils.ts @@ -441,3 +441,11 @@ export const TS_NODE_TYPES = [ 'TSInstantiationExpression', // foo 'TSSatisfiesExpression' // foo satisfies T ] + +export function unwrapTSNode(node: Node): Node { + if (TS_NODE_TYPES.includes(node.type)) { + return unwrapTSNode((node as any).expression) + } else { + return node + } +} diff --git a/packages/compiler-core/src/utils.ts b/packages/compiler-core/src/utils.ts index a159d2eedc..adc5c19476 100644 --- a/packages/compiler-core/src/utils.ts +++ b/packages/compiler-core/src/utils.ts @@ -40,6 +40,7 @@ import { isString, isObject, NOOP } from '@vue/shared' import { PropsExpression } from './transforms/transformElement' import { parseExpression } from '@babel/parser' import { Expression } from '@babel/types' +import { unwrapTSNode } from './babelUtils' export const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode => p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic @@ -158,9 +159,7 @@ export const isMemberExpressionNode = __BROWSER__ let ret: Expression = parseExpression(path, { plugins: context.expressionPlugins }) - if (ret.type === 'TSAsExpression' || ret.type === 'TSTypeAssertion') { - ret = ret.expression - } + ret = unwrapTSNode(ret) as Expression return ( ret.type === 'MemberExpression' || ret.type === 'OptionalMemberExpression' || diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index b3f5a14148..1783c6db9e 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -2,7 +2,8 @@ import { BindingTypes, UNREF, isFunctionType, - walkIdentifiers + walkIdentifiers, + unwrapTSNode } from '@vue/compiler-dom' import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse' import { ParserPlugin } from '@babel/parser' @@ -43,12 +44,7 @@ import { DEFINE_EXPOSE, processDefineExpose } from './script/defineExpose' import { DEFINE_OPTIONS, processDefineOptions } from './script/defineOptions' import { processDefineSlots } from './script/defineSlots' import { DEFINE_MODEL, processDefineModel } from './script/defineModel' -import { - isLiteralNode, - unwrapTSNode, - isCallOf, - getImportedName -} from './script/utils' +import { isLiteralNode, isCallOf, getImportedName } from './script/utils' import { analyzeScriptBindings } from './script/analyzeScriptBindings' import { isImportUsed } from './script/importUsageCheck' import { processAwait } from './script/topLevelAwait' diff --git a/packages/compiler-sfc/src/script/defineModel.ts b/packages/compiler-sfc/src/script/defineModel.ts index 432b8676fb..c104eb306a 100644 --- a/packages/compiler-sfc/src/script/defineModel.ts +++ b/packages/compiler-sfc/src/script/defineModel.ts @@ -5,10 +5,9 @@ import { UNKNOWN_TYPE, concatStrings, isCallOf, - toRuntimeTypeString, - unwrapTSNode + toRuntimeTypeString } from './utils' -import { BindingTypes } from '@vue/compiler-dom' +import { BindingTypes, unwrapTSNode } from '@vue/compiler-dom' import { warnOnce } from '../warn' export const DEFINE_MODEL = 'defineModel' diff --git a/packages/compiler-sfc/src/script/defineOptions.ts b/packages/compiler-sfc/src/script/defineOptions.ts index 8da3dbc0b4..4a3551d755 100644 --- a/packages/compiler-sfc/src/script/defineOptions.ts +++ b/packages/compiler-sfc/src/script/defineOptions.ts @@ -1,6 +1,7 @@ import { Node } from '@babel/types' +import { unwrapTSNode } from '@vue/compiler-dom' import { ScriptCompileContext } from './context' -import { isCallOf, unwrapTSNode } from './utils' +import { isCallOf } from './utils' import { DEFINE_PROPS } from './defineProps' import { DEFINE_EMITS } from './defineEmits' import { DEFINE_EXPOSE } from './defineExpose' diff --git a/packages/compiler-sfc/src/script/defineProps.ts b/packages/compiler-sfc/src/script/defineProps.ts index ca631895f2..3df6daea7d 100644 --- a/packages/compiler-sfc/src/script/defineProps.ts +++ b/packages/compiler-sfc/src/script/defineProps.ts @@ -6,7 +6,7 @@ import { ObjectExpression, Expression } from '@babel/types' -import { BindingTypes, isFunctionType } from '@vue/compiler-dom' +import { BindingTypes, isFunctionType, unwrapTSNode } from '@vue/compiler-dom' import { ScriptCompileContext } from './context' import { TypeResolveContext, @@ -19,7 +19,6 @@ import { concatStrings, isLiteralNode, isCallOf, - unwrapTSNode, toRuntimeTypeString, getEscapedPropName } from './utils' diff --git a/packages/compiler-sfc/src/script/definePropsDestructure.ts b/packages/compiler-sfc/src/script/definePropsDestructure.ts index 5f2a1ef0aa..c9c4fb5f7b 100644 --- a/packages/compiler-sfc/src/script/definePropsDestructure.ts +++ b/packages/compiler-sfc/src/script/definePropsDestructure.ts @@ -15,10 +15,11 @@ import { isInDestructureAssignment, isReferencedIdentifier, isStaticProperty, - walkFunctionParams + walkFunctionParams, + unwrapTSNode } from '@vue/compiler-dom' import { genPropsAccessExp } from '@vue/shared' -import { isCallOf, resolveObjectKey, unwrapTSNode } from './utils' +import { isCallOf, resolveObjectKey } from './utils' import { ScriptCompileContext } from './context' import { DEFINE_PROPS } from './defineProps' import { warnOnce } from '../warn' diff --git a/packages/compiler-sfc/src/script/utils.ts b/packages/compiler-sfc/src/script/utils.ts index bc621d2584..a1124ce81b 100644 --- a/packages/compiler-sfc/src/script/utils.ts +++ b/packages/compiler-sfc/src/script/utils.ts @@ -9,7 +9,6 @@ import { StringLiteral } from '@babel/types' import path from 'path' -import { TS_NODE_TYPES } from '@vue/compiler-dom' export const UNKNOWN_TYPE = 'Unknown' @@ -32,14 +31,6 @@ export function isLiteralNode(node: Node) { return node.type.endsWith('Literal') } -export function unwrapTSNode(node: Node): Node { - if (TS_NODE_TYPES.includes(node.type)) { - return unwrapTSNode((node as any).expression) - } else { - return node - } -} - export function isCallOf( node: Node | null | undefined, test: string | ((id: string) => boolean) | null | undefined