Skip to content

Commit 3981561

Browse files
committed
normalize prop options at merge time
1 parent 1d43db9 commit 3981561

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

src/compiler/compile.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ function makeChildLinkFn (linkFns) {
416416
* a props link function.
417417
*
418418
* @param {Element|DocumentFragment} el
419-
* @param {Array} propDescriptors
419+
* @param {Array} propOptions
420420
* @return {Function} propsLinkFn
421421
*/
422422

@@ -425,20 +425,13 @@ var settablePathRE = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\[[^\[\]]+\])*$/
425425
var literalValueRE = /^(true|false)$|^\d.*/
426426
var identRE = require('../parsers/path').identRE
427427

428-
function compileProps (el, propDescriptors) {
428+
function compileProps (el, propOptions) {
429429
var props = []
430-
var i = propDescriptors.length
431-
var descriptor, name, options, value, path, prop, literal, single
430+
var i = propOptions.length
431+
var options, name, value, path, prop, literal, single
432432
while (i--) {
433-
descriptor = propDescriptors[i]
434-
// normalize prop string/descriptor
435-
if (typeof descriptor === 'object') {
436-
name = descriptor.name
437-
options = descriptor
438-
} else {
439-
name = descriptor
440-
options = null
441-
}
433+
options = propOptions[i]
434+
name = options.name
442435
// props could contain dashes, which will be
443436
// interpreted as minus calculations by the parser
444437
// so we need to camelize the path here

src/instance/scope.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ exports._setData = function (newData) {
9494
if (props) {
9595
i = props.length
9696
while (i--) {
97-
key = props[i]
97+
key = props[i].name
9898
if (key !== '$data' && !newData.hasOwnProperty(key)) {
9999
newData.$set(key, oldData[key])
100100
}

src/util/misc.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ var config = require('../config')
1010

1111
exports.assertProp = function (prop, value) {
1212
var options = prop.options
13-
if (!options) {
14-
return true
15-
}
1613
var type = options.type
1714
var valid = true
1815
var expectedType

src/util/options.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,30 @@ function guardComponents (components) {
232232
}
233233
}
234234

235-
function guardProps (child) {
236-
var props = child.props
235+
/**
236+
* Ensure all props option syntax are normalized into the
237+
* Object-based format.
238+
*
239+
* @param {Object} options
240+
*/
241+
242+
function guardProps (options) {
243+
var props = options.props
237244
if (_.isPlainObject(props)) {
238-
child.props = Object.keys(props).map(function (key) {
245+
options.props = Object.keys(props).map(function (key) {
239246
var val = props[key]
240247
if (!_.isPlainObject(val)) {
241248
val = { type: val }
242249
}
243250
val.name = key
244251
return val
245252
})
253+
} else if (_.isArray(props)) {
254+
options.props = props.map(function (prop) {
255+
return typeof prop === 'string'
256+
? { name: prop }
257+
: prop
258+
})
246259
}
247260
}
248261

test/unit/specs/compiler/compile_spec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ if (_.inBrowser) {
161161
name: 'default-value',
162162
default: 123
163163
}
164-
]
164+
].map(function (p) {
165+
return typeof p === 'string'
166+
? { name: p }
167+
: p
168+
})
165169
var def = Vue.options.directives._prop
166170
el.setAttribute('a', '1')
167171
el.setAttribute('data-some-attr', '{{a}}')
@@ -230,7 +234,10 @@ if (_.inBrowser) {
230234
var def = Vue.options.directives._prop
231235
el.setAttribute('a', 'hi')
232236
el.setAttribute('b', '{{hi}}')
233-
compiler.compileAndLinkProps(vm, el, ['a', 'b'])
237+
compiler.compileAndLinkProps(vm, el, [
238+
{ name: 'a' },
239+
{ name: 'b' }
240+
])
234241
expect(vm._bindDir.calls.count()).toBe(0)
235242
expect(vm._data.a).toBe('hi')
236243
expect(hasWarned(_, 'Cannot bind dynamic prop on a root')).toBe(true)

0 commit comments

Comments
 (0)