如何在 jsdoc-toolkit 中包含已经记录的方法?

发布于 2024-09-29 11:58:08 字数 760 浏览 0 评论 0原文

我有一个使用闭包方法创建的 JavaScript 单例对象:

/**
 * This is the singleton.
 *
 * @namespace window.Something.Singleton
 */
window.Something.Singleton = (function() {
  /**
   * Foo method.
   *
   * @return {String} this method always returns with <code>bar</code>
   */
  function _foo() { return "bar"; }

  /**
   * @lends window.Something.Singleton#
   */
  return {
    /**
     * @borrows window.Something-_foo
     * @function
     */
    foo: _foo
  };
}());

但是 jsdoc-toolkit 没有生成我想要的从 _foo 方法到 foo 方法的继承文档。

如果我写 @see 而不是 @borrows 它可以工作(插入正确方法的链接),但我不想复制和粘贴 < code>foo 方法也有公共文档。

I have a JavaScript singleton object created with closure method:

/**
 * This is the singleton.
 *
 * @namespace window.Something.Singleton
 */
window.Something.Singleton = (function() {
  /**
   * Foo method.
   *
   * @return {String} this method always returns with <code>bar</code>
   */
  function _foo() { return "bar"; }

  /**
   * @lends window.Something.Singleton#
   */
  return {
    /**
     * @borrows window.Something-_foo
     * @function
     */
    foo: _foo
  };
}());

But the jsdoc-toolkit does not generate the inherited documentation from _foo method to the foo method what I would like to have.

If I write @see instead of @borrows it works (inserts a link to the correct method), but I don't want to copy&paste the documentation for the foo method to have a public documentation as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪 2024-10-06 11:58:08

我做了一些测试,有好消息和坏消息:)。好消息是,看起来您可以通过将 @borrows 标记放入 Singleton 文档注释中来使其工作:

/**
 * This is the singleton.
 *
 * @namespace Something.Singleton
 * @borrows Something.Singleton-_foo as foo
 */
Something.Singleton = (function() {
   // etc
})());

但这有几个后果,好的和坏的:

  • foo 函数被标记为static。这可能是一件好事,因为根据我理解你的代码,它似乎是准确的。
  • 使用当前显示的代码(即单例上不再有任何方法等),您可以完全放弃 @lends 标记,除非您为了清楚起见而想要包含它。如果所有方法都在顶级单例命名空间中列为 @borrows,那么您无需在返回的对象中进一步记录它们。再说一次,这可能是一件好事。
  • 坏消息是,除非借用的方法被显式标记为 @public,否则我无法让它工作 - 这使得它在文档中冗余地显示。我认为这是不可避免的 - 如果 jsdoc-toolkit 认为该方法是私有的,它不会创建文档,因此您无法引用它(我得到警告:无法借用未记录的 Something.Singleton-_foo。 )。如果该方法是公共的,它将显示在文档中。

所以这是有效的:

/**
 * This is the singleton.
 *
 * @namespace Something.Singleton
 * @borrows Something.Singleton-_foo as foo
 */
Something.Singleton = (function() {
  /**
   * @public
   * Foo method.
   *
   * @return {String} this method always returns with <code>bar</code>
   */
  function _foo() { return "bar"; }

  return {
    foo: _foo
  };
}());

...它将 _foo 的文档复制到 foo,但它也会在文档页面中显示 _foo

Method Summary
<inner>  _foo()
<static>  Something.Singleton.foo()

最好的解决方法可能就是完全忘记 @borrows 并在文档注释中明确将 _foo 命名为 Something.Singleton.foo ,将其标记为作为公共函数(如果您没有使用下划线命名内部函数,则可以省略 @public ,但这会告诉 jsdoc-toolkit 将其视为私有函数,除非另有说明):

/**
 * This is the singleton.
 *
 * @namespace Something.Singleton
 */
Something.Singleton = (function() {
  /**
   * @name Something.Singleton#foo
   * @public
   * @function
   * Foo method.
   *
   * @return {String} this method always returns with <code>bar</code>
   */
  function _foo() { return "bar"; }

  return {
    foo: _foo
  };
}());

