Personal tools
You are here: Home ブログ matsuyama Emacs で JavaScript のシンタックスエラーを検出する
Document Actions

Emacs で JavaScript のシンタックスエラーを検出する

ネタとしては既出なので詳しくは説明しません(*)。必要なものは javascript-modeflymakespidermonkey です。

(*) http://code.google.com/p/flymake-js/ rhino を使ってシンタックスチェックしているようですがいろいろと無駄がある

以下の設定を .emacs に書いておけば OK です。

ちなみに flymake-js-detect-trailing-commat にしておけば(デフォルトで t )、オブジェクトの最後のコンマを検出してくれます。

.emacs:

(require 'flymake)

;; flymake for javascript
(defconst flymake-allowed-js-file-name-masks '(("\\.json$" flymake-js-init)
                                               ("\\.js$" flymake-js-init)))
(defcustom flymake-js-detect-trailing-comma t nil :type 'boolean)
(defvar flymake-js-err-line-patterns '(("^\\(.+\\)\:\\([0-9]+\\)\: \\(SyntaxError\:.+\\)\:$" 1 2 nil 3)))
(when flymake-js-detect-trailing-comma
  (setq flymake-js-err-line-patterns (append flymake-js-err-line-patterns
                                             '(("^\\(.+\\)\:\\([0-9]+\\)\: \\(strict warning: trailing comma.+\\)\:$" 1 2 nil 3)))))

(defun flymake-js-init ()
  (let* ((temp-file (flymake-init-create-temp-buffer-copy
                     'flymake-create-temp-inplace))
         (local-file (file-relative-name
                      temp-file
                      (file-name-directory buffer-file-name))))
    (list "js" (list "-s" local-file))))
(defun flymake-js-load ()
  (interactive)
  (defadvice flymake-post-syntax-check (before flymake-force-check-was-interrupted)
    (setq flymake-check-was-interrupted t))
  (ad-activate 'flymake-post-syntax-check)
  (setq flymake-allowed-file-name-masks (append flymake-allowed-file-name-masks flymake-allowed-js-file-name-masks))
  (setq flymake-err-line-patterns flymake-js-err-line-patterns)
  (flymake-mode t))

(add-hook 'javascript-mode-hook '(lambda () (flymake-js-load)))

ついでに以下のようなことも書いておくとハッピーになれるかも。

.emacs:

(global-set-key "\M-gn" '(lambda ()
                           (interactive)
                           (let (line (line-number-at-pos))
                             (flymake-goto-next-error)
                             (when (equal line (line-number-at-pos))
                               (next-error)))))
(global-set-key "\M-gp" '(lambda ()
                           (interactive)
                           (let (line (line-number-at-pos))
                             (flymake-goto-prev-error)
                             (when (equal line (line-number-at-pos))
                               (previous-error)))))
(global-set-key "\C-cd" 'flymake-display-err-menu-for-current-line)
flymake-js

Category(s)
emacs
The URL to Trackback this entry is:
http://dev.ariel-networks.com/Members/matsuyama/flymake-for-javascript/tbping

Re:Emacs で JavaScript のシンタックスエラーを検出する

Posted by watch movies online at 2009-10-02 17:42
Thanks for the tip!

Re:Emacs で JavaScript のシンタックスエラーを検出する

Posted by blog at 2010-10-28 22:44
Thanks! These are really nice tips
Add comment

You can add a comment by filling out the form below. Plain text formatting.

(Required)
(Required)
(Required)
(Required)
(Required)
This helps us prevent automated spamming.
Captcha Image


Copyright(C) 2001 - 2006 Ariel Networks, Inc. All rights reserved.