Merge pull request #1052 from sveltejs/gh-1051

Warn on unused events/transitions
pull/1064/head
Rich Harris 7 years ago committed by GitHub
commit 57ec514654
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -138,12 +138,15 @@ export default function validateElement(
); );
} }
} else if (attribute.type === 'EventHandler') { } else if (attribute.type === 'EventHandler') {
validator.used.events.add(attribute.name);
validateEventHandler(validator, attribute, refCallees); validateEventHandler(validator, attribute, refCallees);
} else if (attribute.type === 'Transition') { } else if (attribute.type === 'Transition') {
if (isComponent) { if (isComponent) {
validator.error(`Transitions can only be applied to DOM elements, not components`, attribute.start); 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; const bidi = attribute.intro && attribute.outro;
if (hasTransition) { if (hasTransition) {

@ -36,7 +36,10 @@ export class Validator {
slots: Set<string>; slots: Set<string>;
used: { used: {
components: Set<string> components: Set<string>;
helpers: Set<string>;
events: Set<string>;
transitions: Set<string>;
}; };
constructor(parsed: Parsed, source: string, options: CompileOptions) { constructor(parsed: Parsed, source: string, options: CompileOptions) {
@ -56,7 +59,10 @@ export class Validator {
this.slots = new Set(); this.slots = new Set();
this.used = { 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 // need to do a second pass of the JS, now that we've analysed the markup
if (parsed.js && validator.defaultExport) { if (parsed.js && validator.defaultExport) {
const components = validator.defaultExport.declaration.properties.find(prop => prop.key.name === 'components'); const categories = {
if (components) { components: 'component',
components.value.properties.forEach(prop => { // TODO helpers require a bit more work — need to analyse all expressions
const { name } = prop.key; // helpers: 'helper',
if (!validator.used.components.has(name)) { events: 'event definition',
validator.warn( transitions: 'transition'
`The ${name} component is unused`, };
prop.start
); 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) { } catch (err) {
if (onerror) { if (onerror) {

@ -1,6 +1,6 @@
[ [
{ {
"message": "The Foo component is unused", "message": "The 'Foo' component is unused",
"loc": { "loc": {
"line": 7, "line": 7,
"column": 3 "column": 3
@ -8,7 +8,7 @@
"pos": 109 "pos": 109
}, },
{ {
"message": "The Bar component is unused", "message": "The 'Bar' component is unused",
"loc": { "loc": {
"line": 8, "line": 8,
"column": 3 "column": 3

@ -0,0 +1,9 @@
<script>
export default {
events: {
drag: (node, callback) => {
// implementation goes here
}
}
};
</script>

@ -0,0 +1,8 @@
[{
"message": "The 'drag' event definition is unused",
"loc": {
"line": 4,
"column": 3
},
"pos": 42
}]

@ -0,0 +1,7 @@
<script>
export default {
helpers: {
uppercase: x => x.toUpperCase()
}
};
</script>

@ -0,0 +1,8 @@
[{
"message": "The 'uppercase' helper is unused",
"loc": {
"line": 4,
"column": 3
},
"pos": 43
}]

@ -0,0 +1,9 @@
<script>
export default {
transitions: {
spin: node => {
// implementation goes here
}
}
};
</script>

@ -0,0 +1,8 @@
[{
"message": "The 'spin' transition is unused",
"loc": {
"line": 4,
"column": 3
},
"pos": 47
}]
Loading…
Cancel
Save