TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Best libraries for converting JavaScript comments to both docs and tests?

3 点作者 tfb将近 10 年前
Basically, I&#x27;d like to automate docs and tests as much as possible from comments like this (or something similar):<p><pre><code> &#x2F;** * Adds `b` to `a`. * * @param a {Number} * @param b {Number} * @return {Number} * @api public *&#x2F; function add (a, b) { return a + b; } </code></pre> Obviously there are libraries that can do what I&#x27;m asking, but I&#x27;m having trouble finding the best one(s), so I figured I&#x27;d just ask here. :)

2 条评论

trcollinson将近 10 年前
How exactly would you like to be able to turn these into tests? I can see how this would turn into some sort of rudimentary documentation. However, the testing is a bit harder. Looking at your example, from the comments we know that your method accepts two numbers and returns a number. So what should the test framework do? @param a = 5, @param b = -23482739, @return = 9038745. Wait, no that&#x27;s not right, the test failed. I guess you could run some rudimentary tests like &quot;Does @param a only accept numbers?&quot; or &quot;Does @return only return a number?&quot; or even &quot;Is the add method in the @api public?&quot; but I am not sure I see the value of that.<p>Now what you might want is something like:<p><pre><code> &#x2F;** * Adds `b` to `a`. * * @param a {Number} * @param b {Number} * @return {Number} * @api public * @test https:&#x2F;&#x2F;github.com&#x2F;blah&#x2F;something&#x2F;blob&#x2F;master&#x2F;test&#x2F;add_test.js#L53 * *&#x2F; </code></pre> Then the documentation could link out to the specific test or tests for this method. Though if you are going to go this route you will probably want some sort of automated code analysis to find the test locations and link them in. Creating rudimentary documentation from these comments should be trivial.
评论 #10011221 未加载
tfb将近 10 年前
Forgot to add: Compatibility with ES6&#x2F;ES7 is preferred.<p>Edit: Found <a href="https:&#x2F;&#x2F;github.com&#x2F;tj&#x2F;dox" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tj&#x2F;dox</a>, but surely there&#x27;s a way to convert these comments (with maybe a little extra info) to tests?