Skip to content

fix: tolerate trailing inline comment in _tools/eslint/rules/jsdoc-doctest#11632

Draft
Planeshifter wants to merge 1 commit intodevelopfrom
philipp/jsdoc-doctest-allow-trailing-comment
Draft

fix: tolerate trailing inline comment in _tools/eslint/rules/jsdoc-doctest#11632
Planeshifter wants to merge 1 commit intodevelopfrom
philipp/jsdoc-doctest-allow-trailing-comment

Conversation

@Planeshifter
Copy link
Copy Markdown
Member

Description

This pull request:

  • _tools/eslint/rules/jsdoc-doctest — Relaxes the RE_ANNOTATION matcher in lib/main.js so that an optional trailing inline // ... comment between the statement-terminating ; and the newline preceding a // returns X (or // => X, // throws X) annotation no longer disables the doctest. Previously the regex required ;\n// literally, which meant any statement carrying a math-narration / explanatory trailing comment caused the rule to silently skip the assertion. Adds five fixture-driven test cases (two valid, three invalid) covering both var-annotated and console.log-annotated forms, the autofix output (which preserves the trailing comment and only rewrites the // returns annotation), and the edge case where the trailing inline comment itself contains a ;.

Related Issues

This pull request surfaced from review of #11584, where a stale // returns 0.5 annotation in chebyshev-seriesf's JSDoc example slipped past the doctest rule because of this exact gap. With this fix in place, the rule would have flagged that bug at lint time, and will now also catch the same pattern still present in the sibling chebyshev-series package.

No.

Questions

No.

Other

Verification.

  • node test/test.js (from the rule's package directory): all 5 tape tests pass, including the 5 newly added fixtures.
  • Direct microtest of the old vs. new regex against the exact chebyshev-seriesf example confirms the old regex returned 0 matches (silently skipping both annotations), while the new regex returns 2 matches and correctly surfaces the stale // returns 0.5.

Why the old regex missed it. The chebyshev example reads:

v = polyval( 0.0 ); // 1*T_0(0) + 0.5*T_1(0)
// returns 0.5

The old ;\n\/\/ literal in RE_ANNOTATION required the semicolon to be followed immediately by a newline. The space + // 1*T_0(0) + 0.5*T_1(0) between ; and \n broke the match, so the assertion was never executed and the wrong return value was never compared.

Risk surface.

  • The added group [ \t]*(?:\/\/[^\n]*)? is non-capturing; existing capture indices (arr[1] variable name, arr[2] annotation kind, arr[3] arrow-fn name, arr[4] expected value) are unchanged.
  • arr[0] is fed back to vm.runInContext at line 300 — appended trailing line comments are stripped at JS parse time, so execution is unaffected.
  • The autofix path (replace( tag.description, arr[ 0 ], str ) at line 318) still works because the enlarged arr[0] remains a verbatim substring of the JSDoc description; the trailing inline comment is preserved verbatim in the autofix output, as covered by the new invalid fixtures' output assertions.

Checklist

AI Assistance

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

This PR was authored by Claude (Opus 4.7) under my review. The bug was identified during follow-up review of PR #11584; Claude traced it to the exact regex literal, drafted the one-character-region regex change, added paired valid/invalid fixtures, and verified with both the existing tape suite and a direct microtest against the real-world chebyshev snippet that previously slipped through.


@stdlib-js/reviewers

…octest`

The `RE_ANNOTATION` matcher required `;\n//` literally, so any statement
carrying a trailing inline `// ...` comment between the `;` and the `\n`
caused the entire `// returns X` assertion to be silently skipped. Allow
an optional `[ \t]*(?://[^\n]*)?` between the semicolon and newline so
math-narration and other inline comments no longer disable the doctest.

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@stdlib-bot stdlib-bot added the Tools Issue or pull request related to project tooling. label Apr 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Tools Issue or pull request related to project tooling.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants