Skip to content

Commit f7062b9

Browse files
kazuponyyx990803
authored andcommitted
flowtype reafactoring (#4945)
1 parent 8bb6c2b commit f7062b9

File tree

6 files changed

+67
-67
lines changed

6 files changed

+67
-67
lines changed

src/core/components/keep-alive.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import { callHook } from 'core/instance/lifecycle'
44
import { getFirstComponentChild } from 'core/vdom/helpers/index'
55

6+
type VNodeCache = { [key: string]: ?VNode };
7+
68
const patternTypes: Array<Function> = [String, RegExp]
79

810
function getComponentName (opts: ?VNodeComponentOptions): ?string {
@@ -19,11 +21,11 @@ function matches (pattern: string | RegExp, name: string): boolean {
1921
return false
2022
}
2123

22-
function pruneCache (cache, filter) {
24+
function pruneCache (cache: VNodeCache, filter: Function) {
2325
for (const key in cache) {
24-
const cachedNode = cache[key]
26+
const cachedNode: ?VNode = cache[key]
2527
if (cachedNode) {
26-
const name = getComponentName(cachedNode.componentOptions)
28+
const name: ?string = getComponentName(cachedNode.componentOptions)
2729
if (name && !filter(name)) {
2830
pruneCacheEntry(cachedNode)
2931
cache[key] = null
@@ -32,7 +34,7 @@ function pruneCache (cache, filter) {
3234
}
3335
}
3436

35-
function pruneCacheEntry (vnode: ?MountedComponentVNode) {
37+
function pruneCacheEntry (vnode: ?VNode) {
3638
if (vnode) {
3739
if (!vnode.componentInstance._inactive) {
3840
callHook(vnode.componentInstance, 'deactivated')
@@ -71,17 +73,17 @@ export default {
7173

7274
render () {
7375
const vnode: VNode = getFirstComponentChild(this.$slots.default)
74-
const componentOptions = vnode && vnode.componentOptions
76+
const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
7577
if (componentOptions) {
7678
// check pattern
77-
const name = getComponentName(componentOptions)
79+
const name: ?string = getComponentName(componentOptions)
7880
if (name && (
7981
(this.include && !matches(this.include, name)) ||
8082
(this.exclude && matches(this.exclude, name))
8183
)) {
8284
return vnode
8385
}
84-
const key = vnode.key == null
86+
const key: ?string = vnode.key == null
8587
// same constructor may get registered as different local components
8688
// so cid alone is not enough (#3269)
8789
? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')

src/core/vdom/helpers/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export * from './merge-hook'
44
export * from './update-listeners'
55
export * from './normalize-children'
66

7-
export function getFirstComponentChild (children: ?Array<any>): ?VNodeWithData {
8-
return children && children.filter(c => c && c.componentOptions)[0]
7+
export function getFirstComponentChild (children: ?Array<VNode>): ?VNode {
8+
return children && children.filter((c: VNode) => c && c.componentOptions)[0]
99
}

src/core/vdom/helpers/merge-hook.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
export function mergeVNodeHook (def: Object, hookKey: string, hook: Function, key: string) {
44
key = key + hookKey
5-
const injectedHash = def.__injected || (def.__injected = {})
5+
const injectedHash: Object = def.__injected || (def.__injected = {})
66
if (!injectedHash[key]) {
77
injectedHash[key] = true
8-
const oldHook = def[hookKey]
8+
const oldHook: ?Function = def[hookKey]
99
if (oldHook) {
1010
def[hookKey] = function () {
1111
oldHook.apply(this, arguments)

src/platforms/web/runtime/components/transition-group.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,33 @@ export default {
3434
props,
3535

3636
render (h: Function) {
37-
const tag = this.tag || this.$vnode.data.tag || 'span'
38-
const map = Object.create(null)
39-
const prevChildren = this.prevChildren = this.children
40-
const rawChildren = this.$slots.default || []
41-
const children = this.children = []
42-
const transitionData = extractTransitionData(this)
37+
const tag: string = this.tag || this.$vnode.data.tag || 'span'
38+
const map: Object = Object.create(null)
39+
const prevChildren: Array<VNode> = this.prevChildren = this.children
40+
const rawChildren: Array<VNode> = this.$slots.default || []
41+
const children: Array<VNode> = this.children = []
42+
const transitionData: Object = extractTransitionData(this)
4343

4444
for (let i = 0; i < rawChildren.length; i++) {
45-
const c = rawChildren[i]
45+
const c: VNode = rawChildren[i]
4646
if (c.tag) {
4747
if (c.key != null && String(c.key).indexOf('__vlist') !== 0) {
4848
children.push(c)
4949
map[c.key] = c
5050
;(c.data || (c.data = {})).transition = transitionData
5151
} else if (process.env.NODE_ENV !== 'production') {
52-
const opts = c.componentOptions
53-
const name = opts
54-
? (opts.Ctor.options.name || opts.tag)
55-
: c.tag
52+
const opts: ?VNodeComponentOptions = c.componentOptions
53+
const name: string = opts ? (opts.Ctor.options.name || opts.tag || '') : c.tag
5654
warn(`<transition-group> children must be keyed: <${name}>`)
5755
}
5856
}
5957
}
6058

6159
if (prevChildren) {
62-
const kept = []
63-
const removed = []
60+
const kept: Array<VNode> = []
61+
const removed: Array<VNode> = []
6462
for (let i = 0; i < prevChildren.length; i++) {
65-
const c = prevChildren[i]
63+
const c: VNode = prevChildren[i]
6664
c.data.transition = transitionData
6765
c.data.pos = c.elm.getBoundingClientRect()
6866
if (map[c.key]) {
@@ -90,8 +88,8 @@ export default {
9088
},
9189

9290
updated () {
93-
const children = this.prevChildren
94-
const moveClass = this.moveClass || ((this.name || 'v') + '-move')
91+
const children: Array<VNode> = this.prevChildren
92+
const moveClass: string = this.moveClass || ((this.name || 'v') + '-move')
9593
if (!children.length || !this.hasMove(children[0].elm, moveClass)) {
9694
return
9795
}
@@ -104,12 +102,12 @@ export default {
104102

105103
// force reflow to put everything in position
106104
const body: any = document.body
107-
const f = body.offsetHeight // eslint-disable-line
105+
const f: number = body.offsetHeight // eslint-disable-line
108106

109-
children.forEach(c => {
107+
children.forEach((c: VNode) => {
110108
if (c.data.moved) {
111-
var el = c.elm
112-
var s = el.style
109+
var el: any = c.elm
110+
var s: any = el.style
113111
addTransitionClass(el, moveClass)
114112
s.transform = s.WebkitTransform = s.transitionDuration = ''
115113
el.addEventListener(transitionEndEvent, el._moveCb = function cb (e) {
@@ -137,21 +135,21 @@ export default {
137135
// transition at this very moment, we make a clone of it and remove
138136
// all other transition classes applied to ensure only the move class
139137
// is applied.
140-
const clone = el.cloneNode()
138+
const clone: HTMLElement = el.cloneNode()
141139
if (el._transitionClasses) {
142-
el._transitionClasses.forEach(cls => { removeClass(clone, cls) })
140+
el._transitionClasses.forEach((cls: string) => { removeClass(clone, cls) })
143141
}
144142
addClass(clone, moveClass)
145143
clone.style.display = 'none'
146144
this.$el.appendChild(clone)
147-
const info = getTransitionInfo(clone)
145+
const info: Object = getTransitionInfo(clone)
148146
this.$el.removeChild(clone)
149147
return (this._hasMove = info.hasTransform)
150148
}
151149
}
152150
}
153151

154-
function callPendingCbs (c) {
152+
function callPendingCbs (c: VNode) {
155153
/* istanbul ignore if */
156154
if (c.elm._moveCb) {
157155
c.elm._moveCb()
@@ -162,11 +160,11 @@ function callPendingCbs (c) {
162160
}
163161
}
164162

165-
function recordPosition (c) {
163+
function recordPosition (c: VNode) {
166164
c.data.newPos = c.elm.getBoundingClientRect()
167165
}
168166

169-
function applyTranslation (c) {
167+
function applyTranslation (c: VNode) {
170168
const oldPos = c.data.pos
171169
const newPos = c.data.newPos
172170
const dx = oldPos.left - newPos.left

src/platforms/web/runtime/components/transition.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const transitionProps = {
2828
// in case the child is also an abstract component, e.g. <keep-alive>
2929
// we want to recursively retrieve the real component to be rendered
3030
function getRealChild (vnode: ?VNode): ?VNode {
31-
const compOptions = vnode && vnode.componentOptions
31+
const compOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
3232
if (compOptions && compOptions.Ctor.options.abstract) {
3333
return getRealChild(getFirstComponentChild(compOptions.children))
3434
} else {
@@ -38,35 +38,35 @@ function getRealChild (vnode: ?VNode): ?VNode {
3838

3939
export function extractTransitionData (comp: Component): Object {
4040
const data = {}
41-
const options = comp.$options
41+
const options: ComponentOptions = comp.$options
4242
// props
4343
for (const key in options.propsData) {
4444
data[key] = comp[key]
4545
}
4646
// events.
4747
// extract listeners and pass them directly to the transition methods
48-
const listeners = options._parentListeners
48+
const listeners: ?Object = options._parentListeners
4949
for (const key in listeners) {
5050
data[camelize(key)] = listeners[key].fn
5151
}
5252
return data
5353
}
5454

55-
function placeholder (h, rawChild) {
55+
function placeholder (h: Function, rawChild: VNode): ?VNode {
5656
return /\d-keep-alive$/.test(rawChild.tag)
5757
? h('keep-alive')
5858
: null
5959
}
6060

61-
function hasParentTransition (vnode) {
61+
function hasParentTransition (vnode: VNode): ?boolean {
6262
while ((vnode = vnode.parent)) {
6363
if (vnode.data.transition) {
6464
return true
6565
}
6666
}
6767
}
6868

69-
function isSameChild (child, oldChild) {
69+
function isSameChild (child: VNode, oldChild: VNode): boolean {
7070
return oldChild.key === child.key && oldChild.tag === child.tag
7171
}
7272

@@ -76,13 +76,13 @@ export default {
7676
abstract: true,
7777

7878
render (h: Function) {
79-
let children = this.$slots.default
79+
let children: ?Array<VNode> = this.$slots.default
8080
if (!children) {
8181
return
8282
}
8383

8484
// filter out text nodes (possible whitespaces)
85-
children = children.filter(c => c.tag)
85+
children = children.filter((c: VNode) => c.tag)
8686
/* istanbul ignore if */
8787
if (!children.length) {
8888
return
@@ -97,7 +97,7 @@ export default {
9797
)
9898
}
9999

100-
const mode = this.mode
100+
const mode: string = this.mode
101101

102102
// warn invalid mode
103103
if (process.env.NODE_ENV !== 'production' &&
@@ -108,7 +108,7 @@ export default {
108108
)
109109
}
110110

111-
const rawChild = children[0]
111+
const rawChild: VNode = children[0]
112112

113113
// if this is a component root node and the component's
114114
// parent container node also has transition, skip.
@@ -118,7 +118,7 @@ export default {
118118

119119
// apply transition data to child
120120
// use getRealChild() to ignore abstract components e.g. keep-alive
121-
const child = getRealChild(rawChild)
121+
const child: ?VNode = getRealChild(rawChild)
122122
/* istanbul ignore if */
123123
if (!child) {
124124
return rawChild
@@ -131,15 +131,15 @@ export default {
131131
// ensure a key that is unique to the vnode type and to this transition
132132
// component instance. This key will be used to remove pending leaving nodes
133133
// during entering.
134-
const id = `__transition-${this._uid}-`
135-
const key = child.key = child.key == null
134+
const id: string = `__transition-${this._uid}-`
135+
const key: string = child.key = child.key == null
136136
? id + child.tag
137137
: isPrimitive(child.key)
138138
? (String(child.key).indexOf(id) === 0 ? child.key : id + child.key)
139139
: child.key
140-
const data = (child.data || (child.data = {})).transition = extractTransitionData(this)
141-
const oldRawChild = this._vnode
142-
const oldChild: any = getRealChild(oldRawChild)
140+
const data: Object = (child.data || (child.data = {})).transition = extractTransitionData(this)
141+
const oldRawChild: VNode = this._vnode
142+
const oldChild: VNode = getRealChild(oldRawChild)
143143

144144
// mark v-show
145145
// so that the transition module can hand over the control to the directive
@@ -150,7 +150,7 @@ export default {
150150
if (oldChild && oldChild.data && !isSameChild(child, oldChild)) {
151151
// replace old child transition data with fresh one
152152
// important for dynamic transitions!
153-
const oldData = oldChild && (oldChild.data.transition = extend({}, data))
153+
const oldData: Object = oldChild && (oldChild.data.transition = extend({}, data))
154154
// handle transition mode
155155
if (mode === 'out-in') {
156156
// return placeholder node and queue update when leave finishes
@@ -161,8 +161,8 @@ export default {
161161
}, key)
162162
return placeholder(h, rawChild)
163163
} else if (mode === 'in-out') {
164-
var delayedLeave
165-
var performLeave = () => { delayedLeave() }
164+
let delayedLeave
165+
const performLeave = () => { delayedLeave() }
166166
mergeVNodeHook(data, 'afterEnter', performLeave, key)
167167
mergeVNodeHook(data, 'enterCancelled', performLeave, key)
168168
mergeVNodeHook(oldData, 'delayLeave', leave => {

src/platforms/web/runtime/transition-util.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export function whenTransitionEnds (
8888
) {
8989
const { type, timeout, propCount } = getTransitionInfo(el, expectedType)
9090
if (!type) return cb()
91-
const event = type === TRANSITION ? transitionEndEvent : animationEndEvent
91+
const event: string = type === TRANSITION ? transitionEndEvent : animationEndEvent
9292
let ended = 0
9393
const end = () => {
9494
el.removeEventListener(event, onEnd)
@@ -117,15 +117,15 @@ export function getTransitionInfo (el: Element, expectedType?: ?string): {
117117
timeout: number;
118118
hasTransform: boolean;
119119
} {
120-
const styles = window.getComputedStyle(el)
121-
const transitioneDelays = styles[transitionProp + 'Delay'].split(', ')
122-
const transitionDurations = styles[transitionProp + 'Duration'].split(', ')
123-
const transitionTimeout = getTimeout(transitioneDelays, transitionDurations)
124-
const animationDelays = styles[animationProp + 'Delay'].split(', ')
125-
const animationDurations = styles[animationProp + 'Duration'].split(', ')
126-
const animationTimeout = getTimeout(animationDelays, animationDurations)
127-
128-
let type
120+
const styles: any = window.getComputedStyle(el)
121+
const transitioneDelays: Array<string> = styles[transitionProp + 'Delay'].split(', ')
122+
const transitionDurations: Array<string> = styles[transitionProp + 'Duration'].split(', ')
123+
const transitionTimeout: number = getTimeout(transitioneDelays, transitionDurations)
124+
const animationDelays: Array<string> = styles[animationProp + 'Delay'].split(', ')
125+
const animationDurations: Array<string> = styles[animationProp + 'Duration'].split(', ')
126+
const animationTimeout: number = getTimeout(animationDelays, animationDurations)
127+
128+
let type: ?string
129129
let timeout = 0
130130
let propCount = 0
131131
/* istanbul ignore if */
@@ -154,7 +154,7 @@ export function getTransitionInfo (el: Element, expectedType?: ?string): {
154154
: animationDurations.length
155155
: 0
156156
}
157-
const hasTransform =
157+
const hasTransform: boolean =
158158
type === TRANSITION &&
159159
transformRE.test(styles[transitionProp + 'Property'])
160160
return {

0 commit comments

Comments
 (0)