Skip to content

Commit a3707b1

Browse files
ota-meshimysticatea
authored andcommitted
Fix: ignore names that can not be identified (fixed #768) (#790)
1 parent 0f4861a commit a3707b1

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed

lib/utils/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,11 @@ module.exports = {
265265

266266
return componentsNode.value.properties
267267
.filter(p => p.type === 'Property')
268-
.map(node => ({
269-
node,
270-
name: this.getStaticPropertyName(node.key)
271-
}))
268+
.map(node => {
269+
const name = this.getStaticPropertyName(node)
270+
return name ? { node, name } : null
271+
})
272+
.filter(comp => comp != null)
272273
},
273274

274275
/**

tests/lib/rules/no-unused-components.js

+78
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,50 @@ tester.run('no-unused-components', rule, {
427427
<template>
428428
<component :is></component>
429429
</template>`
430+
},
431+
432+
// computed properties
433+
{
434+
filename: 'test.vue',
435+
code: `
436+
<template>
437+
<div />
438+
</template>
439+
<script>
440+
export default {
441+
components: {
442+
[foo.bar]: baz
443+
}
444+
}
445+
</script>`
446+
},
447+
{
448+
filename: 'test.vue',
449+
code: `
450+
<template>
451+
<div />
452+
</template>
453+
<script>
454+
export default {
455+
components: {
456+
[foo]: Bar
457+
}
458+
}
459+
</script>`
460+
},
461+
{
462+
filename: 'test.vue',
463+
code: `
464+
<template>
465+
<foo />
466+
</template>
467+
<script>
468+
export default {
469+
components: {
470+
['foo']: Foo
471+
}
472+
}
473+
</script>`
430474
}
431475
],
432476
invalid: [
@@ -566,6 +610,40 @@ tester.run('no-unused-components', rule, {
566610
message: 'The "Foo" component has been registered but not used.',
567611
line: 8
568612
}]
613+
},
614+
615+
// computed properties
616+
{
617+
filename: 'test.vue',
618+
code: `
619+
<template>
620+
<div />
621+
</template>
622+
<script>
623+
export default {
624+
components: {
625+
['foo']: Foo,
626+
[\`bar\`]: Bar,
627+
['baz']: Baz,
628+
[qux]: Qux,
629+
...components,
630+
quux,
631+
}
632+
}
633+
</script>`,
634+
errors: [{
635+
message: 'The "foo" component has been registered but not used.',
636+
line: 8
637+
}, {
638+
message: 'The "bar" component has been registered but not used.',
639+
line: 9
640+
}, {
641+
message: 'The "baz" component has been registered but not used.',
642+
line: 10
643+
}, {
644+
message: 'The "quux" component has been registered but not used.',
645+
line: 13
646+
}]
569647
}
570648
]
571649
})

tests/lib/utils/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,25 @@ describe('getRegisteredComponents', () => {
246246
['PrimaryButton', 'secondaryButton', 'the-modal', 'the_dropdown', 'the_input', 'SomeComponent'],
247247
)
248248
})
249+
250+
it('should return an array of only components whose names can be identified', () => {
251+
node = parse(`const test = {
252+
name: 'test',
253+
components: {
254+
...test,
255+
Foo,
256+
[bar]: Bar,
257+
[baz.baz]: Baz,
258+
[\`\${qux}\`]: Qux,
259+
[\`Quux\`]: Quux
260+
}
261+
}`)
262+
263+
assert.deepEqual(
264+
utils.getRegisteredComponents(node).map(c => c.name),
265+
['Foo', 'Quux'],
266+
)
267+
})
249268
})
250269

251270
describe('getComponentProps', () => {

0 commit comments

Comments
 (0)