eslint-plugin-lit
Version:
lit-html support for ESLint
67 lines (66 loc) • 2.54 kB
JavaScript
/**
* @fileoverview Disallows arrow functions in templates
* @author James Garbutt <https://github.com/43081j>
*/
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
export const rule = {
meta: {
docs: {
description: 'Disallows arrow functions in templates',
recommended: false,
url: 'https://github.com/43081j/eslint-plugin-lit/blob/master/docs/rules/no-template-arrow.md'
},
schema: [],
messages: {
noArrow: 'Arrow functions must not be used in templates, ' +
'a method should be passed directly like `${this.myMethod}` as it ' +
'will be bound automatically.'
}
},
create(context) {
// variables should be defined here
//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------
/**
* Determines whether a node is a disallowed expression
* or not.
*
* @param {ESTree.Node} node
* @return {boolean}
*/
function isDisallowedExpr(node) {
if (node.type === 'ArrowFunctionExpression' ||
node.type === 'FunctionExpression') {
return true;
}
if (node.type === 'ConditionalExpression') {
return (isDisallowedExpr(node.test) ||
isDisallowedExpr(node.consequent) ||
isDisallowedExpr(node.alternate));
}
return false;
}
//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
return {
TaggedTemplateExpression: (node) => {
if (node.type === 'TaggedTemplateExpression' &&
node.tag.type === 'Identifier' &&
node.tag.name === 'html') {
for (const expr of node.quasi.expressions) {
if (isDisallowedExpr(expr)) {
context.report({
node: expr,
messageId: 'noArrow'
});
}
}
}
}
};
}
};