Created
October 4, 2023 20:15
-
-
Save B0und/a59dfbbc1fd746c93c559dfeedaf69e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}); | |
} | |
} | |
}; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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