问题描述
我正在重构我的JavaScript库以使用单个命名空间。我们有〜200个模块,以前注册为jQuery插件或全局对象(坏)。在过去的分配中,为了让Intellisense工作,我为每个模块添加了模块引用(从中我想要Intellisense)作为一个
///< reference path =/>
到每个模块文件的顶部。
所有关于以前的配送的一切都是有缺陷的,脆弱的和不合适的。
我希望在新的免除。这个重构的主要目标之一是让新开发人员轻松开始在图书馆工作,而Intellisense是非常有益的。
目前,将所有* .js文件连接到单个nameSpace-vsdoc.js文件中。每个类的每个方法都使用vsdoc语法进行记录。每个模块都使用单个vsdoc引用。这样可以,但是很笨拙。 Intellisense比以前好300%,但仍然不够准确。它错过了很多我的xml文档,它不会很好地返回到返回甚至稍微复杂的对象的方法/类。
YUI Doc提供了一种从JsDoc语法,但这对Intellisense无帮助。一个深刻的灵魂谷歌搜索未能揭示任何第三方解决方案。
有没有任何工具以更智能的智能方式生成VsDoc?
更新:
经过广泛的思考,测试和增量重构,我有一个基本的命名空间,。
这使我能够自由地写入松散的模块 - 执行函数,它在命名空间中注册所需的方法。所有模块的开头都是:
///< reference path =〜/ js / NameSpace-vsdoc.js />
我用一个简单的Perl脚本生成了vsdoc,我附加到VS 2010构建事件:
use strict;
我的$ dir = $ ARGV [0];
我的$ destfile =$ dir\\\js\\NameSpace-vsdoc.js;
unlink($ destfile);
我的$ js =;
$ js。= extractFile($ dir\\\js\\_first-vsdoc.js);
$ js。= extractFile($ dir\\js\\NameSpace.js);
$ js。= extract($ dir\\\js\\);
#根据需要添加其他目录
$ js。= extractFile($ dir\\\js\\_last-vsdoc.js);
打开(VSDOC,> $ destfile)或死(无法打开vsdoc文件:$ destfile; $!);
打印VSDOC $ js;
close(VSDOC);
sub extract
{
my $ ret =;
我的$ path = $ _ [0];
opendir(JSDIR,$ path)or die(Can not open js directory:$ path; $!);
while((my $ filename = readdir(JSDIR)))
{
if($ filename =〜/.*\.js$/&&
$文件名!〜/ -vsdoc /&&
$ filename!〜/ _first /&
$ filename!〜/ _last /&&
$ filename!〜/ .min\.js /)
{
$ ret。= extractFile($ path \\ $ filename);
}
}
closedir(JSDIR);
return $ ret;
}
sub extractFile
{
my $ ret =;
我的$ filename = $ _ [0];
打开(JSFILE,$ filename)或死(无法打开js文件:$ filename; $!);
while((my $ line =< JSFILE>))
{
if($ line!〜m / -vsdoc\.js /)
{
$ ret。= $ line;
}
}
close(JSFILE);
return $ ret;
}
printf(完成生成NameSpace vsdoc.\\\
);
_first-vsdoc.js和_last-vsdoc.js文件将整个内容包装在自定义执行功能。然后,我将所得的vsdoc传递给Closure Compiler,YUI Compressor和Uglify,如需要/适当的。
此过程的工作原理比以前更好,但仍然没有缺陷
从头开始,在引用NameSpace-vsdoc.js的NewModule.js中,我获得了适当的智能感知:
Ns.register()// visible
Ns.controls.register()// visible
Ns.actions.register()// visible
然而,当我将NewModule.js定义为(和编译)时:
(function _alertClosure(){
Ns.register('alert',function alert(someText){
/// insert proper vsdoc style comment
});
}());
我没有获得适当的Intellisense:
Ns.alert()// no Intellisense help
这是令人困惑的,因为它在子命名空间上运行良好。我被迫这样做:
Ns.alert = Ns.alert || Ns.register('alert',function ....
编译,突然(显然)Intellisense按照预期的方式工作。
Intellisense使用jQuery的vsdoc(我在构建此过程中进行了深入研究)的链接方法,而jQuery并不诉诸 yx = yx || new x()
诡计使其发生。
所以这里是修改后的问题: / strong>从结构的角度来看,jQuery的vsdoc和我自己之间唯一可以看出的差异在于,jQuery在相同的封装中组装了整个命名空间,我的命名空间和我的每个模块都被包装相比之下,我的vsdoc看起来像一长串自行执行的功能。
与封闭/模块模式相关的风险太大了;而很难孤立地测试假设,当图书馆很小时,Intellisense的效果很好因此无法生成数千个Javascript Intellisense消息:C:\\js\NameSpace-vsdoc.js(40:16):对象需要错误。
想法?
我同意@Ilya Volodin。
ReSharper是视觉工作室的好工具。
我使用它只有一个月,我不能做没有。
这不回答你的问题,但对你来说非常有用。 p>
这是一个很好的总结,可以在官方网站上找到:
ReSharper是一个着名的productivité工具使Microsoft Visual Studio IDE更好,全球数以千计的.NET开发人员都想知道,如果没有ReSharper的代码检查,自动重构,快速运送和编码协助,他们将如何生活。
I'm in the process of refactoring my JavaScript library to utilize a single namespace. We have ~200 modules, which previously registered themselves as jQuery plugins or on the global object (bad). In the past dispensation, to get Intellisense working, I added module references for each module (from which I wanted Intellisense) as a
/// <reference path="" />
to the top of every module file.
Everything about the previous dispensation was flawed, fragile and inelegant.
I hope to fix that in the new dispensation. One of the primary goals of this refactoring is to make it easy for new developers to get started working in the library, and Intellisense is of great benefit.
Currently, I'm concatenating all of the *.js files into a single nameSpace-vsdoc.js file. Every method of every class is documented using vsdoc syntax. Every module uses that, single vsdoc reference. This works OK, but it's clumsy. Intellisense works 300% better than before, but it's still not accurate enough. It misses lots of my xml documentation and it doesn't recurse well into methods/classes which return even slightly complicated objects.
YUI Doc offers a way to build documentation from the JsDoc syntax, but that doesn't help with Intellisense. A deep soul-googling has failed to reveal any 3rd party solutions.
Are there any tools to generate VsDocs in a more Intellisense-intelligent way?
UPDATE:
After some extensive thought, testing and incremental refactorings, I have a basic namespace that looks like this jsFiddle.
This enables me to write loosely couple modules in self-executing functions, which register the desired methods on the namespace. All of the modules begin with:
/// <reference path="~/js/NameSpace-vsdoc.js" />
I generate this vsdoc with a simple Perl script, which I've attached to a VS 2010 build event:
use strict;
my $dir = $ARGV[0];
my $destfile = "$dir\\js\\NameSpace-vsdoc.js";
unlink($destfile);
my $js = "";
$js .= extractFile("$dir\\js\\_first-vsdoc.js");
$js .= extractFile("$dir\\js\\NameSpace.js");
$js .= extract("$dir\\js\\modules");
#Add additional directories as needed
$js .= extractFile("$dir\\js\\_last-vsdoc.js");
open(VSDOC, "> $destfile") or die("Cannot open vsdoc file: $destfile ; $!");
print VSDOC $js;
close(VSDOC);
sub extract
{
my $ret = "";
my $path = $_[0];
opendir(JSDIR, $path) or die("Cannot open js directory: $path ; $!");
while((my $filename = readdir(JSDIR)))
{
if($filename =~ /.*\.js$/ &&
$filename !~ /-vsdoc/ &&
$filename !~ /_first/ &&
$filename !~ /_last/ &&
$filename !~ /.min\.js/)
{
$ret .= extractFile("$path\\$filename");
}
}
closedir(JSDIR);
return $ret;
}
sub extractFile
{
my $ret = "";
my $filename = $_[0];
open(JSFILE, "$filename") or die("Cannot open js file: $filename ; $!");
while((my $line = <JSFILE>))
{
if($line !~ m/-vsdoc\.js/ )
{
$ret .= $line;
}
}
close(JSFILE);
return $ret;
}
printf("Finished generating NameSpace vsdoc.\n");
The _first-vsdoc.js and _last-vsdoc.js files wrap the entire contents in a self-executing function. I then pass the resulting vsdoc to Closure Compiler, YUI Compressor and Uglify as needed/appropriate.
This process works much better than before, but it is still not without its flaws.
Starting from scratch, in NewModule.js which references NameSpace-vsdoc.js, I get proper Intellisense:
Ns.register() //visible
Ns.controls.register() //visible
Ns.actions.register() //visible
However, when I define NewModule.js as (and compile):
(function _alertClosure() {
Ns.register('alert', function alert(someText) {
///insert proper vsdoc style comment
});
}());
I do not get proper Intellisense for:
Ns.alert() //no Intellisense help
which is perplexing, because it works so well on sub-namespaces. I'm forced to do this:
Ns.alert = Ns.alert || Ns.register('alert', function ....
Compile, and suddenly (obviously) Intellisense works as expected.
Intellisense works with chained methods using jQuery's vsdoc (which I have studied closely in constructing this process) and jQuery doesn't resort to the y.x = y.x || new x()
trickery to make it happen.
So here is the revised question: From a structural standpoint, the only discernible difference I can perceive between jQuery's vsdoc and my own is that jQuery assembles the entire namespace inside the same closure. My namespace and each of my modules are wrapped in individual closures. In comparison, my vsdoc looks like a long string of self-executing functions.
There is simply too much risk associated with forgoing the closure/module pattern; and it's difficult to test the hypothesis in isolation. Intellisense works well when the library is small and therefore unable to generate thousands of "Javascript Intellisense Message: C:\\js\NameSpace-vsdoc.js(40:16) : Object required" errors.
Ideas?
I agree with @Ilya Volodin.ReSharper is a great tool for visual studio.I use it for only a month and I can not do without.
This does not answer your question but it would be very useful to you.
Here is a good summary that can be found on the official website:
"ReSharper is a renowned productivité Tool That Makes Microsoft Visual Studio IDE is much better. Thousands of. NET developers worldwide wonder how they 've ever Lived without ReSharper's code inspections, automated refactorings, blazing fast shipping, and coding assistance."
这篇关于自动生成Visual Studio VsDoc for JavaScript库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!