Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(props): correctly warn when a provided prop is Symbol #10529

Merged
merged 2 commits into from Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/core/util/props.js
Expand Up @@ -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 &&
isExplicable(expectedType) &&
!isBoolean(expectedType, receivedType)) {
message += ` with value ${expectedValue}`
if (
expectedTypes.length === 1 &&
isExplicable(expectedType) &&
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
}
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/features/options/props.spec.js
Expand Up @@ -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', () => {
Expand Down