Hoc jsdoc

From openreachtech/hora-skills-ort-core · View interactive version

Hoc jsdoc is an AI agent skill from the GitHub repository openreachtech/hora-skills-ort-core. JSDoc writing conventions shared by backend and frontend. Use when writing or reviewing JSDoc, the type a tag carries included, in plain JavaScript or in Vue. Use it also when clearing a type error a checker such as `tsc` reports: the error is cleared by writing the annotation, and which annotations

How to use this skill

Skill file

Shared: JSDoc

Summarizes JSDoc writing conventions. All typing is JSDoc — no TypeScript syntax, no .ts

files — so always annotate types with JSDoc.

The rules in this body apply to every JavaScript file, backend and frontend alike.

How the jsdoc-plugin rules actually in effect under this project's ESLint map to

each of this skill's conventions is summarized in

eslint-jsdoc-rules.md. If you write JSDoc as

this skill prescribes, npm run lint produces no jsdoc-related errors. Note that

lint also enforces such points as: function declarations and methods must have

JSDoc (empty constructors are not exempt), each @param requires a type and a name,

and a function that returns a value needs @returns (with a type). This skill goes

one step further and requires @returns even when nothing is returned ({void}).

Write object types with named parameters

Example:

// NG
/**
 * @param {object} params - Parameters.
 * @param {string} params.alpha - Alpha.
 */

// OK
/**
 * @param {{
 *   alpha: string
 * }} params - Parameters.
 */

Name a single object argument `params`

// OK
/**
 * @param {{
 *   alpha: string
 * }} params - Parameters.
 */

Always write `@returns`

when it is async). Never omit the tag.

so this skill is stricter than lint here (see

eslint-jsdoc-rules.md). The reason is that "returns

nothing" should be stated, not inferred from the absence of a tag.

// NG: returns nothing, so @returns is omitted
/**
 * @param {{
 *   message: string
 * }} params - Parameters.
 */

// OK
/**
 * @param {{
 *   message: string
 * }} params - Parameters.
 * @returns {void}
 */

Attach a description to `@returns`

returned value to describe, so the type stands on its own.

@param convention (jsdoc/require-hyphen-before-param-description) and does not

apply to @returns.

but this skill always attaches one for QA reasons (see

eslint-jsdoc-rules.md).

// NG: no description
/**
 * @returns {string}
 */

// NG: a hyphen before the description
/**
 * @returns {string} - Default characters to pick from.
 */

// OK
/**
 * @returns {string} Default characters to pick from.
 */

Add `@public` to entry points

/**
 * Generate a random text of the given length.
 *
 * @param {{
 *   length: number
 * }} params - Parameters.
 * @returns {string | null} Generated random text, or null if it cannot be generated.
 * @public
 */
generate ({
  length,
}) {
  // ...
}

Write array types as `Array<T>`

// NG
/**
 * @param {string[]} values - Values.
 */

// OK
/**
 * @param {Array<string>} values - Values.
 */

Avoid vague types; write the most specific type you can

or as a concrete type name.

Array<object>; write Array<UserEntity> instead.

// NG: vague element type and return type
/**
 * @param {{
 *   users?: Array<object>
 * }} params - Parameters.
 * @returns {object} Users and their total count.
 */

// OK: investigate and write the detailed type
/**
 * @param {{
 *   users?: Array<UserEntity>
 * }} params - Parameters.
 * @returns {{
 *   users: Array<UserEntity>
 *   total: number
 * }} Users and their total count.
 */

`object` / `Object` is prohibited; use `Record<string, *>`

forced to represent an object with unknown keys, Record<string, *> is better.

wherever possible.

// NG
/**
 * @param {object} config - Config.
 */

// BETTER: when the keys are genuinely unknown
/**
 * @param {Record<string, *>} config - Config.
 */

// BEST: when you can list the properties
/**
 * @param {{
 *   host: string
 *   port: number
 * }} config - Config.
 */

Do not write undefined type names (define custom types before referencing them)

boolean / Array / * / null, etc.) and TS utility types (Record / Partial /

Pick / Omit / ReturnType, etc.) must be defined before it is referenced, via one of

@typedef / @class / @interface / import.

UserEntity must exist in the same file (or its import source).

// NG: UserEntity is not defined anywhere
/**
 * @param {{
 *   users?: Array<UserEntity>
 * }} params - Parameters.
 */

// OK: define it with @typedef before referencing it
/**
 * @typedef {{
 *   id: number
 *   name: string
 * }} UserEntity
 */

/**
 * @param {{
 *   users?: Array<UserEntity>
 * }} params - Parameters.
 */

The undefined-type-name check (jsdoc/no-undefined-types) is off by default in

@openreachtech/eslint-config, so this skill enforces it rather than relying on lint.

Do not write `undefined` as a type (use `null`)

(see "Always write @returns" above).

@returns and so on. If there is a way to avoid undefined, prefer that.

// NG
/**
 * @returns {string | undefined} Generated text.
 */

// OK
/**
 * @returns {string | null} Generated text.
 */

Write the any type as `*`

can" above. Write * (not any) only when the type cannot be narrowed and any is unavoidable.

// NG
/**
 * @param {any} value - Value.
 */

// OK
/**
 * @param {*} value - Value.
 */

A `*` cast on the right-hand side is prohibited

right-hand side.** Both /** @type {*} */ (...) and /** @type {Array<*>} */ ([...]) are

prohibited there, as is any written in place of *.

any. Every use of that variable downstream is unchecked, so the cast is not a type — it is a

switch that turns the checker off for everything the value reaches.

not assignable to Record<string, T>" is pointing at the union TypeScript inferred from the

literal, and the fix is to declare what the literal is. Casting deletes the message rather than

the defect.

inside the .create() of the MockXxxx stand-in**, so the cast bridges a deliberately partial

literal exactly once and every caller holds the real type. What that class is, where it lives

and what tests it belong to /hoc-jest.

over. A cast literal standing in for one of ours declares itself complete while holding two

members, and that claim is one the checker can never test — the type gains a member, the

literal does not, and nothing reports it. Every place that stood the type in has to be found

and corrected by hand, and nothing says which places those are. A real instance follows its own

class instead, so the change reaches the test the way it reaches everything else.

case that hands the code under test null, a missing key or another value its contract

refuses, on purpose. The declaration above cases states the contract type, and

/** @type {Array<*>} */ on the literal lets the violation through. It is allowed only where

the type error is the point of the test and plain to see; the test convention (/hoc-jest)

settles which describe() holds those cases.

test is written, so every type the test needs is already declared somewhere in the code under

test. A * there is never "the type cannot be narrowed"; it is "the type was not looked up".

// NG: nothing declares the type, and the cast removes the check
const cases = /** @type {Array<*>} */ ([
  {
    input: {
      errorHash: {
        Alpha: AlphaError,
      },
    },
  },
])

// OK: the declaration carries the type
/**
 * @type {Array<{
 *   input: {
 *     errorHash: Record<string, typeof RenchanGraphqlError>
 *   }
 * }>}
 */
const cases = [
  {
    input: {
      errorHash: {
        Alpha: AlphaError,
      },
    },
  },
]

// NG: the cast written where it is used, instead of inside the MockXxxx stand-in
/** @type {GraphqlType.ValidationContext} */
const mockContext = /** @type {*} */ ({
  getType: () => null,
  reportError: () => {},
})

// NG: a stand-in for a type the project owns, wherever it is written
/** @type {GraphqlType.ServerEngine} */
const mockEngine = /** @type {*} */ ({
  env: {
    isProduction: () => true,
  },
})

(see /hoc-statements), and var errors under lint.

A type is stated in three places only

A @type annotation or a cast stands in one of three places, and nowhere else:

Never inside a statement: not on an argument, an element of an array, a property value in an

object literal, or the operand of a property access. **Resolve the type in a line of its own,

then run the statement with the resolved value.** Resolving a type and using the value are two

intents; a cast inside the statement packs both into one line, where readability drops sharply.

to resolve a type and the intent to run .create() are crammed into one line, and readability

drops sharply. The type is resolved in the line above, and .create() runs with the value it

resolved.

right-hand side of cases.** Nothing inside them is cast again — not the argument of a

factory that builds a refused value, not a property of a case.

// NG: a cast on an argument, inside the statement
return JSON.stringify(
  value,
  /** @type {Parameters<typeof JSON.stringify>[1]} */ (this.replacer)
)

// OK: the type stated where the value is assigned
const replacer = /** @type {Parameters<typeof JSON.stringify>[1]} */ (this.replacer)

return JSON.stringify(value, replacer)

// NG: a refused value cast again inside an abnormal-value case
const cases = /** @type {Array<*>} */ ([
  {
    input: {
      request: ReportRequest.create(/** @type {*} */ ({
        // to: omitted
        subject: 'subject-0001',
      })),
    },
  },
])

// OK: the Array<*> on the right-hand side of cases is the only cast
const cases = /** @type {Array<*>} */ ([
  {
    input: {
      request: ReportRequest.create({
        // to: omitted
        subject: 'subject-0001',
      }),
    },
  },
])

An override is reduced to `/** @override */` only where nothing else is stated

A member that overrides another, and whose JSDoc adds nothing the base does not already

say, is written as the one-line block. **It may be reduced that far only when the block

carries no @param and no @returns** — those two are read by the type checker, and

TypeScript does not inherit them from the base member.

// OK: a getter with no parameters, whose return type the literal already gives
/** @override */
static get errorName () {
  return 'DocumentTooDeep'
}

// NG: @param dropped, so the destructured argument becomes an implicit any
/** @override */
static isAcceptable ({
  engine,
}) {
  // ...
}

binding, and dropping @returns from a member returning a visitor reported TS7006 on

that visitor's own parameters — the return type was what gave them their shape. **Lint

stayed at zero through both**, so nothing but the type checker reports this.

@returns would be redundant: the sentence is the thing being added.

among a few others (eslint-jsdoc-rules.md). What

decides whether to use it is this rule, not that exemption.

A `*`-only line is the block's one separator, and it goes in one place

A JSDoc block cannot hold a blank line. Every line inside it carries the leading *

(jsdoc/require-asterisk-prefix), so a line left truly blank is not a wider gap — it is a

lint error, reported alongside jsdoc/check-alignment. What reads as a blank line is a line

carrying nothing but *, and that is the only separator the block has.

It is spent in one place: between the block description and the first tag, where

jsdoc/tag-lines requires exactly one. Nowhere else — not between tags, not before the

closing, and not inside a type literal.

// OK: the one place it goes, between the description and the first tag
/**
 * Generate a random text of the given length.
 *
 * @param {{
 *   length: number
 * }} params - Parameters.
 */

// NG: a `*`-only line used to group the parameters
/**
 * @param {{
 *   ErrorCtor: typeof RenchanGraphqlError
 *
 *   maxDocumentDepth: number
 * }} params - Parameters of this constructor.
 */

// OK: the properties stand as one list
/**
 * @param {{
 *   ErrorCtor: typeof RenchanGraphqlError
 *   maxDocumentDepth: number
 * }} params - Parameters of this constructor.
 */

the argument list of the constructor or the factory carries it, and the JSDoc above

stays a flat list of the same properties.

lines around the tags, so it is what reports a missing separator before the first tag and

a stray one between two tags. Inside a type literal it sees one @param and nothing else,

so a *-only line between two properties is not a line it counts; and

jsdoc/require-asterisk-prefix passes it too, because the line has its asterisk. The

block comes out green, reading as though the grouping were sanctioned, which is why this

convention carries the rest.

Wrap a JSDoc sentence at a clause boundary

is read here for the @param and @returns descriptions, which follow the same break where

they run long.

Do not place a delimiter after each chopped-down property

// OK
/**
 * @param {{
 *   alpha: number
 *   beta: number
 * }} params - Parameters.
 */

Writing `@typedef`

refused by lint (jsdoc/multiline-blocks).

/hoc-comments.

// NG: written on a single line
/** @typedef {*} BooleanLike */

// NG: multiple @typedef in one block
/**
 * @typedef {*} BooleanLike
 * @typedef {{
 *   id: number
 *   name: string
 * }} UserEntity
 */

// OK: at least three lines, one per block, separated by a blank line
/**
 * @typedef {*} BooleanLike
 */

/**
 * @typedef {{
 *   id: number
 *   name: string
 * }} UserEntity
 */

Type-only imports

TypeScript import type statement — it is not available in a JavaScript-only project.

the style the repository has established, and do not mix both for the same type.** renchan

backends use the inline import('…') expression. If a repository has established neither,

prefer @import.

above.

// OK: the @import block tag
/**
 * @import {
 *   IncomingMessage,
 * } from 'node:http'
 */

// OK: the inline import('…') expression
/**
 * @returns {import('./CustomerOrdersBk.js').default} Backup model declaration.
 */

See import-tag.md and

import-expression.md for each style in full.

References

| Reference | Scope | Topic |

| --- | --- | --- |

| eslint-jsdoc-rules.md | Common | ESLint (jsdoc plugin) mapping — follow it and npm run lint passes / where this skill is stricter than lint / intentionally relaxed rules |

| import-tag.md | Common | Type-only import via the @import block tag — placement, named/default forms, source modules, consuming by bare name |

| import-expression.md | Common | Type-only import via the inline import('…') expression — forms, use sites, trade-offs vs @import |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.