Skip to content

Commit a173d8c

Browse files
committed
refactor(types): improve types
1 parent eb7e854 commit a173d8c

15 files changed

+58
-31
lines changed

Diff for: packages/create-instance/add-mocks.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import $$Vue from 'vue'
33
import { warn } from 'shared/util'
44

5-
export default function addMocks (mockedProperties: Object, Vue: Component) {
5+
export default function addMocks (
6+
mockedProperties: Object,
7+
Vue: Component
8+
): void {
69
Object.keys(mockedProperties).forEach(key => {
710
try {
811
Vue.prototype[key] = mockedProperties[key]

Diff for: packages/create-instance/add-slots.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import { compileToFunctions } from 'vue-template-compiler'
44

5-
function startsWithTag (str) {
6-
return str && str.trim()[0] === '<'
5+
function startsWithTag (str: SlotValue): boolean {
6+
return typeof str === 'string' && str.trim()[0] === '<'
77
}
88

99
function createVNodesForSlot (

Diff for: packages/create-instance/create-functional-component.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { createSlotVNodes } from './add-slots'
77
export default function createFunctionalComponent (
88
component: Component,
99
mountingOptions: Options
10-
) {
10+
): Component {
1111
if (mountingOptions.context && typeof mountingOptions.context !== 'object') {
1212
throwError('mount.context must be an object')
1313
}

Diff for: packages/create-instance/extract-instance-options.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @flow
2+
13
const MOUNTING_OPTIONS = [
24
'attachToDocument',
35
'mocks',
@@ -11,7 +13,9 @@ const MOUNTING_OPTIONS = [
1113
'propsData'
1214
]
1315

14-
export default function extractInstanceOptions (options) {
16+
export default function extractInstanceOptions (
17+
options: Object
18+
): Object {
1519
const instanceOptions = { ...options }
1620
MOUNTING_OPTIONS.forEach(mountingOption => {
1721
delete instanceOptions[mountingOption]

Diff for: packages/create-instance/log-events.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export function logEvents (
44
vm: Component,
55
emitted: Object,
66
emittedByOrder: Array<any>
7-
) {
7+
): void {
88
const emit = vm.$emit
99
vm.$emit = (name, ...args) => {
1010
(emitted[name] || (emitted[name] = [])).push(args)
@@ -13,7 +13,7 @@ export function logEvents (
1313
}
1414
}
1515

16-
export function addEventLogger (vue: Component) {
16+
export function addEventLogger (vue: Component): void {
1717
vue.mixin({
1818
beforeCreate: function () {
1919
this.__emitted = Object.create(null)

Diff for: packages/create-instance/validate-slots.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function isValidSlot (slot: any): boolean {
1111
)
1212
}
1313

14-
function requiresTemplateCompiler (slot) {
14+
function requiresTemplateCompiler (slot: any): void {
1515
if (typeof slot === 'string' && !compileToFunctions) {
1616
throwError(
1717
`vueTemplateCompiler is undefined, you must pass ` +

Diff for: packages/shared/compile-template.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { compileToFunctions } from 'vue-template-compiler'
44

5-
export function compileTemplate (component: Component) {
5+
export function compileTemplate (component: Component): void {
66
if (component.template) {
77
Object.assign(component, compileToFunctions(component.template))
88
}

Diff for: packages/shared/util.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// @flow
22
import Vue from 'vue'
33

4-
export function throwError (msg: string) {
4+
export function throwError (msg: string): void {
55
throw new Error(`[vue-test-utils]: ${msg}`)
66
}
77

8-
export function warn (msg: string) {
8+
export function warn (msg: string): void {
99
console.error(`[vue-test-utils]: ${msg}`)
1010
}
1111

1212
const camelizeRE = /-(\w)/g
13-
export const camelize = (str: string) => {
13+
export const camelize = (str: string): string => {
1414
const camelizedStr = str.replace(
1515
camelizeRE,
1616
(_, c) => (c ? c.toUpperCase() : '')
@@ -21,14 +21,14 @@ export const camelize = (str: string) => {
2121
/**
2222
* Capitalize a string.
2323
*/
24-
export const capitalize = (str: string) =>
24+
export const capitalize = (str: string): string =>
2525
str.charAt(0).toUpperCase() + str.slice(1)
2626

2727
/**
2828
* Hyphenate a camelCase string.
2929
*/
3030
const hyphenateRE = /\B([A-Z])/g
31-
export const hyphenate = (str: string) =>
31+
export const hyphenate = (str: string): string =>
3232
str.replace(hyphenateRE, '-$1').toLowerCase()
3333

3434
export const vueVersion = Number(

Diff for: packages/shared/validators.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
22
import { throwError, capitalize, camelize, hyphenate } from './util'
33

4-
export function isDomSelector (selector: any) {
4+
export function isDomSelector (selector: any): boolean {
55
if (typeof selector !== 'string') {
66
return false
77
}
@@ -28,7 +28,7 @@ export function isDomSelector (selector: any) {
2828
}
2929
}
3030

31-
export function isVueComponent (component: any) {
31+
export function isVueComponent (component: any): boolean {
3232
if (typeof component === 'function' && component.options) {
3333
return true
3434
}
@@ -44,7 +44,7 @@ export function isVueComponent (component: any) {
4444
return typeof component.render === 'function'
4545
}
4646

47-
export function componentNeedsCompiling (component: Component) {
47+
export function componentNeedsCompiling (component: Component): boolean {
4848
return (
4949
component &&
5050
!component.render &&
@@ -53,7 +53,7 @@ export function componentNeedsCompiling (component: Component) {
5353
)
5454
}
5555

56-
export function isRefSelector (refOptionsObject: any) {
56+
export function isRefSelector (refOptionsObject: any): boolean {
5757
if (
5858
typeof refOptionsObject !== 'object' ||
5959
Object.keys(refOptionsObject || {}).length !== 1
@@ -64,15 +64,18 @@ export function isRefSelector (refOptionsObject: any) {
6464
return typeof refOptionsObject.ref === 'string'
6565
}
6666

67-
export function isNameSelector (nameOptionsObject: any) {
67+
export function isNameSelector (nameOptionsObject: any): boolean {
6868
if (typeof nameOptionsObject !== 'object' || nameOptionsObject === null) {
6969
return false
7070
}
7171

7272
return !!nameOptionsObject.name
7373
}
7474

75-
export function templateContainsComponent (template: string, name: string) {
75+
export function templateContainsComponent (
76+
template: string,
77+
name: string
78+
): boolean {
7679
return [capitalize, camelize, hyphenate].some(format => {
7780
const re = new RegExp(`<${format(name)}\\s*(\\s|>|(\/>))`, 'g')
7881
return re.test(template)

Diff for: packages/test-utils/src/add-scoped-slots.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ function getVueTemplateCompilerHelpers (proxy: Object): Object {
3232
return helpers
3333
}
3434

35-
export function addScopedSlots (vm: Component, scopedSlots: any) {
35+
export function addScopedSlots (
36+
vm: Component,
37+
scopedSlots: { [name: string]: string }
38+
): void {
3639
if (window.navigator.userAgent.match(/PhantomJS/i)) {
3740
throwError(
3841
`the scopedSlots option does not support PhantomJS. ` +

Diff for: packages/test-utils/src/error-handler.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export default function errorHandler (errorOrString, vm) {
1+
// @flow
2+
3+
export default function errorHandler (
4+
errorOrString: any,
5+
vm: Component
6+
): void {
27
const error =
38
typeof errorOrString === 'object'
49
? errorOrString

Diff for: packages/test-utils/src/find-vue-components.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ export function vmCtorMatchesName (vm: Component, name: string): boolean {
5656
))
5757
}
5858

59-
export function vmCtorMatchesSelector (component: Component, selector: Object) {
59+
export function vmCtorMatchesSelector (
60+
component: Component,
61+
selector: Object
62+
): boolean {
6063
const Ctor = selector._Ctor || (selector.options && selector.options._Ctor)
6164
if (!Ctor) {
6265
return false
@@ -70,7 +73,7 @@ export function vmCtorMatchesSelector (component: Component, selector: Object) {
7073
export function vmFunctionalCtorMatchesSelector (
7174
component: VNode,
7275
Ctor: Object
73-
) {
76+
): boolean {
7477
if (VUE_VERSION < 2.3) {
7578
throwError(
7679
`find for functional components is not support in ` + `Vue < 2.3`

Diff for: packages/test-utils/src/order-watchers.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
// @flow
2+
13
let i = 0
24

3-
function orderDeps (watcher) {
5+
function orderDeps (watcher): void {
46
watcher.deps.forEach(dep => {
57
if (dep._sortedId === i) {
68
return
@@ -11,7 +13,7 @@ function orderDeps (watcher) {
1113
})
1214
}
1315

14-
function orderVmWatchers (vm) {
16+
function orderVmWatchers (vm: Component): void {
1517
if (vm._watchers) {
1618
vm._watchers.forEach(orderDeps)
1719
}
@@ -27,7 +29,7 @@ function orderVmWatchers (vm) {
2729
vm.$children.forEach(orderVmWatchers)
2830
}
2931

30-
export function orderWatchers (vm) {
32+
export function orderWatchers (vm: Component): void {
3133
orderVmWatchers(vm)
3234
i++
3335
}

Diff for: packages/test-utils/src/set-watchers-to-sync.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
// @flow
2+
13
import { VUE_VERSION } from './consts'
24

3-
function setDepsSync (dep) {
5+
function setDepsSync (dep): void {
46
dep.subs.forEach(setWatcherSync)
57
}
68

7-
function setWatcherSync (watcher) {
9+
function setWatcherSync (watcher): void {
810
if (watcher.sync === true) {
911
return
1012
}
1113
watcher.sync = true
1214
watcher.deps.forEach(setDepsSync)
1315
}
1416

15-
export function setWatchersToSync (vm) {
17+
export function setWatchersToSync (vm: Component): void {
1618
if (vm._watchers) {
1719
vm._watchers.forEach(setWatcherSync)
1820
}

Diff for: packages/test-utils/src/warn-if-no-window.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
// @flow
2+
13
import { throwError } from 'shared/util'
24

3-
export default function warnIfNoWindow () {
5+
export default function warnIfNoWindow (): void {
46
if (typeof window === 'undefined') {
57
throwError(
68
`window is undefined, vue-test-utils needs to be ` +

0 commit comments

Comments
 (0)