Skip to content

Commit 025e763

Browse files
CodinCatyyx990803
authored andcommitted
Warn when not passing props in kebab-case (vuejs#5161)
* Warn when not passing props in kebab-case * Move keyInLowerCase to dev only block
1 parent 4d227b9 commit 025e763

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/core/vdom/create-component.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,18 @@ function extractProps (data: VNodeData, Ctor: Class<Component>): ?Object {
286286
if (attrs || props || domProps) {
287287
for (const key in propOptions) {
288288
const altKey = hyphenate(key)
289+
if (process.env.NODE_ENV !== 'production') {
290+
const keyInLowerCase = key.toLowerCase()
291+
if (
292+
key !== keyInLowerCase &&
293+
attrs && attrs.hasOwnProperty(keyInLowerCase)
294+
) {
295+
warn(
296+
`HTML attributes are case-insensitive. camelCased prop names need ` +
297+
`to use their kebab-case equivalents. ${key} should be ${altKey}.`
298+
)
299+
}
300+
}
289301
checkProp(res, props, key, altKey, true) ||
290302
checkProp(res, attrs, key, altKey) ||
291303
checkProp(res, domProps, key, altKey)

test/unit/features/component/component.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,25 @@ describe('Component', () => {
258258
expect(vm.$el.outerHTML).toBe('<ul><li>1</li><li>2</li></ul>')
259259
})
260260

261+
it('should warn when not passing props in kebab-case', () => {
262+
new Vue({
263+
data: {
264+
list: [{ a: 1 }, { a: 2 }]
265+
},
266+
template: '<test :somecollection="list"></test>',
267+
components: {
268+
test: {
269+
template: '<ul><li v-for="item in someCollection">{{item.a}}</li></ul>',
270+
props: ['someCollection']
271+
}
272+
}
273+
}).$mount()
274+
expect(
275+
'HTML attributes are case-insensitive. camelCased prop names need ' +
276+
'to use their kebab-case equivalents. someCollection should be some-collection.'
277+
).toHaveBeenWarned()
278+
})
279+
261280
it('not found component should not throw', () => {
262281
expect(function () {
263282
new Vue({

0 commit comments

Comments
 (0)