From b52883cd5b20406fc2bd2f141fea1bb4e0bd2e5f Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 17 Sep 2019 10:54:24 +0200 Subject: [PATCH 1/2] fix(props): correctly warn when a provided prop is Symbol Fixes #10519 --- src/core/util/props.js | 21 +++++++++++---------- test/unit/features/options/props.spec.js | 10 ++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/core/util/props.js b/src/core/util/props.js index 1b57f77cdb5..aa69027efd0 100644 --- a/src/core/util/props.js +++ b/src/core/util/props.js @@ -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 } @@ -231,9 +232,9 @@ function styleValue (value, type) { } } -function isExplicable (value) { - const explicitTypes = ['string', 'number', 'boolean'] - return explicitTypes.some(elem => value.toLowerCase() === elem) +const EXPLICABLE_TYPES = ['string', 'number', 'boolean'] +function isExplicable(value) { + return EXPLICABLE_TYPES.some(elem => value.toLowerCase() === elem) } function isBoolean (...args) { diff --git a/test/unit/features/options/props.spec.js b/test/unit/features/options/props.spec.js index 5adedeb2e6e..b2bf482737b 100644 --- a/test/unit/features/options/props.spec.js +++ b/test/unit/features/options/props.spec.js @@ -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', () => { From 3d1c8f5c93b26546601b0992996eaa5021d9867b Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 17 Sep 2019 10:56:27 +0200 Subject: [PATCH 2/2] style: space before parens --- src/core/util/props.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/util/props.js b/src/core/util/props.js index aa69027efd0..26a76664ccc 100644 --- a/src/core/util/props.js +++ b/src/core/util/props.js @@ -233,7 +233,7 @@ function styleValue (value, type) { } const EXPLICABLE_TYPES = ['string', 'number', 'boolean'] -function isExplicable(value) { +function isExplicable (value) { return EXPLICABLE_TYPES.some(elem => value.toLowerCase() === elem) }