简体   繁体   English

如何在emacs for Windows中运行jslint作为javascript编译工具?

[英]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包:

  • downloads jshint or jslint for you (configurable) 为您下载jshint或jslint(可配置)
  • applies the necessary WSH-friendly modifications I describe below 应用我在下面描述的必要的WSH友好修改
  • saves the modified result to a temporary file 将修改后的结果保存到临时文件中
  • invokes that script via flymake in js-mode buffers 在js-mode缓冲区中通过flymake调用该脚本

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: 这种改变做了两件事:

  1. allows you to specify the file to run lint on, on the command line, rather than as stdin. 允许您在命令行上指定要运行lint的文件,而不是stdin。 Stdin still works if no file is specified at all. 如果根本没有指定文件,Stdin仍然有效。
  2. emits the error messages in a format that is more similar to most C/C++ compilers. 以与大多数C / C ++编译器更相似的格式发出错误消息。

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。

Update 更新

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进行设置的说明。

Steps 脚步

  1. Install Node.js . 安装Node.js.

  2. 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 
  3. Test JSLint from Emacs' built in shell EShell : 从Emacs的内置shell EShell测试JSLint

     $ ~/.emacs.d/jslint.bat No files specified. [...] 
  4. Install flymake-jslint and its dependency flymake-easy . 安装flymake-jslint及其依赖flymake-easy

  5. Add to ~/.emacs : 添加到~/.emacs

     (require 'flymake-jslint) (add-hook 'js-mode-hook 'flymake-jslint-load) 
  6. Customize flymake-jslint-command : ~/.emacs.d/jslint.bat 自定义flymake-jslint-command~/.emacs.d/jslint.bat

  7. Optionally customize other flymake-jslint options. (可选)自定义其他flymake-jslint选项。

  8. Restart Emacs to make sure that everything loads fine. 重新启动Emacs以确保一切正常。

  9. Test by opening a flawed JavaScript file. 通过打开有缺陷的JavaScript文件进行测试。

    在Windows XP / SP3上运行Emacs 23的flymake-jslint的屏幕截图

Troubleshooting 故障排除

  • 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM