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> 'TSInstantiationExpression', // foo<string>
'TSSatisfiesExpression' // foo satisfies T '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 { PropsExpression } from './transforms/transformElement'
import { parseExpression } from '@babel/parser' import { parseExpression } from '@babel/parser'
import { Expression } from '@babel/types' import { Expression } from '@babel/types'
import { unwrapTSNode } from './babelUtils'
export const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode => export const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic
@ -158,9 +159,7 @@ export const isMemberExpressionNode = __BROWSER__
let ret: Expression = parseExpression(path, { let ret: Expression = parseExpression(path, {
plugins: context.expressionPlugins plugins: context.expressionPlugins
}) })
if (ret.type === 'TSAsExpression' || ret.type === 'TSTypeAssertion') { ret = unwrapTSNode(ret) as Expression
ret = ret.expression
}
return ( return (
ret.type === 'MemberExpression' || ret.type === 'MemberExpression' ||
ret.type === 'OptionalMemberExpression' || ret.type === 'OptionalMemberExpression' ||

View File

@ -2,7 +2,8 @@ import {
BindingTypes, BindingTypes,
UNREF, UNREF,
isFunctionType, isFunctionType,
walkIdentifiers walkIdentifiers,
unwrapTSNode
} from '@vue/compiler-dom' } from '@vue/compiler-dom'
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse' import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
import { ParserPlugin } from '@babel/parser' import { ParserPlugin } from '@babel/parser'
@ -43,12 +44,7 @@ import { DEFINE_EXPOSE, processDefineExpose } from './script/defineExpose'
import { DEFINE_OPTIONS, processDefineOptions } from './script/defineOptions' import { DEFINE_OPTIONS, processDefineOptions } from './script/defineOptions'
import { processDefineSlots } from './script/defineSlots' import { processDefineSlots } from './script/defineSlots'
import { DEFINE_MODEL, processDefineModel } from './script/defineModel' import { DEFINE_MODEL, processDefineModel } from './script/defineModel'
import { import { isLiteralNode, isCallOf, getImportedName } from './script/utils'
isLiteralNode,
unwrapTSNode,
isCallOf,
getImportedName
} from './script/utils'
import { analyzeScriptBindings } from './script/analyzeScriptBindings' import { analyzeScriptBindings } from './script/analyzeScriptBindings'
import { isImportUsed } from './script/importUsageCheck' import { isImportUsed } from './script/importUsageCheck'
import { processAwait } from './script/topLevelAwait' import { processAwait } from './script/topLevelAwait'

View File

@ -5,10 +5,9 @@ import {
UNKNOWN_TYPE, UNKNOWN_TYPE,
concatStrings, concatStrings,
isCallOf, isCallOf,
toRuntimeTypeString, toRuntimeTypeString
unwrapTSNode
} from './utils' } from './utils'
import { BindingTypes } from '@vue/compiler-dom' import { BindingTypes, unwrapTSNode } from '@vue/compiler-dom'
import { warnOnce } from '../warn' import { warnOnce } from '../warn'
export const DEFINE_MODEL = 'defineModel' export const DEFINE_MODEL = 'defineModel'

View File

@ -1,6 +1,7 @@
import { Node } from '@babel/types' import { Node } from '@babel/types'
import { unwrapTSNode } from '@vue/compiler-dom'
import { ScriptCompileContext } from './context' import { ScriptCompileContext } from './context'
import { isCallOf, unwrapTSNode } from './utils' import { isCallOf } from './utils'
import { DEFINE_PROPS } from './defineProps' import { DEFINE_PROPS } from './defineProps'
import { DEFINE_EMITS } from './defineEmits' import { DEFINE_EMITS } from './defineEmits'
import { DEFINE_EXPOSE } from './defineExpose' import { DEFINE_EXPOSE } from './defineExpose'

View File

@ -6,7 +6,7 @@ import {
ObjectExpression, ObjectExpression,
Expression Expression
} from '@babel/types' } from '@babel/types'
import { BindingTypes, isFunctionType } from '@vue/compiler-dom' import { BindingTypes, isFunctionType, unwrapTSNode } from '@vue/compiler-dom'
import { ScriptCompileContext } from './context' import { ScriptCompileContext } from './context'
import { import {
TypeResolveContext, TypeResolveContext,
@ -19,7 +19,6 @@ import {
concatStrings, concatStrings,
isLiteralNode, isLiteralNode,
isCallOf, isCallOf,
unwrapTSNode,
toRuntimeTypeString, toRuntimeTypeString,
getEscapedPropName getEscapedPropName
} from './utils' } from './utils'

View File

@ -15,10 +15,11 @@ import {
isInDestructureAssignment, isInDestructureAssignment,
isReferencedIdentifier, isReferencedIdentifier,
isStaticProperty, isStaticProperty,
walkFunctionParams walkFunctionParams,
unwrapTSNode
} from '@vue/compiler-dom' } from '@vue/compiler-dom'
import { genPropsAccessExp } from '@vue/shared' import { genPropsAccessExp } from '@vue/shared'
import { isCallOf, resolveObjectKey, unwrapTSNode } from './utils' import { isCallOf, resolveObjectKey } from './utils'
import { ScriptCompileContext } from './context' import { ScriptCompileContext } from './context'
import { DEFINE_PROPS } from './defineProps' import { DEFINE_PROPS } from './defineProps'
import { warnOnce } from '../warn' import { warnOnce } from '../warn'

View File

@ -9,7 +9,6 @@ import {
StringLiteral StringLiteral
} from '@babel/types' } from '@babel/types'
import path from 'path' import path from 'path'
import { TS_NODE_TYPES } from '@vue/compiler-dom'
export const UNKNOWN_TYPE = 'Unknown' export const UNKNOWN_TYPE = 'Unknown'
@ -32,14 +31,6 @@ export function isLiteralNode(node: Node) {
return node.type.endsWith('Literal') 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( export function isCallOf(
node: Node | null | undefined, node: Node | null | undefined,
test: string | ((id: string) => boolean) | null | undefined test: string | ((id: string) => boolean) | null | undefined