diff --git a/src/compile/Component.ts b/src/compile/Component.ts index e153fc6b6d..05bab19cc5 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -260,8 +260,7 @@ export default class Component { if (this.defaultExport) { const categories = { components: 'component', - // TODO helpers require a bit more work — need to analyse all expressions - // helpers: 'helper', + helpers: 'helper', events: 'event definition', transitions: 'transition', actions: 'actions', diff --git a/src/compile/nodes/shared/Expression.ts b/src/compile/nodes/shared/Expression.ts index ddc1007e7b..6acb6282ec 100644 --- a/src/compile/nodes/shared/Expression.ts +++ b/src/compile/nodes/shared/Expression.ts @@ -115,6 +115,8 @@ export default class Expression { let object = node; while (object.type === 'MemberExpression') object = object.object; + component.used.helpers.add(name); + const alias = component.templateVars.get(`helpers-${name}`); if (alias !== name) code.overwrite(object.start, object.end, alias); return; diff --git a/test/validator/samples/method-nonexistent-helper/warnings.json b/test/validator/samples/method-nonexistent-helper/warnings.json index d1999de882..6b8049931e 100644 --- a/test/validator/samples/method-nonexistent-helper/warnings.json +++ b/test/validator/samples/method-nonexistent-helper/warnings.json @@ -1,15 +1,32 @@ -[{ - "code": "invalid-callee", - "message": "'foo' is an invalid callee (should be one of this.*, refs.*, event.*, options.*, console.*, set, fire, destroy or bar). 'foo' exists on 'helpers', did you put it in the wrong place?", - "pos": 18, - "start": { - "line": 1, - "column": 18, - "character": 18 +[ + { + "code": "invalid-callee", + "message": "'foo' is an invalid callee (should be one of this.*, refs.*, event.*, options.*, console.*, set, fire, destroy or bar). 'foo' exists on 'helpers', did you put it in the wrong place?", + "pos": 18, + "start": { + "line": 1, + "column": 18, + "character": 18 + }, + "end": { + "line": 1, + "column": 23, + "character": 23 + } }, - "end": { - "line": 1, - "column": 23, - "character": 23 + { + "code": "unused-helper", + "message": "The 'foo' helper is unused", + "start": { + "line": 12, + "column": 3, + "character": 126 + }, + "end": { + "line": 14, + "column": 4, + "character": 156 + }, + "pos": 126 } -}] +] diff --git a/test/validator/samples/unused-helper.skip/warnings.json b/test/validator/samples/unused-helper.skip/warnings.json deleted file mode 100644 index 08b91c241e..0000000000 --- a/test/validator/samples/unused-helper.skip/warnings.json +++ /dev/null @@ -1,8 +0,0 @@ -[{ - "message": "The 'uppercase' helper is unused", - "start": { - "line": 4, - "column": 3 - }, - "pos": 43 -}] diff --git a/test/validator/samples/unused-helper.skip/input.html b/test/validator/samples/unused-helper/input.html similarity index 100% rename from test/validator/samples/unused-helper.skip/input.html rename to test/validator/samples/unused-helper/input.html diff --git a/test/validator/samples/unused-helper/warnings.json b/test/validator/samples/unused-helper/warnings.json new file mode 100644 index 0000000000..abd2e97e05 --- /dev/null +++ b/test/validator/samples/unused-helper/warnings.json @@ -0,0 +1,15 @@ +[{ + "code": "unused-helper", + "message": "The 'uppercase' helper is unused", + "start": { + "line": 4, + "column": 3, + "character": 43 + }, + "end": { + "line": 4, + "column": 34, + "character": 74 + }, + "pos": 43 +}]