Skip to content

Commit ccf9bed

Browse files
committed
warn possible camelCase props
1 parent d7b55c4 commit ccf9bed

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

src/compiler/compile-props.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,28 @@ export function compileProps (el, propOptions) {
115115
} else if ((value = getAttr(el, attr)) !== null) {
116116
// has literal binding!
117117
prop.raw = value
118-
} else if (options.required) {
119-
// warn missing required
120-
process.env.NODE_ENV !== 'production' && warn(
121-
'Missing required prop: ' + name
118+
} else if (process.env.NODE_ENV !== 'production') {
119+
// check possible camelCase prop usage
120+
var lowerCaseName = path.toLowerCase()
121+
value = /[A-Z\-]/.test(name) && (
122+
el.getAttribute(lowerCaseName) ||
123+
el.getAttribute(':' + lowerCaseName) ||
124+
el.getAttribute('v-bind:' + lowerCaseName) ||
125+
el.getAttribute(':' + lowerCaseName + '.once') ||
126+
el.getAttribute('v-bind:' + lowerCaseName + '.once') ||
127+
el.getAttribute(':' + lowerCaseName + '.sync') ||
128+
el.getAttribute('v-bind:' + lowerCaseName + '.sync')
122129
)
130+
if (value) {
131+
warn(
132+
'Possible usage error for prop `' + lowerCaseName + '` - ' +
133+
'did you mean `' + attr + '`? HTML is case-insensitive, remember to use ' +
134+
'kebab-case for props in templates.'
135+
)
136+
} else if (options.required) {
137+
// warn missing required
138+
warn('Missing required prop: ' + name)
139+
}
123140
}
124141
// push prop
125142
props.push(prop)

src/util/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function checkComponentAttr (el, options) {
5353
warn(
5454
'Unknown custom element: <' + tag + '> - ' +
5555
'did you mean <' + expectedTag + '>? ' +
56-
'HTML is case-insensitive, remember to use kebab-case in templates!'
56+
'HTML is case-insensitive, remember to use kebab-case in templates.'
5757
)
5858
} else if (isUnknownElement(el, tag)) {
5959
warn(

test/unit/specs/directives/internal/component_spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,4 +530,15 @@ describe('Component', function () {
530530
})
531531
}).not.toThrow()
532532
})
533+
534+
it('warn possible camelCase components', function () {
535+
new Vue({
536+
el: document.createElement('div'),
537+
template: '<HelloWorld></HelloWorld>',
538+
components: {
539+
'hello-world': {}
540+
}
541+
})
542+
expect(hasWarned('did you mean <hello-world>?')).toBe(true)
543+
})
533544
})

test/unit/specs/directives/internal/prop_spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,4 +665,20 @@ describe('prop', function () {
665665
expect(vm.$refs.child.propA).toBe(true)
666666
expect(vm.$refs.child.propB).toBe(false)
667667
})
668+
669+
it('detect possible camelCase prop usage', function () {
670+
new Vue({
671+
el: el,
672+
template: '<comp propA="true" :propB="true" v-bind:propC.sync="true"></comp>',
673+
components: {
674+
comp: {
675+
props: ['propA', 'propB', 'prop-c']
676+
}
677+
}
678+
})
679+
expect(getWarnCount()).toBe(3)
680+
expect(hasWarned('did you mean `prop-a`')).toBe(true)
681+
expect(hasWarned('did you mean `prop-b`')).toBe(true)
682+
expect(hasWarned('did you mean `prop-c`')).toBe(true)
683+
})
668684
})

test/unit/specs/misc_spec.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,15 +473,4 @@ describe('Misc', function () {
473473
})
474474
expect(vm.$el.textContent).toBe('default content slot1')
475475
})
476-
477-
it('warn possible camelCase components', function () {
478-
new Vue({
479-
el: document.createElement('div'),
480-
template: '<HelloWorld></HelloWorld>',
481-
components: {
482-
'hello-world': {}
483-
}
484-
})
485-
expect(hasWarned('did you mean <hello-world>?')).toBe(true)
486-
})
487476
})

0 commit comments

Comments
 (0)