@@ -14,19 +14,19 @@ import {
14
14
} from './validators'
15
15
import { compileTemplate } from './compile-template'
16
16
17
- function isVueComponent ( comp ) {
17
+ function isVueComponent ( comp ) : boolean {
18
18
return comp && ( comp . render || comp . template || comp . options )
19
19
}
20
20
21
- function isValidStub ( stub : any ) {
21
+ function isValidStub ( stub : any ) : boolean {
22
22
return (
23
23
( ! ! stub && typeof stub === 'string' ) ||
24
24
stub === true ||
25
25
isVueComponent ( stub )
26
26
)
27
27
}
28
28
29
- function resolveComponent ( obj , component ) {
29
+ function resolveComponent ( obj : Object , component : string ) : Object {
30
30
return obj [ component ] ||
31
31
obj [ hyphenate ( component ) ] ||
32
32
obj [ camelize ( component ) ] ||
@@ -35,7 +35,7 @@ function resolveComponent (obj, component) {
35
35
{ }
36
36
}
37
37
38
- function isRequiredComponent ( name ) {
38
+ function isRequiredComponent ( name ) : boolean {
39
39
return (
40
40
name === 'KeepAlive' || name === 'Transition' || name === 'TransitionGroup'
41
41
)
@@ -59,11 +59,12 @@ function getCoreProperties (componentOptions: Component): Object {
59
59
functional : componentOptions . functional
60
60
}
61
61
}
62
+
62
63
function createStubFromString (
63
64
templateString : string ,
64
65
originalComponent : Component ,
65
66
name : string
66
- ) : Object {
67
+ ) : Component {
67
68
if ( ! compileToFunctions ) {
68
69
throwError (
69
70
`vueTemplateCompiler is undefined, you must pass ` +
@@ -86,7 +87,10 @@ function createStubFromString (
86
87
}
87
88
}
88
89
89
- function createBlankStub ( originalComponent : Component , name : string ) {
90
+ function createBlankStub (
91
+ originalComponent : Component ,
92
+ name : string
93
+ ) : Component {
90
94
const componentOptions = typeof originalComponent === 'function'
91
95
? originalComponent . extendOptions
92
96
: originalComponent
@@ -110,8 +114,8 @@ function createBlankStub (originalComponent: Component, name: string) {
110
114
111
115
export function createComponentStubs (
112
116
originalComponents : Object = { } ,
113
- stubs : Object
114
- ) : Object {
117
+ stubs : Stubs
118
+ ) : Components {
115
119
const components = { }
116
120
if ( ! stubs ) {
117
121
return components
@@ -130,55 +134,61 @@ export function createComponentStubs (
130
134
components [ stub ] = createBlankStub ( component , stub )
131
135
} )
132
136
} else {
133
- Object . keys ( stubs ) . forEach ( stub => {
134
- if ( stubs [ stub ] === false ) {
137
+ const stubsObject = ( stubs : { [ name : string ] : Component | string | true } )
138
+ Object . keys ( stubsObject ) . forEach ( stubName => {
139
+ const stub = stubsObject [ stubName ]
140
+ if ( stub === false ) {
135
141
return
136
142
}
137
- if ( ! isValidStub ( stubs [ stub ] ) ) {
143
+
144
+ if ( ! isValidStub ( stub ) ) {
138
145
throwError (
139
146
`options.stub values must be passed a string or ` + `component`
140
147
)
141
148
}
142
- if ( stubs [ stub ] === true ) {
143
- const component = resolveComponent ( originalComponents , stub )
144
- components [ stub ] = createBlankStub ( component , stub )
149
+
150
+ if ( stub === true ) {
151
+ const component = resolveComponent ( originalComponents , stubName )
152
+ components [ stubName ] = createBlankStub ( component , stubName )
145
153
return
146
154
}
147
155
148
- if ( componentNeedsCompiling ( stubs [ stub ] ) ) {
149
- compileTemplate ( stubs [ stub ] )
156
+ if ( typeof stub !== 'string' && componentNeedsCompiling ( stub ) ) {
157
+ compileTemplate ( stub )
150
158
}
151
159
152
- if ( originalComponents [ stub ] ) {
160
+ if ( originalComponents [ stubName ] ) {
153
161
// Remove cached constructor
154
- delete originalComponents [ stub ] . _Ctor
155
- if ( typeof stubs [ stub ] === 'string' ) {
156
- components [ stub ] = createStubFromString (
157
- stubs [ stub ] ,
158
- originalComponents [ stub ] ,
159
- stub
162
+ delete originalComponents [ stubName ] . _Ctor
163
+ if ( typeof stub === 'string' ) {
164
+ components [ stubName ] = createStubFromString (
165
+ stub ,
166
+ originalComponents [ stubName ] ,
167
+ stubName
160
168
)
161
169
} else {
162
- components [ stub ] = {
163
- ...stubs [ stub ] ,
164
- name : originalComponents [ stub ] . name
170
+ const stubObject = ( stub : Object )
171
+ components [ stubName ] = {
172
+ ...stubObject ,
173
+ name : originalComponents [ stubName ] . name
165
174
}
166
175
}
167
176
} else {
168
- if ( typeof stubs [ stub ] === 'string' ) {
177
+ if ( typeof stub === 'string' ) {
169
178
if ( ! compileToFunctions ) {
170
179
throwError (
171
180
`vueTemplateCompiler is undefined, you must pass ` +
172
181
`precompiled components if vue-template-compiler is ` +
173
182
`undefined`
174
183
)
175
184
}
176
- components [ stub ] = {
177
- ...compileToFunctions ( stubs [ stub ] )
185
+ components [ stubName ] = {
186
+ ...compileToFunctions ( stub )
178
187
}
179
188
} else {
180
- components [ stub ] = {
181
- ...stubs [ stub ]
189
+ const stubObject = ( stub : Object )
190
+ components [ stubName ] = {
191
+ ...stubObject
182
192
}
183
193
}
184
194
}
@@ -187,7 +197,10 @@ export function createComponentStubs (
187
197
return components
188
198
}
189
199
190
- function stubComponents ( components : Object , stubbedComponents : Object ) {
200
+ function stubComponents (
201
+ components : Components ,
202
+ stubbedComponents : Components
203
+ ) : void {
191
204
Object . keys ( components ) . forEach ( component => {
192
205
const cmp = components [ component ]
193
206
const componentOptions = typeof cmp === 'function'
@@ -202,7 +215,7 @@ function stubComponents (components: Object, stubbedComponents: Object) {
202
215
} )
203
216
}
204
217
205
- export function createComponentStubsForAll ( component : Component ) : Object {
218
+ export function createComponentStubsForAll ( component : Component ) : Components {
206
219
const stubbedComponents = { }
207
220
208
221
if ( component . components ) {
@@ -228,7 +241,9 @@ export function createComponentStubsForAll (component: Component): Object {
228
241
return stubbedComponents
229
242
}
230
243
231
- export function createComponentStubsForGlobals ( instance : Component ) : Object {
244
+ export function createComponentStubsForGlobals (
245
+ instance : Component
246
+ ) : Components {
232
247
const components = { }
233
248
Object . keys ( instance . options . components ) . forEach ( c => {
234
249
if ( isRequiredComponent ( c ) ) {
0 commit comments