Skip to content

Add Semgrep/GritQL lint rules for JS/TS complexity and documentation contracts #6

Description

@leynos

Problem

Biome can enforce some of our desired JS/TS maintainability constraints, but not
all of them.

Pure Biome can enforce:

  • Functions no longer than 70 lines via complexity.noExcessiveLinesPerFunction.
  • Functions with no more than 4 parameters via complexity.useMaxParams.

Biome cannot currently enforce these requirements exactly:

  • C90/McCabe cyclomatic complexity no greater than 8.
  • General function nesting depth no greater than 3.
  • Public JS/TS functions must have JSDoc covering usage, parameters, return
    value, and errors.
  • Private JS/TS functions must have one-line JSDoc.
  • JS/TS modules/files must have module-level JSDoc, preferably @file.

Biome has noExcessiveCognitiveComplexity, but cognitive complexity is not the
same metric as C90/McCabe cyclomatic complexity. Biome also has
useSingleJsDocAsterisk, but that only normalizes existing JSDoc formatting and
does not require documentation to exist or validate required tags/sections.

Proposed Work

Add either GritQL or Semgrep rules to enforce the remaining contracts:

  1. Report JS/TS functions whose McCabe/cyclomatic complexity exceeds 8.
  2. Report JS/TS functions whose statement/block nesting depth exceeds 3.
  3. Report exported/public functions without complete JSDoc covering:
    • usage
    • parameters
    • return value, where applicable
    • thrown/rejected errors, where applicable
  4. Report private/internal functions without a concise one-line JSDoc summary.
  5. Report JS/TS files without module-level JSDoc.

Biome Configuration To Enable Alongside This

{
  "linter": {
    "rules": {
      "complexity": {
        "noExcessiveLinesPerFunction": {
          "level": "error",
          "options": {
            "maxLines": 70,
            "skipBlankLines": false,
            "skipIifes": false
          }
        },
        "useMaxParams": {
          "level": "error",
          "options": {
            "max": 4
          }
        }
      }
    }
  }

Mapping:

  • Function length <= 70: complexity.noExcessiveLinesPerFunction.
  • Parameters <= 4: complexity.useMaxParams.
  • McCabe/C90 complexity <= 8: not exactly supported. Biome supports cognitive complexity via noExcessiveCognitiveComplexity, but that is not McCabe/cyclomatic complexity.
  • Nesting depth <= 3: not supported as a general function nesting rule. Biome only has targeted nesting rules such as nested test suites, nested promises, nested ternaries, etc.
  • Required public/private/module JSDoc: not supported by pure Biome. useSingleJsDocAsterisk only formats existing JSDoc blocks.

Additionally, enable cognitive complexity as an additional readability guard, but
do not treat it as satisfying the McCabe/C90 requirement:

{
  "complexity": {
    "noExcessiveCognitiveComplexity": {
      "level": "error",
      "options": {
        "maxAllowedComplexity": 8
      }
    }
  }

Acceptance Criteria

  • CI fails when a JS/TS function exceeds 70 lines.
  • CI fails when a JS/TS function has more than 4 parameters.
  • CI fails when a JS/TS function has McCabe/cyclomatic complexity greater than 8.
  • CI fails when a JS/TS function nests deeper than 3 levels.
  • CI fails when public functions lack complete JSDoc.
  • CI fails when private functions lack one-line JSDoc.
  • CI fails when JS/TS files lack module-level JSDoc.
  • Rules are documented in the developers’ guide with examples and suppression
    guidance.

Sources

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions