@mindfiredigital/eslint-plugin-hub
Version:
eslint-plugin-hub is a powerful, flexible ESLint plugin that provides a curated set of rules to enhance code readability, maintainability, and prevent common errors. Whether you're working with vanilla JavaScript, TypeScript, React, or Angular, eslint-plu
45 lines (40 loc) • 1.24 kB
JavaScript
const { isVerb } = require('../../../utils/check-verb');
module.exports = {
rules: {
'function-descriptive': {
meta: {
type: 'suggestion',
docs: {
description:
'Enforce function names to start with a verb and be descriptive',
},
schema: [], // No options needed
messages: {
notDescriptive:
"Function name '{{name}}' should start with a verb and be descriptive.",
},
},
create(context) {
return {
FunctionDeclaration(node) {
const functionName = node.id && node.id.name;
// Ensure functionName is a string before proceeding
if (typeof functionName !== 'string') {
return;
}
// Extract the first word from camelCase function name
const firstWord = functionName.split(/(?=[A-Z])/)[0].toLowerCase();
// Check if the first word is a verb
if (!isVerb(firstWord)) {
context.report({
node: node.id,
messageId: 'notDescriptive',
data: { name: functionName },
});
}
},
};
},
},
},
};