Skip to content

Commit f3503cd

Browse files
committed
👕 refactor(flow): fixes flowtype errors [ci skip]
NOTE: we should be more refactored flow type definition
1 parent 3180c88 commit f3503cd

File tree

8 files changed

+64
-67
lines changed

8 files changed

+64
-67
lines changed

decls/validator.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ declare type $ValidationCommonResult = {
2525
pristine: boolean,
2626
touched: boolean,
2727
untouched: boolean,
28-
modified: boolean
28+
modified: boolean,
29+
errors?: Array<ValidationError>
2930
}
3031

3132
declare type ValidationResult = Dictionary<Array<ValidationError> | boolean | string> & $ValidationCommonResult
@@ -36,7 +37,7 @@ declare type $ValidationRawResult = Dictionary<boolean | string | void | Diction
3637

3738
declare type ValidateDescriptor = {
3839
fn: Function,
39-
name: string,
40+
name?: string,
4041
value: any,
4142
field: string,
4243
prop?: string,
@@ -60,8 +61,10 @@ declare type ValidityComponent = {
6061
untouched: boolean,
6162
modified: boolean,
6263
result: ValidationResult,
64+
progress: string,
6365
progresses: ValidatorProgresses,
6466
multiple: boolean,
67+
autotouch: boolean,
6568

6669
checkModified (): boolean,
6770
willUpdateTouched (options?: any): void,
@@ -70,6 +73,7 @@ declare type ValidityComponent = {
7073
handleInputable (e: Event): void,
7174
watchInputable (val: any): void,
7275
reset (): void,
76+
touch (): void,
7377
validate (...args: Array<any>): boolean,
7478
} & Component
7579

@@ -100,6 +104,7 @@ declare type $ValidationGroupResult = Dictionary<ValidationResult> & $Validation
100104

101105
declare type ValidityGroupComponent = {
102106
results: $ValidityGroupResult,
107+
validityCount (): Number,
103108
register (name:string, validity: ValidityComponent | ValidityGroupComponent): void,
104109
unregister (name: string): void,
105110
isRegistered (name: string): boolean,

src/components/validity/lifecycles.js

+3-37
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/* @flow */
22
import Elements from '../../elements/index'
3-
import { addClass, toggleClasses } from '../../util'
3+
import { addClass, toggleClasses, memoize } from '../../util'
44

55
export default function (Vue: GlobalAPI): Object {
6-
const { isPlainObject } = Vue.util
76
const { SingleElement, MultiElement, ComponentElement } = Elements(Vue)
87

98
function createValidityElement (vm: ValidityComponent, vnode: VNode): ?ValidityElement {
@@ -16,28 +15,6 @@ export default function (Vue: GlobalAPI): Object {
1615
: null
1716
}
1817

19-
function getValidatorProps (validators: any): Array<string> {
20-
const normalized = typeof validators === 'string' ? [validators] : validators
21-
const targets: Array<string> = []
22-
if (isPlainObject(normalized)) {
23-
Object.keys(normalized).forEach((validator: string) => {
24-
const props: ?Object = (normalized[validator] &&
25-
normalized[validator]['props'] &&
26-
isPlainObject(normalized[validator]['props']))
27-
? normalized[validator]['props']
28-
: null
29-
if (props) {
30-
Object.keys(props).forEach((prop: string) => {
31-
if (!~targets.indexOf(prop)) {
32-
targets.push(prop)
33-
}
34-
})
35-
}
36-
})
37-
}
38-
return targets
39-
}
40-
4118
function watchModelable (val: any): void {
4219
this.$emit('input', {
4320
result: this.result,
@@ -53,8 +30,6 @@ export default function (Vue: GlobalAPI): Object {
5330
return Object.keys(results)
5431
})
5532

56-
this._validatorProps = memoize(getValidatorProps)
57-
5833
// for event control flags
5934
this._modified = false
6035

@@ -123,14 +98,6 @@ export default function (Vue: GlobalAPI): Object {
12398
}
12499
}
125100

126-
function memoize (fn: Function): Function {
127-
const cache = Object.create(null)
128-
return function memoizeFn (id: string, ...args): any {
129-
const hit = cache[id]
130-
return hit || (cache[id] = fn(...args))
131-
}
132-
}
133-
134101
function checkComponentElement (vnode: VNode): any {
135102
return vnode.child &&
136103
vnode.componentOptions &&
@@ -144,7 +111,6 @@ function checkBuiltInElement (vnode: VNode): any {
144111
vnode.tag
145112
}
146113

147-
function hasModelDirective (vnode: VNode): boolean {
148-
return ((vnode && vnode.data && vnode.data.directives) || []).find(dir => { return dir.name === 'model' })
114+
function hasModelDirective (vnode: VNode): any {
115+
return ((vnode && vnode.data && vnode.data.directives) || []).find(dir => dir.name === 'model')
149116
}
150-

src/components/validity/methods-validate.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,14 @@ export default function (Vue: GlobalAPI): Object {
131131
const rawDescriptor = this._getValidateRawDescriptor(validator, this.field, value)
132132
if (!rawDescriptor) { return descriptors }
133133

134-
let descriptor = null
135134
if (!rawDescriptor.props) {
136-
descriptor = { name: validator }
135+
const descriptor: Object = { name: validator }
137136
extend(descriptor, rawDescriptor)
138137
descriptors.push(descriptor)
139138
} else {
140-
const propsKeys = Object.keys(descriptor.props)
139+
const propsKeys = Object.keys(!rawDescriptor.props)
141140
propsKeys.forEach((prop: string) => {
142-
descriptor = {
141+
const descriptor: Object = {
143142
fn: rawDescriptor.fn,
144143
name: validator,
145144
value: rawDescriptor.value[prop],
@@ -172,7 +171,7 @@ export default function (Vue: GlobalAPI): Object {
172171
let count = 0
173172
const len = descriptors.length
174173
descriptors.forEach((desc: ValidateDescriptor) => {
175-
const validator: string = desc.name
174+
const validator: any = desc.name
176175
const prop = desc.prop
177176
if ((!prop && this.progresses[validator]) || (prop && this.progresses[validator][prop])) {
178177
count++
@@ -236,7 +235,7 @@ export default function (Vue: GlobalAPI): Object {
236235
if (this.progresses[validator][prop]) { return }
237236
this.progresses[validator][prop] = 'running'
238237
const values = descriptor.value
239-
const propDescriptor = {
238+
const propDescriptor: Object = {
240239
fn: descriptor.fn,
241240
value: values[prop],
242241
field: descriptor.field

src/components/validity/states.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import baseProps from './props'
44
export default function (Vue: GlobalAPI): Object {
55
const { extend, isPlainObject } = Vue.util
66

7-
function initialStates (states: any, validators: Array<string> | Object, init = undefined): void {
7+
function initialStates (states: any, validators: any, init = undefined): void {
88
if (Array.isArray(validators)) {
99
validators.forEach((validator: string) => {
1010
states[validator] = init

src/elements/component.js

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
11
/* @flow */
2+
import { memoize } from '../util'
23

34
export default function (Vue: GlobalAPI): any {
4-
const { looseEqual } = Vue.util
5+
const { looseEqual, isPlainObject } = Vue.util
6+
7+
function getValidatorProps (validators: any): Array<string> {
8+
const normalized: any = typeof validators === 'string' ? [validators] : validators
9+
const targets: Array<string> = []
10+
if (isPlainObject(normalized)) {
11+
Object.keys(normalized).forEach((validator: string) => {
12+
const props: ?Object = (normalized[validator] &&
13+
normalized[validator]['props'] &&
14+
isPlainObject(normalized[validator]['props']))
15+
? normalized[validator]['props']
16+
: null
17+
if (props) {
18+
Object.keys(props).forEach((prop: string) => {
19+
if (!~targets.indexOf(prop)) {
20+
targets.push(prop)
21+
}
22+
})
23+
}
24+
})
25+
}
26+
return targets
27+
}
528

629
class ComponentElement {
730
_vm: ValidityComponent
831
_vnode: any
932
_unwatchInputable: Function | void
33+
_watchers: Array<Function>
34+
_validatorProps: Function
1035
initValue: any
1136

12-
constructor (vm: ValidityComponent, vnode: any) {
37+
constructor (vm: ValidityComponent, vnode: any, validatorProps: ?Function) {
1338
this._vm = vm
1439
this._vnode = vnode
40+
this._validatorProps = validatorProps || memoize(getValidatorProps)
1541
this.initValue = this.getValue()
1642
this._watchers = []
1743
this.attachValidity()
@@ -23,7 +49,7 @@ export default function (Vue: GlobalAPI): any {
2349

2450
getValidatorProps (): Array<string> {
2551
const vm = this._vm
26-
return vm._validatorProps(vm._uid.toString(), vm.validators)
52+
return this._validatorProps(vm._uid.toString(), vm.validators)
2753
}
2854

2955
getValue (): any {

src/util.js

+8
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,11 @@ export function toggleClasses (el: any, key: string, fn: Function): void {
6363
fn(el, keys[i])
6464
}
6565
}
66+
67+
export function memoize (fn: Function): Function {
68+
const cache = Object.create(null)
69+
return function memoizeFn (id: string, ...args): any {
70+
const hit = cache[id]
71+
return hit || (cache[id] = fn(...args))
72+
}
73+
}

src/validation.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ export default function (Vue: any): any {
9191
this._validityManager.isRegistered(named) && this._validityManager.unregister(named)
9292
this._unregisterValidityGroup('named', named)
9393
}
94-
} else if (namedValidity) {
94+
} else if (named && namedValidity) {
9595
namedValidity.unregister(field)
9696
if (namedValidity.validityCount() === 0) {
9797
this._validityManager.isRegistered(named) && this._validityManager.unregister(named)
9898
this._unregisterValidityGroup('named', named)
9999
}
100-
} else if (groupValidity) {
100+
} else if (group && groupValidity) {
101101
groupValidity.unregister(field)
102102
if (groupValidity.validityCount() === 0) {
103103
this._validityManager.isRegistered(group) && this._validityManager.unregister(group)

test/unit/elements/component.test.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ describe('ComponentElement class', () => {
2222
},
2323
render (h) {
2424
return (this.child = h('comp', { props: { value: this.value, prop1: this.prop1 }}))
25-
},
26-
methods: {
27-
_validatorProps () {
28-
return ['value', 'prop1']
29-
}
3025
}
3126
}).$mount()
3227
const component = new ComponentElement(vm, vm.child)
28+
// set stup
29+
component._validatorProps = () => { return ['value', 'prop1'] }
3330
assert.deepEqual(component.getValue(), { value: 'hello', prop1: 'foo' })
3431
waitForUpdate(() => {
3532
vm.value = 'world'
@@ -60,14 +57,9 @@ describe('ComponentElement class', () => {
6057
},
6158
render (h) {
6259
return (this.child = h('comp', { props: { value: this.value, prop1: this.prop1 }}))
63-
},
64-
methods: {
65-
_validatorProps () {
66-
return ['value', 'prop1']
67-
}
6860
}
6961
}).$mount()
70-
const component = new ComponentElement(vm, vm.child)
62+
const component = new ComponentElement(vm, vm.child, () => ['value', 'prop1'])
7163
assert(component.checkModified() === false)
7264
waitForUpdate(() => {
7365
vm.value = 'world'
@@ -86,7 +78,6 @@ describe('ComponentElement class', () => {
8678
describe('#listenToucheableEvent / #unlistenToucheableEvent', () => {
8779
it('should be work', done => {
8880
const handleFocusout = jasmine.createSpy()
89-
const _validatorProps = function () { return ['value', 'prop1'] }
9081
const vm = new Vue({
9182
data: {
9283
child: null,
@@ -104,11 +95,12 @@ describe('ComponentElement class', () => {
10495
return (this.child = h('comp', { props: { value: this.value }}))
10596
},
10697
methods: {
107-
willUpdateTouched: handleFocusout,
108-
_validatorProps
98+
willUpdateTouched: handleFocusout
10999
}
110100
}).$mount()
111101
const component = new ComponentElement(vm, vm.child)
102+
// set stup
103+
component._validatorProps = () => { return ['value', 'prop1'] }
112104
component.listenToucheableEvent()
113105
triggerEvent(vm.$el, 'focusout')
114106
waitForUpdate(() => {
@@ -126,7 +118,6 @@ describe('ComponentElement class', () => {
126118
describe('component', () => {
127119
it('should be work', done => {
128120
const watchInputable = jasmine.createSpy()
129-
const _validatorProps = function () { return ['value', 'prop1'] }
130121
const vm = new Vue({
131122
data: {
132123
child: null,
@@ -144,9 +135,11 @@ describe('ComponentElement class', () => {
144135
render (h) {
145136
return (this.child = h('comp', { props: { value: this.value, prop1: this.prop1 }}))
146137
},
147-
methods: { watchInputable, _validatorProps }
138+
methods: { watchInputable }
148139
}).$mount()
149140
const component = new ComponentElement(vm, vm.child)
141+
// set stup
142+
component._validatorProps = () => { return ['value', 'prop1'] }
150143
component.listenInputableEvent()
151144
waitForUpdate(() => {
152145
vm.value = 'world'

0 commit comments

Comments
 (0)