Che*_*eso 11 javascript emacs jslint
我在Win32上使用GNU Emacs.
我希望能够将jslint作为.js文件的编译运行,然后逐步执行jslint报告的错误.
我有jslint,WScript版本.
编辑 - 现在有一个简化的模块可以为您完成此操作.
http://marmalade-repo.org/packages/fly-jshint-wsh
这个.el包:
十分简单.尽管如此,仍然是Windows.
出于历史原因,我保留了这个答案的所有旧部分,但您不需要再进一步阅读.
注 - 下面我将介绍如何修改jslint.js以便在emacs中使用.
如果您不想自己动手,可以使用已经修改过的代码.
该链接有一个额外的部分,flymake-for-jslint-for-wsh.el,允许你在Windows上使用jslint和flymake.
要在emacs中使用jslint,
编辑jslint.js文件.滚动到底部找到:
(function(){if(!JSLINT(WScript.StdIn.ReadAll(),.....
Run Code Online (Sandbox Code Playgroud)
用这个替换它(以及随后的所有内容):
(function(){
var filename = "stdin";
var content= "";
if (WScript.Arguments.length > 0){
filename = WScript.Arguments(0);
var fso = new ActiveXObject("Scripting.FileSystemObject");
//var file = fso.GetFile(filename);
var fs = fso.OpenTextFile(filename, 1);
content = fs.ReadAll();
fs.Close();
fso = null;
fs = null;
} else {
content = WScript.StdIn.ReadAll();
}
if(!JSLINT(content,{passfail:false})){
WScript.StdErr.WriteLine("JSLINT");
for (var i=0; i<JSLINT.errors.length; i++) {
// sample error msg:
// sprintf.js(53,42) JSLINT: Use the array literal notation [].
var e=JSLINT.errors[i];
if (e !== null){
var line = (typeof e.line == "undefined")?'0':e.line;
WScript.StdErr.WriteLine(filename + '(' +line+','+e.character+') JSLINT: '+e.reason);
WScript.StdErr.WriteLine(' ' + (e.evidence||'').replace(/^\s*(\S*(\s+\S+)*)\s*$/,"$1"));
}
}}}());
Run Code Online (Sandbox Code Playgroud)
这种改变做了两件事:
第一个更改允许您从emacs中调用jslint.js M-x compile.第二个允许您使用错误消息进行插入M-x next-error.
将该文件保存到jslint-for-wsh.js
然后,在你的init.el或emacs.el中,添加到你的compilation-error-regexp-alist,这个正则表达式:
;; JSLINT
("^[ \t]*\\([A-Za-z.0-9_: \\-]+\\)(\\([0-9]+\\)[,]\\( *[0-9]+\\))\\( Microsoft JScript runtime error\\| JSLINT\\): \\(.+\\)$" 1 2 3)
Run Code Online (Sandbox Code Playgroud)
在你的javascript模式钩子中,设置编译命令:
(setq compile-command
(let ((file (file-name-nondirectory buffer-file-name)))
(concat "%windir%\\system32\\cscript.exe \\LOCATION\\OF\\jslint-for-wsh.js " file)))
Run Code Online (Sandbox Code Playgroud)
而已.
当您打开.js文件并运行时M-x compile,您将在现有缓冲区上运行jslint.js.您将获得错误列表,并按M-x next-error预期工作.
alt text http://i40.tinypic.com/10nclxv.jpg
Yipee!
您还可以在Linux或Windows上运行jslint作为flymake语法检查工具.有关详细信息,请参阅http://www.emacswiki.org/emacs/FlymakeJavaScript.
替代文字http://i47.tinypic.com/2mk1eh.jpg