From dae1e34d520107b75783722b100bb048e87407c8 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Fri, 28 Apr 2017 17:41:18 +0800 Subject: [PATCH 1/3] fix #5539: improve flow type coverage --- flow/component.js | 2 +- flow/global-api.js | 2 +- src/core/observer/watcher.js | 8 +++++--- src/core/util/debug.js | 4 +++- src/core/util/env.js | 9 ++++++++- src/core/util/error.js | 4 +++- src/core/util/index.js | 2 ++ src/core/vdom/create-functional-component.js | 2 +- src/shared/util.js | 2 +- 9 files changed, 25 insertions(+), 10 deletions(-) diff --git a/flow/component.js b/flow/component.js index cce8254854c..e079df4feb9 100644 --- a/flow/component.js +++ b/flow/component.js @@ -42,7 +42,7 @@ declare interface Component { $once: (event: string, fn: Function) => Component; $off: (event?: string | Array, fn?: Function) => Component; $emit: (event: string, ...args: Array) => Component; - $nextTick: (fn: Function) => void; + $nextTick: (fn: Function) => void | Promise<*>; $createElement: (tag?: string | Component, data?: Object, children?: VNodeChildren) => VNode; // private properties diff --git a/flow/global-api.js b/flow/global-api.js index 3dff1a8eed6..d4cd4d4e39f 100644 --- a/flow/global-api.js +++ b/flow/global-api.js @@ -7,7 +7,7 @@ declare interface GlobalAPI { extend: (options: Object) => Function; set: (target: Object | Array, key: string | number, value: T) => T; delete: (target: Object| Array, key: string | number) => void; - nextTick: (fn: Function, context?: Object) => void; + nextTick: (fn: Function, context?: Object) => void | Promise<*>; use: (plugin: Function | Object) => void; mixin: (mixin: Object) => void; compile: (template: string) => { render: Function, staticRenderFns: Array }; diff --git a/src/core/observer/watcher.js b/src/core/observer/watcher.js index 213bc85c79b..3f3993df8be 100644 --- a/src/core/observer/watcher.js +++ b/src/core/observer/watcher.js @@ -12,6 +12,8 @@ import { handleError } from '../util/index' +import type { ISet } from '../util/index' + let uid = 0 /** @@ -32,8 +34,8 @@ export default class Watcher { active: boolean; deps: Array; newDeps: Array; - depIds: Set; - newDepIds: Set; + depIds: ISet; + newDepIds: ISet; getter: Function; value: any; @@ -242,7 +244,7 @@ function traverse (val: any) { _traverse(val, seenObjects) } -function _traverse (val: any, seen: Set) { +function _traverse (val: any, seen: ISet) { let i, keys const isA = Array.isArray(val) if ((!isA && !isObject(val)) || !Object.isExtensible(val)) { diff --git a/src/core/util/debug.js b/src/core/util/debug.js index d50ee221f43..ad19e167e57 100644 --- a/src/core/util/debug.js +++ b/src/core/util/debug.js @@ -1,9 +1,11 @@ +/* @flow */ + import config from '../config' import { noop } from 'shared/util' export let warn = noop export let tip = noop -export let formatComponentName +export let formatComponentName: Function = (null: any) // work around flow check if (process.env.NODE_ENV !== 'production') { const hasConsole = typeof console !== 'undefined' diff --git a/src/core/util/env.js b/src/core/util/env.js index ec5feff4e07..f959fb71e0c 100644 --- a/src/core/util/env.js +++ b/src/core/util/env.js @@ -153,7 +153,7 @@ if (typeof Set !== 'undefined' && isNative(Set)) { _Set = Set } else { // a non-standard Set polyfill that only works with primitive keys. - _Set = class Set { + _Set = class Set implements ISet { set: Object; constructor () { this.set = Object.create(null) @@ -170,4 +170,11 @@ if (typeof Set !== 'undefined' && isNative(Set)) { } } +interface ISet { + has(key: string| number): boolean; + add(key: string| number): mixed; + clear(): void; +} + export { _Set } +export type { ISet } diff --git a/src/core/util/error.js b/src/core/util/error.js index 595cd323d3b..69d44429467 100644 --- a/src/core/util/error.js +++ b/src/core/util/error.js @@ -1,8 +1,10 @@ +/* @flow */ + import config from '../config' import { warn } from './debug' import { inBrowser } from './env' -export function handleError (err, vm, info) { +export function handleError (err: Error, vm: any, info: string) { if (config.errorHandler) { config.errorHandler.call(null, err, vm, info) } else { diff --git a/src/core/util/index.js b/src/core/util/index.js index 8ccff396bbc..f88dd4ebc27 100644 --- a/src/core/util/index.js +++ b/src/core/util/index.js @@ -1,3 +1,5 @@ +/* @flow */ + export * from 'shared/util' export * from './lang' export * from './env' diff --git a/src/core/vdom/create-functional-component.js b/src/core/vdom/create-functional-component.js index 9ef6e72bcdd..99e26a181a5 100644 --- a/src/core/vdom/create-functional-component.js +++ b/src/core/vdom/create-functional-component.js @@ -22,7 +22,7 @@ export function createFunctionalComponent ( const propOptions = Ctor.options.props if (isDef(propOptions)) { for (const key in propOptions) { - props[key] = validateProp(key, propOptions, propsData) + props[key] = validateProp(key, propOptions, propsData || {}) } } else { if (isDef(data.attrs)) mergeProps(props, data.attrs) diff --git a/src/shared/util.js b/src/shared/util.js index eceb0ac48d2..e49ba1e39bb 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -103,7 +103,7 @@ export function remove (arr: Array, item: any): Array | void { * Check whether the object has the property. */ const hasOwnProperty = Object.prototype.hasOwnProperty -export function hasOwn (obj: Object, key: string): boolean { +export function hasOwn (obj: Object | Array<*>, key: string): boolean { return hasOwnProperty.call(obj, key) } From 3027b00dfb6559ce55fc694717f2ee386d72d411 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Fri, 28 Apr 2017 17:45:40 +0800 Subject: [PATCH 2/3] skip unnecessary object creation --- src/core/vdom/create-functional-component.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/vdom/create-functional-component.js b/src/core/vdom/create-functional-component.js index 99e26a181a5..335c32a6a12 100644 --- a/src/core/vdom/create-functional-component.js +++ b/src/core/vdom/create-functional-component.js @@ -21,8 +21,9 @@ export function createFunctionalComponent ( const props = {} const propOptions = Ctor.options.props if (isDef(propOptions)) { + propsData = propsData || {} for (const key in propOptions) { - props[key] = validateProp(key, propOptions, propsData || {}) + props[key] = validateProp(key, propOptions, propsData) } } else { if (isDef(data.attrs)) mergeProps(props, data.attrs) From 58dabcb76ad7c7ca5ddfbbd6b0a2b33d2ebbd4e9 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Fri, 28 Apr 2017 17:48:12 +0800 Subject: [PATCH 3/3] use flow internal syntax to enable predicate type --- src/shared/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/util.js b/src/shared/util.js index e49ba1e39bb..f7d68e60caa 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -6,7 +6,7 @@ export function isUndef (v: any): boolean { return v === undefined || v === null } -export function isDef (v: any): boolean { +export function isDef (v: any) /* : %checks */ { return v !== undefined && v !== null }