refactor(compiler): extract isStaticExp util

This commit is contained in:
Evan You
2020-07-13 16:48:16 -04:00
parent 8b320cc12f
commit 576344d2c3
6 changed files with 33 additions and 28 deletions

View File

@@ -43,7 +43,8 @@ import {
findProp,
isCoreComponent,
isBindKey,
findDir
findDir,
isStaticExp
} from '../utils'
import { buildSlots } from './vSlot'
import { getStaticType } from './hoistStatic'
@@ -275,7 +276,7 @@ export function buildProps(
const dynamicPropNames: string[] = []
const analyzePatchFlag = ({ key, value }: Property) => {
if (key.type === NodeTypes.SIMPLE_EXPRESSION && key.isStatic) {
if (isStaticExp(key)) {
const name = key.content
if (
!isComponent &&

View File

@@ -8,7 +8,12 @@ import {
ElementTypes
} from '../ast'
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) => {
const { exp, arg } = dir
@@ -43,7 +48,7 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
const propName = arg ? arg : createSimpleExpression('modelValue', true)
const eventName = arg
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
? isStaticExp(arg)
? `onUpdate:${arg.content}`
: createCompoundExpression(['"onUpdate:" + ', arg])
: `onUpdate:modelValue`
@@ -74,7 +79,7 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
.map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
.join(`, `)
const modifiersKey = arg
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
? isStaticExp(arg)
? `${arg.content}Modifiers`
: createCompoundExpression([arg, ' + "Modifiers"'])
: `modelModifiers`

View File

@@ -14,7 +14,6 @@ import {
SourceLocation,
createConditionalExpression,
ConditionalExpression,
JSChildNode,
SimpleExpressionNode,
FunctionExpression,
CallExpression,
@@ -24,13 +23,17 @@ import {
} from '../ast'
import { TransformContext, NodeTransform } from '../transform'
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 { parseForExpression, createForLoopParams } from './vFor'
const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic
const defaultFallback = createSimpleExpression(`undefined`, false)
// A NodeTransform that:

View File

@@ -20,7 +20,8 @@ import {
IfBranchNode,
TextNode,
InterpolationNode,
VNodeCall
VNodeCall,
SimpleExpressionNode
} from './ast'
import { TransformContext } from './transform'
import {
@@ -35,6 +36,9 @@ import { parse } from '@babel/parser'
import { walk } from 'estree-walker'
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 =>
tag === expected || tag === hyphenate(expected)
@@ -196,12 +200,7 @@ export function findProp(
}
export function isBindKey(arg: DirectiveNode['arg'], name: string): boolean {
return !!(
arg &&
arg.type === NodeTypes.SIMPLE_EXPRESSION &&
arg.isStatic &&
arg.content === name
)
return !!(arg && isStaticExp(arg) && arg.content === name)
}
export function hasDynamicKeyVBind(node: ElementNode): boolean {