[英]How can I run jslint as a javascript compile tool in emacs for Windows?
I'm using GNU Emacs on Win32. 我在Win32上使用GNU Emacs。
I want to be able to run jslint as a compilation on .js files, and then step through the errors that jslint reports. 我希望能够将jslint作为.js文件的编译运行,然后逐步执行jslint报告的错误。
I have jslint, the WScript version. 我有jslint,WScript版本。
EDIT - There's now a simplified module that does this for you. 编辑 - 现在有一个简化的模块,为您完成此任务。
http://marmalade-repo.org/packages/fly-jshint-wsh http://marmalade-repo.org/packages/fly-jshint-wsh
This .el package: 这个.el包:
Easy peasy. 十分简单。 Still Windows only though.
尽管如此,仍然是Windows。
I kept all the old parts of this answer for reasons of historical interest, but you don't need to read any further. 出于历史原因,我保留了这个答案的所有旧部分,但您不需要再进一步阅读。
Note - Below I describe how to modify jslint.js for use within emacs. 注 - 下面我将介绍如何修改jslint.js以便在emacs中使用。
If you don't want to do it yourself, the already-modified code required is available . 如果您不想自己动手,可以使用已经修改过的代码 。
That link has an additional piece, flymake-for-jslint-for-wsh.el, that allows you to use jslint with flymake, on Windows. 该链接有一个额外的部分,flymake-for-jslint-for-wsh.el,允许你在Windows上使用jslint和flymake。
To use jslint within emacs, 要在emacs中使用jslint,
Download jslint.js, the WScript version . 下载jslint.js,WScript版本 。
Edit the jslint.js file. 编辑jslint.js文件。 Scroll to the bottom and find this:
滚动到底部找到:
(function(){if(!JSLINT(WScript.StdIn.ReadAll(),.....
Replace that (and everything that follows) with this: 用这个替换它(以及随后的所有内容):
(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"));
}
}}}());
This change does two things: 这种改变做了两件事:
The first change allows you to invoke jslint.js from within emacs with Mx compile
. 第一个更改允许您使用
Mx compile
从emacs中调用jslint.js。 The second allows you to interpet error messages with Mx next-error
. 第二个允许您使用
Mx next-error
插入错误消息。
Save that file to jslint-for-wsh.js 将该文件保存到jslint-for-wsh.js
Then, in your init.el, or emacs.el, add to your compilation-error-regexp-alist
, this regexp: 然后,在你的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)
In your javascript mode hook, set the compile command: 在你的javascript模式钩子中,设置编译命令:
(setq compile-command
(let ((file (file-name-nondirectory buffer-file-name)))
(concat "%windir%\\system32\\cscript.exe \\LOCATION\\OF\\jslint-for-wsh.js " file)))
That's it. 而已。
When you then open a .js file, and run Mx compile
, you will run jslint.js on the existing buffer. 然后,当您打开.js文件并运行
Mx compile
,您将在现有缓冲区上运行jslint.js。 You'll get a list of errors, and Mx next-error
works as you expect. 您将获得错误列表,并且
Mx next-error
按预期工作。
Yipee!! Yipee!
You can also run jslint as the flymake syntax checker tool, on Linux or Windows. 您还可以在Linux或Windows上运行jslint作为flymake语法检查工具。 See http://www.emacswiki.org/emacs/FlymakeJavaScript for details.
有关详细信息,请参阅http://www.emacswiki.org/emacs/FlymakeJavaScript 。
JSLint does not support WSH anymore. JSLint不再支持WSH。 I just added instructions to EmacsWiki for setting up JSLint with Node.js on Windows.
我刚刚向EmacsWiki添加了指令 ,用于在Windows上使用Node.js设置JSLint。
Possibly a better alternative to flymake is Flycheck, for which I also provided instructions on EmacsWiki concerning how to set it up with JSLint. Flycheck可能是flymake的一个更好的替代品,我还为EmacsWiki提供了有关如何使用JSLint进行设置的说明。
Install the jslint package for Node.js: 为Node.js安装jslint包:
C:\\>npm -g install jslint
On Windows there is an issue with calling jslint from EMACS 23. Thus, create a wrapper ~/.emacs.d/jslint.bat
(jslint needs to be part of the file name!): 在Windows上,从EMACS 23调用jslint存在问题 。因此,创建一个包装器
~/.emacs.d/jslint.bat
(jslint需要是文件名的一部分!):
@ECHO OFF node "%APPDATA%"\\npm\\node_modules\\jslint\\bin\\jslint.js >"%TEMP%"\\jslint.out 2>&1 %* TYPE "%TEMP%"\\jslint.out
Test JSLint from Emacs' built in shell EShell : 从Emacs的内置shell EShell测试JSLint :
$ ~/.emacs.d/jslint.bat No files specified. [...]
Install flymake-jslint and its dependency flymake-easy . 安装flymake-jslint及其依赖flymake-easy 。
Add to ~/.emacs
: 添加到
~/.emacs
:
(require 'flymake-jslint) (add-hook 'js-mode-hook 'flymake-jslint-load)
Customize flymake-jslint-command
: ~/.emacs.d/jslint.bat
自定义
flymake-jslint-command
: ~/.emacs.d/jslint.bat
Optionally customize other flymake-jslint options. (可选)自定义其他flymake-jslint选项。
Restart Emacs to make sure that everything loads fine. 重新启动Emacs以确保一切正常。
Test by opening a flawed JavaScript file. 通过打开有缺陷的JavaScript文件进行测试。
Customize flymake-log-level
and inspect the *Messages*
buffer. 自定义
flymake-log-level
并检查*Messages*
缓冲区。
Check flymake-jslint-args
. 检查
flymake-jslint-args
。
Make sure that the command configured in flymake-jslint-command
runs in EShell. 确保
flymake-jslint-command
配置的命令在flymake-jslint-command
运行。
Some notes on my setup using Perl, jslint, cygwin, EmacsW32. 使用Perl,jslint,cygwin,EmacsW32对我的设置进行了一些注释。 What a mess!
真是一团糟!
If you use JSHint installed with Node.js, you can, for example, run 如果您使用与Node.js一起安装的JSHint,则可以运行
Mx compile jshint --reporter unix --verbose *.js
. Mx compile jshint --reporter unix --verbose *.js
。
Or, add to your .emacs file: 或者,添加到.emacs文件:
(add-hook 'js-mode-hook
(lambda() (setq compile-command
(concat "jshint --reporter unix --verbose "
(file-name-nondirectory buffer-file-name)))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.