Sub topic 11
Enforcing JSDoc with ESLint
Comments drift. A parameter gets renamed, a new argument is added, and the JSDoc block above the function keeps describing the old signature. eslint-plugin-jsdoc is an ESLint plugin that reads your JSDoc blocks and reports problems such as missing tags, names that don't match the real parameters, and tags that don't exist.
This chapter assumes you already have a working Node.js project that uses ES modules, you can run commands with npx, and you know basic ESLint concepts such as rules and severities ("off", "warn", "error").
Installing the plugin
Install ESLint and the plugin as development dependencies:
npm install --save-dev eslint eslint-plugin-jsdocESLint 9 uses a "flat config" file named eslint.config.js. The smallest useful setup turns on the plugin's recommended rule set:
// eslint.config.js
import jsdoc from 'eslint-plugin-jsdoc';
export default [
jsdoc.configs['flat/recommended'],
];Run it against your source folder:
npx eslint srcWhat the recommended rules catch
The recommended set reports problems as warnings. Use flat/recommended-error if you want them reported as errors. The rules you will run into most often are listed below.
| Rule | What it reports |
|---|---|
| `jsdoc/require-jsdoc` | A function declaration with no JSDoc block at all |
| `jsdoc/require-param` | A function parameter with no matching `@param` tag |
| `jsdoc/check-param-names` | A `@param` name that doesn't match the real parameter, or is in the wrong order |
| `jsdoc/require-returns` | A function that returns a value but has no `@returns` tag |
| `jsdoc/check-tag-names` | Unknown or misspelled tags, such as `@parma` |
| `jsdoc/check-types` | Type names written in a non-preferred form |
| `jsdoc/no-undefined-types` | A type name that is never defined or imported |
Here is a file with two common mistakes:
// src/math.js
/**
* Adds two numbers.
* @param {number} a - First number.
* @returns {number} The sum.
*/
export function add(a, b) {
return a + b;
}
/**
* Scales a value.
* @param {number} x - The value to scale.
* @param {number} factor - The multiplier.
* @returns {number} The scaled value.
*/
export function scale(value, factor) {
return value * factor;
}Running npx eslint src reports a missing @param "b" declaration on add (from require-param) and a name mismatch on scale, where x should be value (from check-param-names). Neither mistake would cause a runtime error. Both would mislead anyone who reads the generated docs or hovers over the function in an editor.
Auto-fixing
Some rules can fix their own problems. For example, require-param can insert a stub @param tag for a missing parameter:
npx eslint src --fixYou still have to fill in the type and description yourself. Treat --fix as a way to add the missing tags for you, not as a replacement for writing the docs.
Tuning the rules
Projects rarely want the defaults unchanged. To override rules, add a second config object after the recommended one. Plugins registered in one config object are available to later ones, so you don't need to import the plugin again.
// eslint.config.js
import jsdoc from 'eslint-plugin-jsdoc';
export default [
jsdoc.configs['flat/recommended'],
{
files: ['src/**/*.js'],
rules: {
'jsdoc/require-jsdoc': ['error', {
publicOnly: true,
require: {
FunctionDeclaration: true,
ClassDeclaration: true,
MethodDefinition: true,
},
}],
'jsdoc/check-param-names': 'error',
'jsdoc/require-param-description': 'off',
},
},
];This config makes the following changes:
- It requires docs only on exported code (
publicOnly: true), so internal helpers don't need them. - It extends
require-jsdocto cover classes and methods, because by default it checks only function declarations. - It turns name mismatches into errors, so they fail the build.
- It stops requiring a description on every
@param.
Where linting fits
ESLint checks the shape of your JSDoc. TypeScript's checkJs mode, covered in an earlier chapter, checks whether the types agree with the code. The two tools complement each other, and most teams run both in CI.
Common errors
- Too many
no-undefined-typesreports. These often appear for types imported withimport()or provided by globals. Check that the type really exists and is spelled correctly before you disable the rule. require-jsdocreports nothing on arrow functions. By default it checks only function declarations. AddArrowFunctionExpression: trueunderrequireif you export arrow functions.- Every rule fires across a large legacy codebase. Limit
filesto new directories, or start with warnings and tighten them over time.
Exercise
A function export function greet(name, greeting) {} has a JSDoc block with only @param {string} name. With flat/recommended enabled, which rule reports this, and what does npx eslint src --fix do about it?
Show answer
jsdoc/require-param reports the missing @param for greeting. Running with --fix inserts a stub @param tag for greeting, which you then complete with a type and description.
Exercise
You want undocumented exported functions to fail CI but internal helpers to be ignored. Which rule and option do you configure?
Show answer
Set jsdoc/require-jsdoc to "error" with the option publicOnly: true, so only exported functions, classes, and methods must have JSDoc blocks.