From bae9958a01464bea4d9496b7e7b86755098f5585 Mon Sep 17 00:00:00 2001 From: ota Date: Thu, 31 Jan 2019 10:24:43 +0900 Subject: [PATCH 1/3] fix(no-unused-components): Ignore names that can not be identified Fixed #768 --- lib/utils/index.js | 9 +-- tests/lib/rules/no-unused-components.js | 79 +++++++++++++++++++++++++ tests/lib/utils/index.js | 19 ++++++ 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/lib/utils/index.js b/lib/utils/index.js index e2af0257f..4c90e310b 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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) }, /** diff --git a/tests/lib/rules/no-unused-components.js b/tests/lib/rules/no-unused-components.js index f62b83fb1..df5a4e7e3 100644 --- a/tests/lib/rules/no-unused-components.js +++ b/tests/lib/rules/no-unused-components.js @@ -411,7 +411,52 @@ tester.run('no-unused-components', rule, { } } ` + }, + + // computed propertys + { + filename: 'test.vue', + code: ` + + ` + }, + { + filename: 'test.vue', + code: ` + + ` + }, + { + filename: 'test.vue', + code: ` + + ` } + ], invalid: [ { @@ -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: ` + + `, + 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 + }] } ] }) diff --git a/tests/lib/utils/index.js b/tests/lib/utils/index.js index 0b8c07e2d..f698a98ef 100644 --- a/tests/lib/utils/index.js +++ b/tests/lib/utils/index.js @@ -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', () => { From 4847f9fb6271c485acf22a9e72f61efac7b41f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sajn=C3=B3g?= Date: Sun, 3 Feb 2019 10:04:41 +0700 Subject: [PATCH 2/3] Update no-unused-components.js --- tests/lib/rules/no-unused-components.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/rules/no-unused-components.js b/tests/lib/rules/no-unused-components.js index df5a4e7e3..5522126b5 100644 --- a/tests/lib/rules/no-unused-components.js +++ b/tests/lib/rules/no-unused-components.js @@ -413,7 +413,7 @@ tester.run('no-unused-components', rule, { ` }, - // computed propertys + // computed properties { filename: 'test.vue', code: ` From 01a3e514ed49634b8f0912707feeb7bdd3d08581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sajn=C3=B3g?= Date: Sun, 3 Feb 2019 10:05:27 +0700 Subject: [PATCH 3/3] Update no-unused-components.js --- tests/lib/rules/no-unused-components.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/rules/no-unused-components.js b/tests/lib/rules/no-unused-components.js index 5522126b5..cf6e897e6 100644 --- a/tests/lib/rules/no-unused-components.js +++ b/tests/lib/rules/no-unused-components.js @@ -559,7 +559,7 @@ tester.run('no-unused-components', rule, { }] }, - // computed propertys + // computed properties { filename: 'test.vue', code: `