From ab940669321194ead0806c5a3a4d30ae3d7e64b7 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 24 Dec 2017 13:59:21 -0500 Subject: [PATCH] warn on unused events/transitions - fixes #1051 --- src/validate/html/validateElement.ts | 3 ++ src/validate/index.ts | 44 +++++++++++++------ .../input.html | 0 .../warnings.json | 4 +- .../validator/samples/unused-event/input.html | 9 ++++ .../samples/unused-event/warnings.json | 8 ++++ .../samples/unused-helper.skip/input.html | 7 +++ .../samples/unused-helper.skip/warnings.json | 8 ++++ .../samples/unused-transition/input.html | 9 ++++ .../samples/unused-transition/warnings.json | 8 ++++ 10 files changed, 84 insertions(+), 16 deletions(-) rename test/validator/samples/{component-unused => unused-components}/input.html (100%) rename test/validator/samples/{component-unused => unused-components}/warnings.json (59%) create mode 100644 test/validator/samples/unused-event/input.html create mode 100644 test/validator/samples/unused-event/warnings.json create mode 100644 test/validator/samples/unused-helper.skip/input.html create mode 100644 test/validator/samples/unused-helper.skip/warnings.json create mode 100644 test/validator/samples/unused-transition/input.html create mode 100644 test/validator/samples/unused-transition/warnings.json diff --git a/src/validate/html/validateElement.ts b/src/validate/html/validateElement.ts index 7c1af7d371..969be13fb0 100644 --- a/src/validate/html/validateElement.ts +++ b/src/validate/html/validateElement.ts @@ -138,12 +138,15 @@ export default function validateElement( ); } } else if (attribute.type === 'EventHandler') { + validator.used.events.add(attribute.name); validateEventHandler(validator, attribute, refCallees); } else if (attribute.type === 'Transition') { if (isComponent) { validator.error(`Transitions can only be applied to DOM elements, not components`, attribute.start); } + validator.used.transitions.add(attribute.name); + const bidi = attribute.intro && attribute.outro; if (hasTransition) { diff --git a/src/validate/index.ts b/src/validate/index.ts index e7bc9c5262..b57f2b0c2a 100644 --- a/src/validate/index.ts +++ b/src/validate/index.ts @@ -36,7 +36,10 @@ export class Validator { slots: Set; used: { - components: Set + components: Set; + helpers: Set; + events: Set; + transitions: Set; }; constructor(parsed: Parsed, source: string, options: CompileOptions) { @@ -56,7 +59,10 @@ export class Validator { this.slots = new Set(); this.used = { - components: new Set() + components: new Set(), + helpers: new Set(), + events: new Set(), + transitions: new Set() }; } @@ -125,18 +131,28 @@ export default function validate( // need to do a second pass of the JS, now that we've analysed the markup if (parsed.js && validator.defaultExport) { - const components = validator.defaultExport.declaration.properties.find(prop => prop.key.name === 'components'); - if (components) { - components.value.properties.forEach(prop => { - const { name } = prop.key; - if (!validator.used.components.has(name)) { - validator.warn( - `The ${name} component is unused`, - prop.start - ); - } - }); - } + const categories = { + components: 'component', + // TODO helpers require a bit more work — need to analyse all expressions + // helpers: 'helper', + events: 'event definition', + transitions: 'transition' + }; + + Object.keys(categories).forEach(category => { + const definitions = validator.defaultExport.declaration.properties.find(prop => prop.key.name === category); + if (definitions) { + definitions.value.properties.forEach(prop => { + const { name } = prop.key; + if (!validator.used[category].has(name)) { + validator.warn( + `The '${name}' ${categories[category]} is unused`, + prop.start + ); + } + }); + } + }); } } catch (err) { if (onerror) { diff --git a/test/validator/samples/component-unused/input.html b/test/validator/samples/unused-components/input.html similarity index 100% rename from test/validator/samples/component-unused/input.html rename to test/validator/samples/unused-components/input.html diff --git a/test/validator/samples/component-unused/warnings.json b/test/validator/samples/unused-components/warnings.json similarity index 59% rename from test/validator/samples/component-unused/warnings.json rename to test/validator/samples/unused-components/warnings.json index 4692da33aa..48e3d80bc9 100644 --- a/test/validator/samples/component-unused/warnings.json +++ b/test/validator/samples/unused-components/warnings.json @@ -1,6 +1,6 @@ [ { - "message": "The Foo component is unused", + "message": "The 'Foo' component is unused", "loc": { "line": 7, "column": 3 @@ -8,7 +8,7 @@ "pos": 109 }, { - "message": "The Bar component is unused", + "message": "The 'Bar' component is unused", "loc": { "line": 8, "column": 3 diff --git a/test/validator/samples/unused-event/input.html b/test/validator/samples/unused-event/input.html new file mode 100644 index 0000000000..b759d37749 --- /dev/null +++ b/test/validator/samples/unused-event/input.html @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/test/validator/samples/unused-event/warnings.json b/test/validator/samples/unused-event/warnings.json new file mode 100644 index 0000000000..88a47fbf86 --- /dev/null +++ b/test/validator/samples/unused-event/warnings.json @@ -0,0 +1,8 @@ +[{ + "message": "The 'drag' event definition is unused", + "loc": { + "line": 4, + "column": 3 + }, + "pos": 42 +}] diff --git a/test/validator/samples/unused-helper.skip/input.html b/test/validator/samples/unused-helper.skip/input.html new file mode 100644 index 0000000000..f948e195a3 --- /dev/null +++ b/test/validator/samples/unused-helper.skip/input.html @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/test/validator/samples/unused-helper.skip/warnings.json b/test/validator/samples/unused-helper.skip/warnings.json new file mode 100644 index 0000000000..fda70ebc0c --- /dev/null +++ b/test/validator/samples/unused-helper.skip/warnings.json @@ -0,0 +1,8 @@ +[{ + "message": "The 'uppercase' helper is unused", + "loc": { + "line": 4, + "column": 3 + }, + "pos": 43 +}] diff --git a/test/validator/samples/unused-transition/input.html b/test/validator/samples/unused-transition/input.html new file mode 100644 index 0000000000..4ddbc200e8 --- /dev/null +++ b/test/validator/samples/unused-transition/input.html @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/test/validator/samples/unused-transition/warnings.json b/test/validator/samples/unused-transition/warnings.json new file mode 100644 index 0000000000..da1952b060 --- /dev/null +++ b/test/validator/samples/unused-transition/warnings.json @@ -0,0 +1,8 @@ +[{ + "message": "The 'spin' transition is unused", + "loc": { + "line": 4, + "column": 3 + }, + "pos": 47 +}]