refactor: remove inheritAttrs / nativeOn, enforce explicit fallthrough

This commit is contained in:
Evan You
2018-10-09 21:10:30 -04:00
parent 699dfd95be
commit 83605ef26c
12 changed files with 97 additions and 127 deletions

View File

@@ -24,7 +24,6 @@ export interface FunctionalComponent<P = {}> {
(props: Readonly<P>, slots: Slots, attrs: Data): any
pure?: boolean
props?: ComponentPropsOptions<P>
inheritAttrs?: boolean
displayName?: string
}

View File

@@ -8,7 +8,6 @@ export interface ComponentClassOptions<This = ComponentInstance> {
computed?: ComponentComputedOptions<This>
watch?: ComponentWatchOptions<This>
displayName?: string
inheritAttrs?: boolean
}
export interface ComponentOptions<This = ComponentInstance>

View File

@@ -7,14 +7,7 @@ import {
Prop,
PropType
} from './componentOptions'
import {
EMPTY_OBJ,
nativeOnRE,
vnodeHookRE,
camelize,
hyphenate,
capitalize
} from './utils'
import { EMPTY_OBJ, camelize, hyphenate, capitalize } from './utils'
export function initializeProps(
instance: ComponentInstance,
@@ -67,7 +60,7 @@ const EMPTY_PROPS = { props: EMPTY_OBJ }
// resolve raw VNode data.
// - filter out reserved keys (key, ref, slots)
// - extract class, style and nativeOn* into $attrs (to be merged onto child
// - extract class and style into $attrs (to be merged onto child
// component root)
// - for the rest:
// - if has declared props: put declared ones in `props`, the rest in `attrs`
@@ -89,17 +82,9 @@ export function resolveProps(
if (key === 'key' || key === 'ref' || key === 'slots') {
continue
}
// class, style, nativeOn & directive hooks are always extracted into a
// separate `attrs` object, which can then be merged onto child component
// root. in addition, if the component has explicitly declared props, then
// any non-matching props are extracted into `attrs` as well.
if (
key === 'class' ||
key === 'style' ||
vnodeHookRE.test(key) ||
nativeOnRE.test(key) ||
(hasDeclaredProps && !options.hasOwnProperty(key))
) {
// any non-declared data are put into a separate `attrs` object
// for spreading
if (hasDeclaredProps && !options.hasOwnProperty(key)) {
;(attrs || (attrs = {}))[key] = rawData[key]
} else {
props[key] = rawData[key]

View File

@@ -55,11 +55,12 @@ export function createComponentInstance(
export function renderInstanceRoot(instance: ComponentInstance): VNode {
let vnode
try {
vnode = instance.render.call(instance.$proxy, h, {
props: instance.$props,
slots: instance.$slots,
attrs: instance.$attrs
})
vnode = instance.render.call(
instance.$proxy,
instance.$props,
instance.$slots,
instance.$attrs
)
} catch (e1) {
handleError(e1, instance, ErrorTypes.RENDER)
if (__DEV__ && instance.renderError) {
@@ -70,12 +71,7 @@ export function renderInstanceRoot(instance: ComponentInstance): VNode {
}
}
}
return normalizeComponentRoot(
vnode,
instance.$parentVNode,
instance.$attrs,
instance.constructor.inheritAttrs
)
return normalizeComponentRoot(vnode, instance.$parentVNode)
}
export function teardownComponentInstance(instance: ComponentInstance) {
@@ -95,9 +91,7 @@ export function teardownComponentInstance(instance: ComponentInstance) {
export function normalizeComponentRoot(
vnode: any,
componentVNode: VNode | null,
attrs: Record<string, any> | void,
inheritAttrs: boolean | void
componentVNode: VNode | null
): VNode {
if (vnode == null) {
vnode = createTextVNode('')
@@ -105,12 +99,7 @@ export function normalizeComponentRoot(
vnode = createTextVNode(vnode + '')
} else if (Array.isArray(vnode)) {
if (vnode.length === 1) {
vnode = normalizeComponentRoot(
vnode[0],
componentVNode,
attrs,
inheritAttrs
)
vnode = normalizeComponentRoot(vnode[0], componentVNode)
} else {
vnode = createFragment(vnode)
}
@@ -120,13 +109,7 @@ export function normalizeComponentRoot(
componentVNode &&
(flags & VNodeFlags.COMPONENT || flags & VNodeFlags.ELEMENT)
) {
if (
inheritAttrs !== false &&
attrs !== void 0 &&
Object.keys(attrs).length > 0
) {
vnode = cloneVNode(vnode, attrs)
} else if (el) {
if (el) {
vnode = cloneVNode(vnode)
}
if (flags & VNodeFlags.COMPONENT) {
@@ -188,6 +171,8 @@ export function createComponentClassFromOptions(
proto.beforeUnmount = value
} else if (key === 'destroyed') {
proto.unmounted = value
} else {
proto[key] = value
}
} else {
proto[key] = value

View File

@@ -238,9 +238,7 @@ export function createRenderer(options: RendererOptions) {
const { props, attrs } = resolveProps(data, render.props)
const subTree = (vnode.children = normalizeComponentRoot(
render(props, slots || EMPTY_OBJ, attrs || EMPTY_OBJ),
vnode,
attrs,
render.inheritAttrs
vnode
))
mount(subTree, container, parentComponent, isSVG, endNode)
vnode.el = subTree.el as RenderNode
@@ -526,9 +524,7 @@ export function createRenderer(options: RendererOptions) {
const { props, attrs } = resolveProps(nextData, render.props)
const nextTree = (nextVNode.children = normalizeComponentRoot(
render(props, nextSlots || EMPTY_OBJ, attrs || EMPTY_OBJ),
nextVNode,
attrs,
render.inheritAttrs
nextVNode
))
patch(prevTree, nextTree, container, parentComponent, isSVG)
nextVNode.el = nextTree.el

View File

@@ -9,7 +9,7 @@ import {
createFragment,
createPortal
} from './vdom'
import { isObservable, unwrap } from '@vue/observer'
import { isObservable } from '@vue/observer'
export const Fragment = Symbol()
export const Portal = Symbol()
@@ -44,9 +44,9 @@ export const h = ((tag: ElementType, data?: any, children?: any): VNode => {
if (children === void 0) children = null
// if value is observable, create a clone of original
// so that we can mutate it later on.
// so that we can normalize its class/style
if (isObservable(data)) {
data = Object.assign({}, unwrap(data))
data = Object.assign({}, data)
}
let key = null

View File

@@ -11,7 +11,10 @@ export * from '@vue/observer'
export { nextTick } from '@vue/scheduler'
// Internal API
export { createComponentInstance } from './componentUtils'
export {
createComponentInstance,
createComponentClassFromOptions
} from './componentUtils'
// Optional APIs
// these are imported on-demand and can be tree-shaken

View File

@@ -3,9 +3,8 @@ export const EMPTY_OBJ: { readonly [key: string]: any } = Object.freeze({})
export const NOOP = () => {}
export const onRE = /^on/
export const nativeOnRE = /^nativeOn/
export const vnodeHookRE = /^vnode/
export const handlersRE = /^on|^nativeOn|^vnode/
export const handlersRE = /^on|^vnode/
export const reservedPropRE = /^(?:key|ref|slots)$|^vnode/
export function normalizeStyle(

View File

@@ -277,7 +277,7 @@ export function cloneVNode(vnode: VNode, extraData?: VNodeData): VNode {
extraData.style
])
} else if (handlersRE.test(key)) {
// on*, nativeOn*, vnode*
// on*, vnode*
const existing = clonedData[key]
clonedData[key] = existing
? [].concat(existing, extraData[key])