eslint-plugin-code-metrics
v0.2.1
Published
ESLint rules for code metrics: Maintainability Index (Visual Studio formula), Halstead volume, cyclomatic complexity and lines of code.
Maintainers
Readme
eslint-plugin-code-metrics
ESLint rules that measure code metrics per function for JavaScript and TypeScript, modeled on the Visual Studio code metrics.
| Rule | Reports when |
| --- | --- |
| code-metrics/maintainability-index | Maintainability Index < threshold |
| code-metrics/halstead-volume | Halstead volume > max |
| code-metrics/cyclomatic-complexity | cyclomatic complexity > max |
| code-metrics/lines-of-code | lines of code > max |
Install
npm install --save-dev eslint-plugin-code-metricsRequires ESLint 9+ (flat config). For TypeScript files, use @typescript-eslint/parser.
Usage
// eslint.config.js
import codeMetrics from 'eslint-plugin-code-metrics';
export default [codeMetrics.configs.recommended];recommended:maintainability-indexaswarn.strict: all four rules aserror, with default options.
Custom options:
import tsParser from '@typescript-eslint/parser';
import codeMetrics from 'eslint-plugin-code-metrics';
export default [
{
plugins: { 'code-metrics': codeMetrics },
rules: {
'code-metrics/maintainability-index': ['error', { threshold: 10 }],
'code-metrics/cyclomatic-complexity': ['warn', { max: 15 }],
},
},
{ files: ['**/*.ts'], languageOptions: { parser: tsParser } },
];Options
| Rule | Option | Type | Default | Allowed values |
| --- | --- | --- | --- | --- |
| maintainability-index | threshold | number | 20 | 0 to 100 |
| maintainability-index | ignoreTypeAnnotations | boolean | true | |
| halstead-volume | max | number | 1000 | 0 or more |
| halstead-volume | ignoreTypeAnnotations | boolean | true | |
| cyclomatic-complexity | max | integer | 10 | 0 or more |
| lines-of-code | max | integer | 60 | 0 or more |
A value outside the allowed range (for example threshold: 101) makes ESLint reject the config.
threshold: the Maintainability Index is never above 100, sothreshold: 100reports every function except the ones with MI 100 (for example empty functions).ignoreTypeAnnotations: skip TypeScript type syntax (: T,<T>,as T,satisfies T, localtype/interface) when counting Halstead operators and operands, so typed code is not penalized compared to the same JavaScript. It changes the Halstead volume, so it affects both rules that use it.max: 0reports every function, which is useful to see the value of each function.
Maintainability Index
Same formula as Visual Studio:
MI = MAX(0, (171 - 5.2 * ln(Halstead Volume) - 0.23 * Cyclomatic Complexity - 16.2 * ln(Lines of Code)) * 100 / 171)| Range | Band | Meaning | | --- | --- | --- | | 20–100 | green | good maintainability | | 10–19 | yellow | moderately maintainable | | 0–9 | red | low maintainability |
The rule uses message veryLow for the red band and low otherwise. An ESLint rule has one severity,
so to split the bands use the threshold: ['warn', { threshold: 20 }] reports yellow and red,
['error', { threshold: 10 }] reports only red.
How metrics are measured
Each function, function expression, method and arrow function is measured separately. A nested function
is not counted in the metrics of its parent.
- Cyclomatic complexity: 1 + one per
if,?:,case(notdefault),for,for...in,for...of,while,do...while,catch,&&,||,??,&&=,||=,??=, default value (a = 1) and optional chaining (a?.b,a?.()). This is the same count as the corecomplexityrule. - Halstead volume:
N * log2(n), from the function tokens. Identifiers, literals, templates,thisandsuperare operands. Keywords and punctuators are operators. Closing brackets are not counted, so a bracket pair counts once. - Lines of code: distinct lines with at least one code token. Blank lines and comment-only lines are not counted.
Code outside functions (top-level code, class field initializers, static blocks) is not measured.
Differences from core rules: cyclomatic-complexity gives the same value as the core complexity rule for
every function, but does not measure class field initializers. lines-of-code differs from
max-lines-per-function because it always skips blank lines, comments and nested functions.
The values are not identical to Visual Studio: the formula is the same, but Halstead volume and lines of code are measured on JavaScript tokens, not on C# syntax or IL.
Development
npm install
npm test
npm run build
npm run examplenpm run example lints the files in examples/ with the built plugin and the strict config. It reports
problems on purpose.
npm run test:smoke -- <eslint version> (after npm run build) packs the package, installs it in a new
project with that ESLint version, and lints the examples through an ESM and a CommonJS config. CI runs it with
ESLint 9 and 10 on Node 18.18 to 24.
License
MIT © Duy Pham
