Browse Source

fix #5539: improve `isDef` type definition (#5549)

* fix #5539: improve flow type coverage

* skip unnecessary object creation

* use flow internal syntax to enable predicate type
Herrington Darkholme 9 years ago
parent
commit
bb7c543fc2

+ 1 - 1
flow/component.js

@@ -42,7 +42,7 @@ declare interface Component {
   $once: (event: string, fn: Function) => Component;
   $off: (event?: string | Array<string>, fn?: Function) => Component;
   $emit: (event: string, ...args: Array<mixed>) => Component;
-  $nextTick: (fn: Function) => void;
+  $nextTick: (fn: Function) => void | Promise<*>;
   $createElement: (tag?: string | Component, data?: Object, children?: VNodeChildren) => VNode;
 
   // private properties

+ 1 - 1
flow/global-api.js

@@ -7,7 +7,7 @@ declare interface GlobalAPI {
   extend: (options: Object) => Function;
   set: <T>(target: Object | Array<T>, key: string | number, value: T) => T;
   delete: <T>(target: Object| Array<T>, 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<Function> };

+ 5 - 3
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<Dep>;
   newDeps: Array<Dep>;
-  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)) {

+ 3 - 1
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'

+ 8 - 1
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 }

+ 3 - 1
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 {

+ 2 - 0
src/core/util/index.js

@@ -1,3 +1,5 @@
+/* @flow */
+
 export * from 'shared/util'
 export * from './lang'
 export * from './env'

+ 1 - 0
src/core/vdom/create-functional-component.js

@@ -21,6 +21,7 @@ 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)
     }

+ 2 - 2
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
 }
 
@@ -103,7 +103,7 @@ export function remove (arr: Array<any>, item: any): Array<any> | 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)
 }