refactor: use const enums for flags

This commit is contained in:
Evan You
2019-08-22 11:12:37 -04:00
parent f26cd5dfe4
commit f3e9848bb2
11 changed files with 144 additions and 130 deletions

View File

@@ -1,16 +1,9 @@
import { isArray, isFunction, isString, isObject, EMPTY_ARR } from '@vue/shared'
import { ComponentInstance } from './component'
import { ComponentInstance, Data } from './component'
import { HostNode } from './createRenderer'
import { RawSlots } from './componentSlots'
import { CLASS } from './patchFlags'
import {
ELEMENT,
FUNCTIONAL_COMPONENT,
STATEFUL_COMPONENT,
TEXT_CHILDREN,
ARRAY_CHILDREN,
SLOTS_CHILDREN
} from './typeFlags'
import { PatchFlags } from './patchFlags'
import { ShapeFlags } from './shapeFlags'
export const Fragment = Symbol('Fragment')
export const Text = Symbol('Text')
@@ -108,12 +101,12 @@ export function createVNode(
props = props || null
// encode the vnode type information into a bitmap
const typeFlag = isString(type)
? ELEMENT
const shapeFlag = isString(type)
? ShapeFlags.ELEMENT
: isObject(type)
? STATEFUL_COMPONENT
? ShapeFlags.STATEFUL_COMPONENT
: isFunction(type)
? FUNCTIONAL_COMPONENT
? ShapeFlags.FUNCTIONAL_COMPONENT
: 0
const vnode: VNode = {
@@ -126,7 +119,7 @@ export function createVNode(
el: null,
anchor: null,
target: null,
shapeFlag: typeFlag,
shapeFlag,
patchFlag,
dynamicProps,
dynamicChildren: null
@@ -138,7 +131,7 @@ export function createVNode(
if (props !== null) {
// class normalization only needed if the vnode isn't generated by
// compiler-optimized code
if (props.class != null && !(patchFlag & CLASS)) {
if (props.class != null && !(patchFlag & PatchFlags.CLASS)) {
props.class = normalizeClass(props.class)
}
if (props.style != null) {
@@ -153,8 +146,8 @@ export function createVNode(
if (
shouldTrack &&
(patchFlag ||
typeFlag & STATEFUL_COMPONENT ||
typeFlag & FUNCTIONAL_COMPONENT)
shapeFlag & ShapeFlags.STATEFUL_COMPONENT ||
shapeFlag & ShapeFlags.FUNCTIONAL_COMPONENT)
) {
trackDynamicNode(vnode)
}
@@ -169,7 +162,7 @@ function trackDynamicNode(vnode: VNode) {
}
}
export function cloneVNode(vnode: VNode): VNode {
export function cloneVNode(vnode: VNode, extraProps?: Data): VNode {
// TODO
return vnode
}
@@ -196,15 +189,15 @@ export function normalizeChildren(vnode: VNode, children: unknown) {
if (children == null) {
children = null
} else if (isArray(children)) {
type = ARRAY_CHILDREN
type = ShapeFlags.ARRAY_CHILDREN
} else if (typeof children === 'object') {
type = SLOTS_CHILDREN
type = ShapeFlags.SLOTS_CHILDREN
} else if (isFunction(children)) {
children = { default: children }
type = SLOTS_CHILDREN
type = ShapeFlags.SLOTS_CHILDREN
} else {
children = isString(children) ? children : children + ''
type = TEXT_CHILDREN
type = ShapeFlags.TEXT_CHILDREN
}
vnode.children = children as NormalizedChildren
vnode.shapeFlag |= type