ember-template-lint
Version:
Linter for Ember or Handlebars templates.
52 lines (44 loc) • 1.22 kB
JavaScript
;
const Rule = require('./_base');
const ERROR_MESSAGE = 'Invoke component directly instead of using `component` helper';
module.exports = class NoUnnecessaryComponentHelper extends Rule {
visitor() {
let inSafeNamespace = false;
const markAsSafeNamespace = {
enter() {
inSafeNamespace = true;
},
exit() {
inSafeNamespace = false;
},
};
function isComponentHelper(node) {
return (
node.path.type === 'PathExpression' &&
node.path.original === 'component' &&
node.params.length > 0
);
}
function checkNode(node) {
if (
isComponentHelper(node) &&
node.params[0].type === 'StringLiteral' &&
!node.params[0].value.includes('@') &&
!inSafeNamespace
) {
this.log({
message: ERROR_MESSAGE,
line: node.loc && node.loc.start.line,
column: node.loc && node.loc.start.column,
source: this.sourceForNode(node),
});
}
}
return {
AttrNode: markAsSafeNamespace,
BlockStatement: checkNode,
MustacheStatement: checkNode,
};
}
};
module.exports.ERROR_MESSAGE = ERROR_MESSAGE;