Skip to content

Commit 2aa0087

Browse files
simplesmileryyx990803
authored andcommitted
Warn if coerce is not a function (#2929)
1 parent 955fa53 commit 2aa0087

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/compiler/compile-props.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function processPropValue (vm, prop, rawValue, fn) {
236236
if (value === undefined) {
237237
value = getPropDefaultValue(vm, prop)
238238
}
239-
value = coerceProp(prop, value)
239+
value = coerceProp(prop, value, vm)
240240
const coerced = value !== rawValue
241241
if (!assertProp(prop, value, vm)) {
242242
value = undefined
@@ -374,13 +374,20 @@ function assertProp (prop, value, vm) {
374374
* @return {*}
375375
*/
376376

377-
function coerceProp (prop, value) {
377+
function coerceProp (prop, value, vm) {
378378
var coerce = prop.options.coerce
379379
if (!coerce) {
380380
return value
381381
}
382-
// coerce is a function
383-
return coerce(value)
382+
if (typeof coerce === 'function') {
383+
return coerce(value)
384+
} else {
385+
process.env.NODE_ENV !== 'production' && warn(
386+
'Invalid coerce for prop "' + prop.name + '": expected function, got ' + typeof coerce + '.',
387+
vm
388+
)
389+
return value
390+
}
384391
}
385392

386393
/**

test/unit/specs/directives/internal/prop_spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,12 @@ describe('prop', function () {
423423
expect(getWarnCount()).toBe(0)
424424
})
425425

426+
it('warn if coerce is not a function', function () {
427+
var coerce = 1
428+
makeInstance('123', String, null, coerce)
429+
expect(getWarnCount()).toBe(1)
430+
})
431+
426432
it('multiple types + custom validator', function () {
427433
makeInstance(123, [String, Boolean, Number], null, String)
428434
expect(getWarnCount()).toBe(0)

0 commit comments

Comments
 (0)