refactor(compiler): extract isStaticExp util
This commit is contained in:
parent
8b320cc12f
commit
576344d2c3
@ -43,7 +43,8 @@ import {
|
|||||||
findProp,
|
findProp,
|
||||||
isCoreComponent,
|
isCoreComponent,
|
||||||
isBindKey,
|
isBindKey,
|
||||||
findDir
|
findDir,
|
||||||
|
isStaticExp
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
import { buildSlots } from './vSlot'
|
import { buildSlots } from './vSlot'
|
||||||
import { getStaticType } from './hoistStatic'
|
import { getStaticType } from './hoistStatic'
|
||||||
@ -275,7 +276,7 @@ export function buildProps(
|
|||||||
const dynamicPropNames: string[] = []
|
const dynamicPropNames: string[] = []
|
||||||
|
|
||||||
const analyzePatchFlag = ({ key, value }: Property) => {
|
const analyzePatchFlag = ({ key, value }: Property) => {
|
||||||
if (key.type === NodeTypes.SIMPLE_EXPRESSION && key.isStatic) {
|
if (isStaticExp(key)) {
|
||||||
const name = key.content
|
const name = key.content
|
||||||
if (
|
if (
|
||||||
!isComponent &&
|
!isComponent &&
|
||||||
|
@ -8,7 +8,12 @@ import {
|
|||||||
ElementTypes
|
ElementTypes
|
||||||
} from '../ast'
|
} from '../ast'
|
||||||
import { createCompilerError, ErrorCodes } from '../errors'
|
import { createCompilerError, ErrorCodes } from '../errors'
|
||||||
import { isMemberExpression, isSimpleIdentifier, hasScopeRef } from '../utils'
|
import {
|
||||||
|
isMemberExpression,
|
||||||
|
isSimpleIdentifier,
|
||||||
|
hasScopeRef,
|
||||||
|
isStaticExp
|
||||||
|
} from '../utils'
|
||||||
|
|
||||||
export const transformModel: DirectiveTransform = (dir, node, context) => {
|
export const transformModel: DirectiveTransform = (dir, node, context) => {
|
||||||
const { exp, arg } = dir
|
const { exp, arg } = dir
|
||||||
@ -43,7 +48,7 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
|
|||||||
|
|
||||||
const propName = arg ? arg : createSimpleExpression('modelValue', true)
|
const propName = arg ? arg : createSimpleExpression('modelValue', true)
|
||||||
const eventName = arg
|
const eventName = arg
|
||||||
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
|
? isStaticExp(arg)
|
||||||
? `onUpdate:${arg.content}`
|
? `onUpdate:${arg.content}`
|
||||||
: createCompoundExpression(['"onUpdate:" + ', arg])
|
: createCompoundExpression(['"onUpdate:" + ', arg])
|
||||||
: `onUpdate:modelValue`
|
: `onUpdate:modelValue`
|
||||||
@ -74,7 +79,7 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
|
|||||||
.map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
|
.map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
|
||||||
.join(`, `)
|
.join(`, `)
|
||||||
const modifiersKey = arg
|
const modifiersKey = arg
|
||||||
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
|
? isStaticExp(arg)
|
||||||
? `${arg.content}Modifiers`
|
? `${arg.content}Modifiers`
|
||||||
: createCompoundExpression([arg, ' + "Modifiers"'])
|
: createCompoundExpression([arg, ' + "Modifiers"'])
|
||||||
: `modelModifiers`
|
: `modelModifiers`
|
||||||
|
@ -14,7 +14,6 @@ import {
|
|||||||
SourceLocation,
|
SourceLocation,
|
||||||
createConditionalExpression,
|
createConditionalExpression,
|
||||||
ConditionalExpression,
|
ConditionalExpression,
|
||||||
JSChildNode,
|
|
||||||
SimpleExpressionNode,
|
SimpleExpressionNode,
|
||||||
FunctionExpression,
|
FunctionExpression,
|
||||||
CallExpression,
|
CallExpression,
|
||||||
@ -24,13 +23,17 @@ import {
|
|||||||
} from '../ast'
|
} from '../ast'
|
||||||
import { TransformContext, NodeTransform } from '../transform'
|
import { TransformContext, NodeTransform } from '../transform'
|
||||||
import { createCompilerError, ErrorCodes } from '../errors'
|
import { createCompilerError, ErrorCodes } from '../errors'
|
||||||
import { findDir, isTemplateNode, assert, isVSlot, hasScopeRef } from '../utils'
|
import {
|
||||||
|
findDir,
|
||||||
|
isTemplateNode,
|
||||||
|
assert,
|
||||||
|
isVSlot,
|
||||||
|
hasScopeRef,
|
||||||
|
isStaticExp
|
||||||
|
} from '../utils'
|
||||||
import { CREATE_SLOTS, RENDER_LIST, WITH_CTX } from '../runtimeHelpers'
|
import { CREATE_SLOTS, RENDER_LIST, WITH_CTX } from '../runtimeHelpers'
|
||||||
import { parseForExpression, createForLoopParams } from './vFor'
|
import { parseForExpression, createForLoopParams } from './vFor'
|
||||||
|
|
||||||
const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
|
|
||||||
p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic
|
|
||||||
|
|
||||||
const defaultFallback = createSimpleExpression(`undefined`, false)
|
const defaultFallback = createSimpleExpression(`undefined`, false)
|
||||||
|
|
||||||
// A NodeTransform that:
|
// A NodeTransform that:
|
||||||
|
@ -20,7 +20,8 @@ import {
|
|||||||
IfBranchNode,
|
IfBranchNode,
|
||||||
TextNode,
|
TextNode,
|
||||||
InterpolationNode,
|
InterpolationNode,
|
||||||
VNodeCall
|
VNodeCall,
|
||||||
|
SimpleExpressionNode
|
||||||
} from './ast'
|
} from './ast'
|
||||||
import { TransformContext } from './transform'
|
import { TransformContext } from './transform'
|
||||||
import {
|
import {
|
||||||
@ -35,6 +36,9 @@ import { parse } from '@babel/parser'
|
|||||||
import { walk } from 'estree-walker'
|
import { walk } from 'estree-walker'
|
||||||
import { Node } from '@babel/types'
|
import { Node } from '@babel/types'
|
||||||
|
|
||||||
|
export const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
|
||||||
|
p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic
|
||||||
|
|
||||||
export const isBuiltInType = (tag: string, expected: string): boolean =>
|
export const isBuiltInType = (tag: string, expected: string): boolean =>
|
||||||
tag === expected || tag === hyphenate(expected)
|
tag === expected || tag === hyphenate(expected)
|
||||||
|
|
||||||
@ -196,12 +200,7 @@ export function findProp(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isBindKey(arg: DirectiveNode['arg'], name: string): boolean {
|
export function isBindKey(arg: DirectiveNode['arg'], name: string): boolean {
|
||||||
return !!(
|
return !!(arg && isStaticExp(arg) && arg.content === name)
|
||||||
arg &&
|
|
||||||
arg.type === NodeTypes.SIMPLE_EXPRESSION &&
|
|
||||||
arg.isStatic &&
|
|
||||||
arg.content === name
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function hasDynamicKeyVBind(node: ElementNode): boolean {
|
export function hasDynamicKeyVBind(node: ElementNode): boolean {
|
||||||
|
@ -8,7 +8,8 @@ import {
|
|||||||
NodeTypes,
|
NodeTypes,
|
||||||
createCompoundExpression,
|
createCompoundExpression,
|
||||||
ExpressionNode,
|
ExpressionNode,
|
||||||
SimpleExpressionNode
|
SimpleExpressionNode,
|
||||||
|
isStaticExp
|
||||||
} from '@vue/compiler-core'
|
} from '@vue/compiler-core'
|
||||||
import { V_ON_WITH_MODIFIERS, V_ON_WITH_KEYS } from '../runtimeHelpers'
|
import { V_ON_WITH_MODIFIERS, V_ON_WITH_KEYS } from '../runtimeHelpers'
|
||||||
import { makeMap } from '@vue/shared'
|
import { makeMap } from '@vue/shared'
|
||||||
@ -30,7 +31,6 @@ const isKeyboardEvent = /*#__PURE__*/ makeMap(
|
|||||||
)
|
)
|
||||||
|
|
||||||
const resolveModifiers = (key: ExpressionNode, modifiers: string[]) => {
|
const resolveModifiers = (key: ExpressionNode, modifiers: string[]) => {
|
||||||
const isStaticKey = key.type === NodeTypes.SIMPLE_EXPRESSION && key.isStatic
|
|
||||||
const keyModifiers = []
|
const keyModifiers = []
|
||||||
const nonKeyModifiers = []
|
const nonKeyModifiers = []
|
||||||
const eventOptionModifiers = []
|
const eventOptionModifiers = []
|
||||||
@ -44,7 +44,7 @@ const resolveModifiers = (key: ExpressionNode, modifiers: string[]) => {
|
|||||||
} else {
|
} else {
|
||||||
// runtimeModifiers: modifiers that needs runtime guards
|
// runtimeModifiers: modifiers that needs runtime guards
|
||||||
if (maybeKeyModifier(modifier)) {
|
if (maybeKeyModifier(modifier)) {
|
||||||
if (isStaticKey) {
|
if (isStaticExp(key)) {
|
||||||
if (isKeyboardEvent((key as SimpleExpressionNode).content)) {
|
if (isKeyboardEvent((key as SimpleExpressionNode).content)) {
|
||||||
keyModifiers.push(modifier)
|
keyModifiers.push(modifier)
|
||||||
} else {
|
} else {
|
||||||
@ -73,9 +73,7 @@ const resolveModifiers = (key: ExpressionNode, modifiers: string[]) => {
|
|||||||
|
|
||||||
const transformClick = (key: ExpressionNode, event: string) => {
|
const transformClick = (key: ExpressionNode, event: string) => {
|
||||||
const isStaticClick =
|
const isStaticClick =
|
||||||
key.type === NodeTypes.SIMPLE_EXPRESSION &&
|
isStaticExp(key) && key.content.toLowerCase() === 'onclick'
|
||||||
key.isStatic &&
|
|
||||||
key.content.toLowerCase() === 'onclick'
|
|
||||||
return isStaticClick
|
return isStaticClick
|
||||||
? createSimpleExpression(event, true)
|
? createSimpleExpression(event, true)
|
||||||
: key.type !== NodeTypes.SIMPLE_EXPRESSION
|
: key.type !== NodeTypes.SIMPLE_EXPRESSION
|
||||||
@ -119,9 +117,7 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
|
|||||||
if (
|
if (
|
||||||
keyModifiers.length &&
|
keyModifiers.length &&
|
||||||
// if event name is dynamic, always wrap with keys guard
|
// if event name is dynamic, always wrap with keys guard
|
||||||
(key.type === NodeTypes.COMPOUND_EXPRESSION ||
|
(!isStaticExp(key) || isKeyboardEvent(key.content))
|
||||||
!key.isStatic ||
|
|
||||||
isKeyboardEvent(key.content))
|
|
||||||
) {
|
) {
|
||||||
handlerExp = createCallExpression(context.helper(V_ON_WITH_KEYS), [
|
handlerExp = createCallExpression(context.helper(V_ON_WITH_KEYS), [
|
||||||
handlerExp,
|
handlerExp,
|
||||||
|
@ -24,7 +24,8 @@ import {
|
|||||||
MERGE_PROPS,
|
MERGE_PROPS,
|
||||||
isBindKey,
|
isBindKey,
|
||||||
createSequenceExpression,
|
createSequenceExpression,
|
||||||
InterpolationNode
|
InterpolationNode,
|
||||||
|
isStaticExp
|
||||||
} from '@vue/compiler-dom'
|
} from '@vue/compiler-dom'
|
||||||
import {
|
import {
|
||||||
escapeHtml,
|
escapeHtml,
|
||||||
@ -194,7 +195,7 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
|
|||||||
}
|
}
|
||||||
for (let j = 0; j < props.length; j++) {
|
for (let j = 0; j < props.length; j++) {
|
||||||
const { key, value } = props[j]
|
const { key, value } = props[j]
|
||||||
if (key.type === NodeTypes.SIMPLE_EXPRESSION && key.isStatic) {
|
if (isStaticExp(key)) {
|
||||||
let attrName = key.content
|
let attrName = key.content
|
||||||
// static key attr
|
// static key attr
|
||||||
if (attrName === 'class') {
|
if (attrName === 'class') {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user