Skip to content

Instantly share code, notes, and snippets.

@B0und
Created October 4, 2023 20:12
Show Gist options
  • Save B0und/289b20094cf8a4d6728a97aa3c6c5319 to your computer and use it in GitHub Desktop.
Save B0und/289b20094cf8a4d6728a97aa3c6c5319 to your computer and use it in GitHub Desktop.
eslint rules
const rule = require('../../../lib/rules/short-variable-name');
const { RuleTester } = require('eslint');
const ruleTester = new RuleTester({
parserOptions: { ecmaVersion: 2020 }
});
ruleTester.run('short-variable-name', rule, {
valid: [
'const alpha = 20;',
'const arr = data.filter(i => Boolean(i));',
'function test(alpha) {}',
'const fn = function(alpha) {};',
'const fn = (alpha, beta) => alpha + beta;'
],
invalid: [
{
code: 'const a = 20;',
errors: [
{ message: 'Variable name should be longer than 3 characters.' }
]
},
{
code: 'let ab = 20;',
errors: [
{ message: 'Variable name should be longer than 3 characters.' }
]
},
{
code: 'var abc = 20;',
errors: [
{ message: 'Variable name should be longer than 3 characters.' }
]
},
{
code: 'function test(a, b) {}',
errors: [
{ message: 'Variable name should be longer than 3 characters.' },
{ message: 'Variable name should be longer than 3 characters.' }
]
},
{
code: 'const fn = function(a, b) {};',
errors: [
{ message: 'Variable name should be longer than 3 characters.' },
{ message: 'Variable name should be longer than 3 characters.' }
]
}
]
});
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow variable names that are less than or equal to 3 characters outside lambda functions",
category: "Stylistic Issues",
recommended: false,
url: "https://github.com/yourusername/eslint-plugin-yourpluginname/blob/main/docs/rules/short-variable-name.md"
},
schema: [],
messages: {
tooShort: "Variable name '{{name}}' should be longer than 3 characters."
}
},
create(context) {
return {
VariableDeclarator(node) {
const inArrowFunction = node.init && node.init.type === 'ArrowFunctionExpression';
if (!inArrowFunction && node.id.name.length <= 3) {
context.report({
node: node.id,
messageId: 'tooShort',
data: {
name: node.id.name
}
});
}
},
FunctionDeclaration(node) {
node.params.forEach(param => {
if (param.name.length <= 3) {
context.report({
node: param,
messageId: 'tooShort',
data: {
name: param.name
}
});
}
});
},
FunctionExpression(node) {
if (node.type !== 'ArrowFunctionExpression') {
node.params.forEach(param => {
if (param.name.length <= 3) {
context.report({
node: param,
messageId: 'tooShort',
data: {
name: param.name
}
});
}
});
}
}
};
}
};
```
### Documentation
Create a Markdown file for your rule's documentation.
**docs/rules/short-variable-name.md**:
```markdown
# disallow variable names that are less than or equal to 3 characters outside lambda functions (short-variable-name)
This rule enforces the use of variable names that are longer than 3 characters, with an exception for lambda function parameters.
## Rule Details
This rule aims to improve code readability by ensuring variable names are descriptive.
Examples of **incorrect** code for this rule:
```js
const a = 20;
let abc = "name";
function check(a, b) {}
const func = function(x, y) {};
```
Examples of **correct** code for this rule:
```js
const name = "John";
const items = data.filter(i => Boolean(i));
function test(itemOne, itemTwo) {}
const combine = (alpha, beta) => alpha + beta;
```
## When Not To Use It
If you're not concerned about the length of variable names or if you have specific scenarios where shorter variable names are desired, you may wish to disable this rule.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment