question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Handling of ambiguous comment blocks

See original GitHub issue

Consider the following TypeScript source file:

/** [1]
 * @file Copyright (c) Example Corporation
 */

/** [2]
 * The Widget class
 */
// [3] TODO: This class needs to be refactored
export class Widget {
  /** [4]
   * The width of the Widget class
   */
  // [5] private width: string;

  /* [6] Renders the class */
  public render(): void {
  }

  /**
   * [7] The height of the widget
   */
  public get height(): number {
  }

  // [8] This is a setter for the above getter
  /**
   * [9] Sets the height
   */
  public set height(value: number) {
  }
}

Which comments are associated with which API items?

We might expect a documentation tool to associate the comments as follows:

  • Widget: Uses [2] as its doc comment
  • Widget.render: No doc comment, but warn that [6] looks like it was intended to be JSDoc, and warn that [4] doesn’t seem to be attached to anything
  • Widget.height: Normally setters/getters are documented as one API item, so use [7] and report a warning for [9]

Does this make sense? Is there an unambiguous algorithm for selecting the right comment?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
octogonzcommented, Feb 12, 2019

We don’t use files-as-namespacs in any of the projects I work on, although I’ve had external requests for this feature that seem reasonable.

API Extractor does rely on file-level comments from the entry point of a package to document the package itself. However, I would be super paranoid about blindly publishing the “first” comment we encounter. What if it’s a copyright banner or other junk that wasn’t intended for customers to see? Or what if we counted wrong, because of the weird way that trivia gets parsed by the compiler (and often omitted from the .d.ts file)? For these reasons we insist on the file comment containing a TSDoc tag @packageDocumentation and do some checks to make sure it’s not missing, nor are there more than one of them.

It’s not great code, but you can see the implementation here: https://github.com/Microsoft/web-build-tools/blob/25d2894edaebb8fc30fe21f13cc691a5e8d78878/apps/api-extractor/src/aedoc/PackageDocComment.ts

I was thinking about revisiting this topic when we add support for namespaces introduced using import * as n1 from ___.

1reaction
MartynasZilinskascommented, Mar 26, 2018

I think there are two possible solutions.

  1. Strict. JSDoc comment block must be in immediate previous line.
class Foo {
    /**
     * Some documentation for `bar`.
     */
    public bar: string;

    /**
     * This JSDoc comment block is not part of `bar2`.
     * WARN developer for floating JSDoc comment block.
     */

    public bar2: string;

    /**
     * Same goes here like in `bar2` example.
     * This JSDoc comment block is not part of `bar3`.
     * WARN developer for floating JSDoc comment block.
     */
    // tslint:disable-next-line
    public bar3: string;    
}

  1. Sticky. Empty lines separate comment blocks.
class Foo {
    /**
     * Some documentation for `bar`.
     */
    public bar: string;

    /**
     * This JSDoc comment block is not part of `bar2`.
     * WARN developer for a floating JSDoc comment block.
     */

    public bar2: string;

    /**
     * JSDoc for `bar3`.
     */
    // tslint:disable-next-line
    public bar3: string;

    /**
     * JSDoc comment will be assigned for `bar5`. But it was intended for commented property `bar4`.
     */
    //public bar4: string;
    public bar5: string;

    /**
     * WARN developer for a floating JSDoc comment block.
     */
    /**
     * Some documentation for `bar6`.
     */
    public bar6: string;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC Errata Report » RFC Editor
RFC 5234, "Augmented BNF for Syntax Specifications: ABNF", January 2008. suggests that only ">" must not appear. Notes:
Read more >
Requests for comments (RFCs) - Sourcegraph handbook
An “RFC” literally means a “Request for Comments. ... be used if a decision explicitly requires one of the bullets in the section...
Read more >
RFC 8792 - Handling Long Lines in Content of Internet-Drafts ...
Handling Long Lines in Content of Internet-Drafts and RFCs (RFC 8792, June 2020)
Read more >
6 RFCs for understanding how the internet works
July 6, 2018 | 2 Comments | 3 min read ... By giving the requirements a well-defined taxonomy, RFC 2119 helps avoid ambiguity....
Read more >
Best practices for writing code comments - Stack Overflow Blog
I've seen students in upper-division computer science classes add a comment to each closed brace to indicate what block is ending:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found