mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-05 05:27:59 +08:00
fix(props): correctly warn when a provided prop is Symbol (#10529)
* fix(props): correctly warn when a provided prop is Symbol Fixes #10519 * style: space before parens
This commit is contained in:
parent
b97606cdc6
commit
abb5ef35dd
@ -205,18 +205,19 @@ function getInvalidTypeMessage (name, value, expectedTypes) {
|
||||
` Expected ${expectedTypes.map(capitalize).join(', ')}`
|
||||
const expectedType = expectedTypes[0]
|
||||
const receivedType = toRawType(value)
|
||||
const expectedValue = styleValue(value, expectedType)
|
||||
const receivedValue = styleValue(value, receivedType)
|
||||
// check if we need to specify expected value
|
||||
if (expectedTypes.length === 1 &&
|
||||
if (
|
||||
expectedTypes.length === 1 &&
|
||||
isExplicable(expectedType) &&
|
||||
!isBoolean(expectedType, receivedType)) {
|
||||
message += ` with value ${expectedValue}`
|
||||
isExplicable(typeof value) &&
|
||||
!isBoolean(expectedType, receivedType)
|
||||
) {
|
||||
message += ` with value ${styleValue(value, expectedType)}`
|
||||
}
|
||||
message += `, got ${receivedType} `
|
||||
// check if we need to specify received value
|
||||
if (isExplicable(receivedType)) {
|
||||
message += `with value ${receivedValue}.`
|
||||
message += `with value ${styleValue(value, receivedType)}.`
|
||||
}
|
||||
return message
|
||||
}
|
||||
@ -231,9 +232,9 @@ function styleValue (value, type) {
|
||||
}
|
||||
}
|
||||
|
||||
const EXPLICABLE_TYPES = ['string', 'number', 'boolean']
|
||||
function isExplicable (value) {
|
||||
const explicitTypes = ['string', 'number', 'boolean']
|
||||
return explicitTypes.some(elem => value.toLowerCase() === elem)
|
||||
return EXPLICABLE_TYPES.some(elem => value.toLowerCase() === elem)
|
||||
}
|
||||
|
||||
function isBoolean (...args) {
|
||||
|
@ -241,6 +241,16 @@ describe('Options props', () => {
|
||||
makeInstance({}, Symbol)
|
||||
expect('Expected Symbol, got Object').toHaveBeenWarned()
|
||||
})
|
||||
|
||||
it('warns when expected an explicable type but Symbol was provided', () => {
|
||||
makeInstance(Symbol('foo'), String)
|
||||
expect('Expected String, got Symbol').toHaveBeenWarned()
|
||||
})
|
||||
|
||||
it('warns when expected an explicable type but Symbol was provided', () => {
|
||||
makeInstance(Symbol('foo'), [String, Number])
|
||||
expect('Expected String, Number, got Symbol').toHaveBeenWarned()
|
||||
})
|
||||
}
|
||||
|
||||
it('custom constructor', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user