Skip to content

Commit 7c3198c

Browse files
committed
Progress with PR review
- Remove no longer needed variable. - Handle edge case where a component is registered using kebab-case but later on is used using PascalCase. e.g: registered as `foo-bar` and used as `FooBar` is not valid. - Handle edge case `<component is />`, where `node.value` would be `null`. See https://github.com/mysticatea/vue-eslint-parser/blob/master/docs/ast.md#vattribute - Add more tests to prove all these changes
1 parent b23457d commit 7c3198c

File tree

2 files changed

+76
-18
lines changed

2 files changed

+76
-18
lines changed

Diff for: lib/rules/no-unregistered-components.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,30 @@ module.exports = {
9797
}
9898
},
9999
"VAttribute[directive=false][key.name='is']" (node) {
100-
if (utils.isHtmlWellKnownElementName(node.value.value)) return
100+
if (
101+
!node.value || // `<component is />`
102+
utils.isHtmlWellKnownElementName(node.value.value)
103+
) return
101104
usedComponentNodes.push({ node, name: node.value.value })
102105
},
103106
"VElement[name='template']" (rootNode) {
104107
templateLocation = templateLocation || rootNode.loc.start
105108
},
106-
"VElement[name='template']:exit" (rootNode) {
107-
const registeredComponentNames = registeredComponents.map(({ name }) => casing.kebabCase(name))
109+
"VElement[name='template']:exit" () {
110+
// All registered components, transformed to kebab-case
111+
const registeredComponentNames = registeredComponents
112+
.map(({ name }) => casing.kebabCase(name))
113+
114+
// All registered components using kebab-case syntax
115+
const componentsRegisteredAsKebabCase = registeredComponents
116+
.filter(({ name }) => name === casing.kebabCase(name))
117+
.map(({ name }) => name)
108118

109119
usedComponentNodes
110120
.filter(({ name }) => {
111121
const kebabCaseName = casing.kebabCase(name)
122+
123+
// Check ignored patterns in first place
112124
if (ignorePatterns.find(pattern => {
113125
const regExp = new RegExp(pattern)
114126
return regExp.test(kebabCaseName) ||
@@ -117,6 +129,17 @@ module.exports = {
117129
regExp.test(casing.snakeCase(name)) ||
118130
regExp.test(name)
119131
})) return false
132+
133+
// Component registered as `foo-bar` cannot be used as `FooBar`
134+
if (
135+
name.indexOf('-') === -1 &&
136+
name === casing.pascalCase(name) &&
137+
componentsRegisteredAsKebabCase.indexOf(kebabCaseName) !== -1
138+
) {
139+
return true
140+
}
141+
142+
// Otherwise
120143
return registeredComponentNames.indexOf(kebabCaseName) === -1
121144
})
122145
.forEach(({ node, name }) => context.report({

Diff for: tests/lib/rules/no-unregistered-components.js

+50-15
Original file line numberDiff line numberDiff line change
@@ -324,21 +324,6 @@ tester.run('no-unregistered-components', rule, {
324324
</script>
325325
`
326326
},
327-
{
328-
filename: 'test.vue',
329-
code: `
330-
<template>
331-
<CustomComponent />
332-
</template>
333-
<script>
334-
export default {
335-
components: {
336-
'custom-component': InfoPrimaryWrapper
337-
}
338-
}
339-
</script>
340-
`
341-
},
342327
{
343328
filename: 'test.vue',
344329
code: `
@@ -378,6 +363,37 @@ tester.run('no-unregistered-components', rule, {
378363
</slot>
379364
</template>
380365
`
366+
},
367+
{
368+
filename: 'test.vue',
369+
code: `
370+
<template>
371+
<Custom-Component />
372+
</template>
373+
<script>
374+
export default {
375+
components: {
376+
'custom-component': InfoPrimaryWrapper
377+
}
378+
}
379+
</script>
380+
`
381+
},
382+
{
383+
filename: 'test.vue',
384+
code: `
385+
<template>
386+
<component is />
387+
</template>
388+
`
389+
},
390+
{
391+
filename: 'test.vue',
392+
code: `
393+
<template>
394+
<Component is />
395+
</template>
396+
`
381397
}
382398
],
383399
invalid: [
@@ -526,6 +542,25 @@ tester.run('no-unregistered-components', rule, {
526542
message: 'The "CustomComponent" component has been used but not registered.',
527543
line: 3
528544
}]
545+
},
546+
{
547+
filename: 'test.vue',
548+
code: `
549+
<template>
550+
<CustomComponent />
551+
</template>
552+
<script>
553+
export default {
554+
components: {
555+
'custom-component': InfoPrimaryWrapper
556+
}
557+
}
558+
</script>
559+
`,
560+
errors: [{
561+
message: 'The "CustomComponent" component has been used but not registered.',
562+
line: 3
563+
}]
529564
}
530565
]
531566
})

0 commit comments

Comments
 (0)