Skip to content

fix(no-unused-components): Ignore names that can not be identified #790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ module.exports = {

return componentsNode.value.properties
.filter(p => p.type === 'Property')
.map(node => ({
node,
name: this.getStaticPropertyName(node.key)
}))
.map(node => {
const name = this.getStaticPropertyName(node)
return name ? { node, name } : null
})
.filter(comp => comp != null)
},

/**
Expand Down
79 changes: 79 additions & 0 deletions tests/lib/rules/no-unused-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,52 @@ tester.run('no-unused-components', rule, {
}
}
</script>`
},

// computed propertys
{
filename: 'test.vue',
code: `
<template>
<div />
</template>
<script>
export default {
components: {
[foo.bar]: baz
}
}
</script>`
},
{
filename: 'test.vue',
code: `
<template>
<div />
</template>
<script>
export default {
components: {
[foo]: Bar
}
}
</script>`
},
{
filename: 'test.vue',
code: `
<template>
<foo />
</template>
<script>
export default {
components: {
['foo']: Foo
}
}
</script>`
}

],
invalid: [
{
Expand Down Expand Up @@ -512,6 +557,40 @@ tester.run('no-unused-components', rule, {
message: 'The "Bar" component has been registered but not used.',
line: 14
}]
},

// computed propertys
{
filename: 'test.vue',
code: `
<template>
<div />
</template>
<script>
export default {
components: {
['foo']: Foo,
[\`bar\`]: Bar,
['baz']: Baz,
[qux]: Qux,
...components,
quux,
}
}
</script>`,
errors: [{
message: 'The "foo" component has been registered but not used.',
line: 8
}, {
message: 'The "bar" component has been registered but not used.',
line: 9
}, {
message: 'The "baz" component has been registered but not used.',
line: 10
}, {
message: 'The "quux" component has been registered but not used.',
line: 13
}]
}
]
})
19 changes: 19 additions & 0 deletions tests/lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,25 @@ describe('getRegisteredComponents', () => {
['PrimaryButton', 'secondaryButton', 'the-modal', 'the_dropdown', 'the_input', 'SomeComponent'],
)
})

it('should return an array of only components whose names can be identified', () => {
node = parse(`const test = {
name: 'test',
components: {
...test,
Foo,
[bar]: Bar,
[baz.baz]: Baz,
[\`\${qux}\`]: Qux,
[\`Quux\`]: Quux
}
}`)

assert.deepEqual(
utils.getRegisteredComponents(node).map(c => c.name),
['Foo', 'Quux'],
)
})
})

describe('getComponentProps', () => {
Expand Down