这将产生您正在查找的代码文档,并将注释放在与其相关的实际代码旁边。它并不能完全满足让计算机完成所有工作的需要 - 你必须在评论中非常明确 - 但我认为它与你最初的内容一样清晰,甚至更清晰,而且我不这么认为不要认为这需要更多的维护(即使在最初的示例中,如果您重命名了 Something.Singleton,您也必须更改所有文档注释)。

I did some tests, and I have good news and bad news :). The good news is that it looks like you can get this to work by putting the @borrows tag in the Singleton doc comment:

/**
 * This is the singleton.
 *
 * @namespace Something.Singleton
 * @borrows Something.Singleton-_foo as foo
 */
Something.Singleton = (function() {
   // etc
})());

But this has several ramifications, good and bad:

  • The foo function is marked static. This is probably a good thing, as it seems to be accurate as I understand your code.
  • With the code as it is currently shown (i.e. no more methods on the singleton, etc), you can dispense with the @lends tag entirely, unless you want to include it for clarity. If all methods are listed as @borrows on the top singleton namespace, then you don't need to further document them in the returned object. Again, this is probably a good thing.
  • The bad news is that I couldn't get this to work unless the borrowed method was explicitly marked @public - which makes it show up, redundantly, in the documentation. I think this is unavoidable - if jsdoc-toolkit thinks the method is private, it won't create documentation, so you can't refer to it (I get WARNING: Can't borrow undocumented Something.Singleton-_foo.). If the method is public, it gets displayed in the documentation.

So this works:

/**
 * This is the singleton.
 *
 * @namespace Something.Singleton
 * @borrows Something.Singleton-_foo as foo
 */
Something.Singleton = (function() {
  /**
   * @public
   * Foo method.
   *
   * @return {String} this method always returns with <code>bar</code>
   */
  function _foo() { return "bar"; }

  return {
    foo: _foo
  };
}());

...in that it copies the documentation for _foo to foo, but it will display _foo in the documentation page as well:

Method Summary
<inner>  _foo()
<static>  Something.Singleton.foo()

Probably the best workaround is simply to forget @borrows entirely and explicitly name _foo as Something.Singleton.foo in the doc comment, marking it as a public function (you could dispense with @public if you didn't name your inner function with an underscore, but this tells jsdoc-toolkit to treat it as private unless told otherwise):

/**
 * This is the singleton.
 *
 * @namespace Something.Singleton
 */
Something.Singleton = (function() {
  /**
   * @name Something.Singleton#foo
   * @public
   * @function
   * Foo method.
   *
   * @return {String} this method always returns with <code>bar</code>
   */
  function _foo() { return "bar"; }

  return {
    foo: _foo
  };
}());

This will produce the code documentation you're looking for, and puts the comment next to the actual code it relates to. It doesn't fully satisfy one's need to have the computer do all the work - you have to be very explicit in the comment - but I think it's just as clear, if not more so, than what you had originally, and I don't think it's much more maintenance (even in your original example, you'd have to change all your doc comments if you renamed Something.Singleton).

凉风有信 2024-10-06 11:58:08

http://code.google.com/p/jsdoc-toolkit/wiki/TagBorrows

@borrows otherMemberName as thisMemberName
otherMemberName - 必需:其他成员的名称路径。
thisMemberName - 必需:该成员在此类中分配的新名称。

/** @constructor */
function Remote() {
}

/** Connect to a remote address. */
Remote.prototype.transfer = function(address, data) {
}

/**
 * @constructor
 * @borrows Remote#transfer as this.send
 */
function SpecialWriter() {
    this.send = Remote.prototype.transfer;
}

Remote#transfer 的任何文档也将显示为 SpecialWriter#send 的文档。

http://code.google.com/p/jsdoc-toolkit/wiki/TagBorrows

@borrows otherMemberName as thisMemberName
otherMemberName - Required: the namepath to the other member.
thisMemberName - Required: the new name that the member is being assigned to in this class.

/** @constructor */
function Remote() {
}

/** Connect to a remote address. */
Remote.prototype.transfer = function(address, data) {
}

/**
 * @constructor
 * @borrows Remote#transfer as this.send
 */
function SpecialWriter() {
    this.send = Remote.prototype.transfer;
}

Any documentation for Remote#transfer will also appear as documentation for SpecialWriter#send.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文