Skip to content

Commit 4e4cbe3

Browse files
committed
move prop related functions into compile-props
1 parent 6ff1290 commit 4e4cbe3

File tree

3 files changed

+149
-147
lines changed

3 files changed

+149
-147
lines changed

src/compiler/compile-props.js

Lines changed: 148 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import config from '../config'
22
import { parseDirective } from '../parsers/directive'
3+
import { defineReactive } from '../observer/index'
34
import propDef from '../directives/internal/prop'
45
import {
56
warn,
@@ -8,10 +9,13 @@ import {
89
getAttr,
910
getBindAttr,
1011
isLiteral,
11-
initProp,
1212
toBoolean,
1313
toNumber,
14-
stripQuotes
14+
stripQuotes,
15+
isArray,
16+
isPlainObject,
17+
isObject,
18+
hasOwn
1519
} from '../util/index'
1620

1721
const propBindingModes = config._propBindingModes
@@ -201,3 +205,145 @@ function makePropsLinkFn (props) {
201205
}
202206
}
203207
}
208+
209+
/**
210+
* Set a prop's initial value on a vm and its data object.
211+
*
212+
* @param {Vue} vm
213+
* @param {Object} prop
214+
* @param {*} value
215+
*/
216+
217+
export function initProp (vm, prop, value) {
218+
const key = prop.path
219+
value = coerceProp(prop, value)
220+
if (value === undefined) {
221+
value = getPropDefaultValue(vm, prop.options)
222+
}
223+
if (assertProp(prop, value)) {
224+
defineReactive(vm, key, value, true /* doNotObserve */)
225+
}
226+
}
227+
228+
/**
229+
* Get the default value of a prop.
230+
*
231+
* @param {Vue} vm
232+
* @param {Object} options
233+
* @return {*}
234+
*/
235+
236+
function getPropDefaultValue (vm, options) {
237+
// no default, return undefined
238+
if (!hasOwn(options, 'default')) {
239+
// absent boolean value defaults to false
240+
return options.type === Boolean
241+
? false
242+
: undefined
243+
}
244+
var def = options.default
245+
// warn against non-factory defaults for Object & Array
246+
if (isObject(def)) {
247+
process.env.NODE_ENV !== 'production' && warn(
248+
'Object/Array as default prop values will be shared ' +
249+
'across multiple instances. Use a factory function ' +
250+
'to return the default value instead.'
251+
)
252+
}
253+
// call factory function for non-Function types
254+
return typeof def === 'function' && options.type !== Function
255+
? def.call(vm)
256+
: def
257+
}
258+
259+
/**
260+
* Assert whether a prop is valid.
261+
*
262+
* @param {Object} prop
263+
* @param {*} value
264+
*/
265+
266+
export function assertProp (prop, value) {
267+
if (
268+
!prop.options.required && ( // non-required
269+
prop.raw === null || // abscent
270+
value == null // null or undefined
271+
)
272+
) {
273+
return true
274+
}
275+
var options = prop.options
276+
var type = options.type
277+
var valid = true
278+
var expectedType
279+
if (type) {
280+
if (type === String) {
281+
expectedType = 'string'
282+
valid = typeof value === expectedType
283+
} else if (type === Number) {
284+
expectedType = 'number'
285+
valid = typeof value === 'number'
286+
} else if (type === Boolean) {
287+
expectedType = 'boolean'
288+
valid = typeof value === 'boolean'
289+
} else if (type === Function) {
290+
expectedType = 'function'
291+
valid = typeof value === 'function'
292+
} else if (type === Object) {
293+
expectedType = 'object'
294+
valid = isPlainObject(value)
295+
} else if (type === Array) {
296+
expectedType = 'array'
297+
valid = isArray(value)
298+
} else {
299+
valid = value instanceof type
300+
}
301+
}
302+
if (!valid) {
303+
process.env.NODE_ENV !== 'production' && warn(
304+
'Invalid prop: type check failed for ' +
305+
prop.path + '="' + prop.raw + '".' +
306+
' Expected ' + formatType(expectedType) +
307+
', got ' + formatValue(value) + '.'
308+
)
309+
return false
310+
}
311+
var validator = options.validator
312+
if (validator) {
313+
if (!validator(value)) {
314+
process.env.NODE_ENV !== 'production' && warn(
315+
'Invalid prop: custom validator check failed for ' +
316+
prop.path + '="' + prop.raw + '"'
317+
)
318+
return false
319+
}
320+
}
321+
return true
322+
}
323+
324+
/**
325+
* Force parsing value with coerce option.
326+
*
327+
* @param {*} value
328+
* @param {Object} options
329+
* @return {*}
330+
*/
331+
332+
export function coerceProp (prop, value) {
333+
var coerce = prop.options.coerce
334+
if (!coerce) {
335+
return value
336+
}
337+
// coerce is a function
338+
return coerce(value)
339+
}
340+
341+
function formatType (val) {
342+
return val
343+
? val.charAt(0).toUpperCase() + val.slice(1)
344+
: 'custom type'
345+
}
346+
347+
function formatValue (val) {
348+
return Object.prototype.toString.call(val).slice(8, -1)
349+
}

