Skip to content
← Back to rules

eslint/id-length Style

What it does

This rule enforces a minimum and/or maximum identifier length convention by counting the graphemes for a given identifier.

Why is this bad?

Very short identifier names like e, x, _t or very long ones like hashGeneratorResultOutputContainerObject can make code harder to read and potentially less maintainable. To prevent this, one may enforce a minimum and/or maximum identifier length.

Examples

Examples of incorrect code for this rule:

js
/* id-length: "error" */ // default is minimum 2-chars ({ "min": 2 })

const x = 5;
obj.e = document.body;
const foo = function (e) {};
try {
  dangerousStuff();
} catch (e) {
  // ignore as many do
}
const myObj = { a: 1 };
(a) => {
  a * a;
};
class y {}
class Foo {
  x() {}
}
class Bar {
  #x() {}
}
class Baz {
  x = 1;
}
class Qux {
  #x = 1;
}
function bar(...x) {}
function baz([x]) {}
const [z] = arr;
const {
  prop: [i],
} = {};
function qux({ x }) {}
const { j } = {};
const { prop: a } = {};
({ prop: obj.x } = {});

Examples of correct code for this rule:

js
/* id-length: "error" */ // default is minimum 2-chars ({ "min": 2 })

const num = 5;
function _f() {
  return 42;
}
function _func() {
  return 42;
}
obj.el = document.body;
const foo = function (evt) {
  /* do stuff */
};
try {
  dangerousStuff();
} catch (error) {
  // ignore as many do
}
const myObj = { apple: 1 };
(num) => {
  num * num;
};
function bar(num = 0) {}
class MyClass {}
class Foo {
  method() {}
}
class Bar {
  #method() {}
}
class Baz {
  field = 1;
}
class Qux {
  #field = 1;
}
function baz(...args) {}
function qux([longName]) {}
const { prop } = {};
const {
  prop: [name],
} = {};
const [longName] = arr;
function foobar({ prop }) {}
function foobaz({ a: prop }) {}
const { a: property } = {};
({ prop: obj.longName } = {});
const data = { x: 1 }; // excused because of quotes
data["y"] = 3; // excused because of calculated property access

Configuration

This rule accepts a configuration object with the following properties:

checkGeneric

type: boolean

default: true

Whether to check TypeScript generic type parameter names. Defaults to true.

exceptionPatterns

type: string[]

An array of regex patterns for identifiers to exclude from the rule. For example, ["^x.*"] would exclude all identifiers starting with "x".

exceptions

type: string[]

default: []

An array of identifier names that are excluded from the rule. For example, ["x", "y", "z"] would allow single-letter identifiers "x", "y", and "z".

max

type: integer

default: Infinity

The maximum number of graphemes allowed in an identifier. Defaults to no maximum (effectively unlimited).

min

type: integer

default: 2

The minimum number of graphemes required in an identifier.

properties

type: "always" | "never"

default: "always"

Whether to check property names for length.

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "id-length": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "id-length": "error",
  },
});
bash
oxlint --deny id-length

Version

This rule was added in v1.4.0.

References