Skip to content

Instantly share code, notes, and snippets.

@B0und
Created October 4, 2023 20:15
Show Gist options
  • Save B0und/a59dfbbc1fd746c93c559dfeedaf69e8 to your computer and use it in GitHub Desktop.
Save B0und/a59dfbbc1fd746c93c559dfeedaf69e8 to your computer and use it in GitHub Desktop.
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow generic variable names to promote descriptive naming",
category: "Stylistic Issues",
recommended: false,
url: "https://github.com/yourusername/eslint-plugin-yourpluginname/blob/main/docs/rules/no-generic-names.md"
},
schema: [],
messages: {
genericName: "Avoid generic variable names. '{{name}}' is too generic."
}
},
create(context) {
const genericNames = ["data", "item", "obj", "arr", "num", "str", "val", "info", "list", "el", "element"];
return {
VariableDeclarator(node) {
if (genericNames.includes(node.id.name)) {
context.report({
node: node.id,
messageId: 'genericName',
data: {
name: node.id.name
}
});
}
}
};
}
};
const rule = require('../../../lib/rules/no-generic-names');
const { RuleTester } = require('eslint');
const ruleTester = new RuleTester({
parserOptions: { ecmaVersion: 2020 }
});
ruleTester.run('no-generic-names', rule, {
valid: [
'const userData = fetchUserData();',
'const menuItem = getMenuItems();',
'const userPosts = getUserPosts();',
'const configObject = getConfigurations();'
],
invalid: [
{
code: 'const data = fetchData();',
errors: [
{ message: "Avoid generic variable names. 'data' is too generic." }
]
},
{
code: 'const item = getItem();',
errors: [
{ message: "Avoid generic variable names. 'item' is too generic." }
]
},
{
code: 'const arr = getArray();',
errors: [
{ message: "Avoid generic variable names. 'arr' is too generic." }
]
},
{
code: 'const obj = getObject();',
errors: [
{ message: "Avoid generic variable names. 'obj' is too generic." }
]
}
]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment