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
+}]