src/directives/internal/prop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import Watcher from '../../watcher'
77
import config from '../../config'
8-
import { assertProp, initProp, coerceProp } from '../../util/index'
8+
import { assertProp, initProp, coerceProp } from '../../compiler/compile-props'
99

1010
const bindingModes = config._propBindingModes
1111

src/util/component.js

Lines changed: 0 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { warn } from './debug'
22
import { resolveAsset } from './options'
33
import { getAttr, getBindAttr } from './dom'
4-
import { isArray, isPlainObject, isObject, hasOwn } from './lang'
5-
import { defineReactive } from '../observer/index'
64

75
export const commonTagRE = /^(div|p|span|img|a|b|i|br|ul|ol|li|h1|h2|h3|h4|h5|h6|code|pre|table|th|td|tr|form|label|input|select|option|nav|article|section|header|footer)$/i
86
export const reservedTagRE = /^(slot|partial|component)$/i
@@ -89,145 +87,3 @@ function getIsBinding (el) {
8987
}
9088
}
9189
}
92-
93-
/**
94-
* Set a prop's initial value on a vm and its data object.
95-
*
96-
* @param {Vue} vm
97-
* @param {Object} prop
98-
* @param {*} value
99-
*/
100-
101-
export function initProp (vm, prop, value) {
102-
const key = prop.path
103-
value = coerceProp(prop, value)
104-
if (value === undefined) {
105-
value = getPropDefaultValue(vm, prop.options)
106-
}
107-
if (assertProp(prop, value)) {
108-
defineReactive(vm, key, value, true /* doNotObserve */)
109-
}
110-
}
111-
112-
/**
113-
* Get the default value of a prop.
114-
*
115-
* @param {Vue} vm
116-
* @param {Object} options
117-
* @return {*}
118-
*/
119-
120-
function getPropDefaultValue (vm, options) {
121-
// no default, return undefined
122-
if (!hasOwn(options, 'default')) {
123-
// absent boolean value defaults to false
124-
return options.type === Boolean
125-
? false
126-
: undefined
127-
}
128-
var def = options.default
129-
// warn against non-factory defaults for Object & Array
130-
if (isObject(def)) {
131-
process.env.NODE_ENV !== 'production' && warn(
132-
'Object/Array as default prop values will be shared ' +
133-
'across multiple instances. Use a factory function ' +
134-
'to return the default value instead.'
135-
)
136-
}
137-
// call factory function for non-Function types
138-
return typeof def === 'function' && options.type !== Function
139-
? def.call(vm)
140-
: def
141-
}
142-
143-
/**
144-
* Assert whether a prop is valid.
145-
*
146-
* @param {Object} prop
147-
* @param {*} value
148-
*/
149-
150-
export function assertProp (prop, value) {
151-
if (
152-
!prop.options.required && ( // non-required
153-
prop.raw === null || // abscent
154-
value == null // null or undefined
155-
)
156-
) {
157-
return true
158-
}
159-
var options = prop.options
160-
var type = options.type
161-
var valid = true
162-
var expectedType
163-
if (type) {
164-
if (type === String) {
165-
expectedType = 'string'
166-
valid = typeof value === expectedType
167-
} else if (type === Number) {
168-
expectedType = 'number'
169-
valid = typeof value === 'number'
170-
} else if (type === Boolean) {
171-
expectedType = 'boolean'
172-
valid = typeof value === 'boolean'
173-
} else if (type === Function) {
174-
expectedType = 'function'
175-
valid = typeof value === 'function'
176-
} else if (type === Object) {
177-
expectedType = 'object'
178-
valid = isPlainObject(value)
179-
} else if (type === Array) {
180-
expectedType = 'array'
181-
valid = isArray(value)
182-
} else {
183-
valid = value instanceof type
184-
}
185-
}
186-
if (!valid) {
187-
process.env.NODE_ENV !== 'production' && warn(
188-
'Invalid prop: type check failed for ' +
189-
prop.path + '="' + prop.raw + '".' +
190-
' Expected ' + formatType(expectedType) +
191-
', got ' + formatValue(value) + '.'
192-
)
193-
return false
194-
}
195-
var validator = options.validator
196-
if (validator) {
197-
if (!validator(value)) {
198-
process.env.NODE_ENV !== 'production' && warn(
199-
'Invalid prop: custom validator check failed for ' +
200-
prop.path + '="' + prop.raw + '"'
201-
)
202-
return false
203-
}
204-
}
205-
return true
206-
}
207-
208-
/**
209-
* Force parsing value with coerce option.
210-
*
211-
* @param {*} value
212-
* @param {Object} options
213-
* @return {*}
214-
*/
215-
216-
export function coerceProp (prop, value) {
217-
var coerce = prop.options.coerce
218-
if (!coerce) {
219-
return value
220-
}
221-
// coerce is a function
222-
return coerce(value)
223-
}
224-
225-
function formatType (val) {
226-
return val
227-
? val.charAt(0).toUpperCase() + val.slice(1)
228-
: 'custom type'
229-
}
230-
231-
function formatValue (val) {
232-
return Object.prototype.toString.call(val).slice(8, -1)
233-
}

0 commit comments

Comments
 (0)