name rule

This commit is contained in:
Niels Lyngsø
2024-01-24 14:08:17 +01:00
parent 6d7a496abb
commit 09238161ac
2 changed files with 45 additions and 1 deletions

View File

@@ -0,0 +1,41 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Enforce Custom Element names to start with "umb-".',
category: 'Naming',
recommended: true,
},
schema: [],
},
create: function (context) {
return {
CallExpression(node) {
// check if the expression is @customElement decorator
const isCustomElementDecorator =
node.callee.type === 'Identifier' &&
node.callee.name === 'customElement' &&
node.arguments.length === 1 &&
node.arguments[0].type === 'Literal' &&
typeof node.arguments[0].value === 'string';
if (isCustomElementDecorator) {
const elementName = node.arguments[0].value;
// check if the element name starts with 'umb-'
const isElementNameValid = elementName.startsWith('umb-');
if (!isElementNameValid) {
context.report({
node,
message: 'Custom Element name should start with "umb-".',
// There is no fixer on purpose because it's not safe to automatically rename the element name.
// Renaming should be done manually with consideration of potential impacts.
});
}
}
},
};
},
};