refactor(compiler-core): reuse unwrapTS utility function (#9795)

This commit is contained in:
三咲智子 Kevin Deng 2023-12-11 10:46:28 +08:00 committed by GitHub
parent 096ba81817
commit cf77435338
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 21 additions and 27 deletions

View File

@ -441,3 +441,11 @@ export const TS_NODE_TYPES = [
'TSInstantiationExpression', // foo<string>
'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
}
}

View File

@ -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' ||

View File

@ -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'

View File

@ -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'

View File

@ -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'

View File

@ -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'

View File

@ -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'

View File

@ -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