Created
December 25, 2010 07:49
-
-
Save bootleq/754754 to your computer and use it in GitHub Desktop.
Using JSLint command within Vim. See http://bootleq.blogspot.com/2010/12/jslint-vim.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
RHINO_JAR_FILE="$HOME/scripts/rhino.jar" | |
RHINO_JAR_CLASS="org.mozilla.javascript.tools.shell.Main" | |
JSLINT_JS_FILE="$HOME/scripts/jslint.js" | |
java -cp $RHINO_JAR_FILE $RHINO_JAR_CLASS $JSLINT_JS_FILE $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
command! -nargs=* JSLint call JSLint(<f-args>) | |
" @param boolean interact: 1 to prompt before lint. | |
" @param string options: custom options for running JSLint. in JSON string, ex: '{"onevar":false}' | |
fun! JSLint(...) | |
if ! executable('jslint') | |
echohl WarningMsg | echoerr "jslint command not found." | echohl None | |
return | |
endif | |
let input = expand('%') | |
let interact = a:0 > 0 ? a:1 : 0 | |
let options = a:0 > 1 ? a:2 : '' | |
if &modified | |
echomsg 'No write since last change, write before lint? (y/n) ' | |
let ans = getchar() | |
if nr2char(ans) == 'y' | w | |
elseif nr2char(ans) != 'n' | redraw! | echomsg 'JSLint aborted.' | return | |
endif | |
endif | |
if &filetype == 'javascript' | |
let cmd = 'jslint ' . input . ' ' . options | |
if interact | |
echomsg 'JSLint ' . input . '? (y/n) ' | redraw | |
let yes = getchar() | |
if nr2char(yes) == 'y' | |
return DoJSLint(cmd, input) | |
else | |
redraw! | |
echomsg 'JSLint aborted.' | |
return | |
endif | |
else | |
return DoJSLint(cmd, input) | |
endif | |
else | |
echohl WarningMsg | echomsg "Unsupported filetype." | echohl None | |
endif | |
endf | |
fun! DoJSLint(cmd, file) | |
echomsg "JSLint in progress..." | |
let ret = system(a:cmd) | |
if v:shell_error | |
cexpr ret | |
if len(getqflist()) > 0 | |
QFix!<CR> | |
endif | |
else | |
redraw | |
echomsg "No problems found in " . a:file | |
return | |
endif | |
endf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" http://vim.wikia.com/wiki/Toggle_to_open_or_close_the_quickfix_window | |
command! -bang -nargs=? QFix cal QFixToggle(<bang>0) | |
fu! QFixToggle(forced) | |
if exists("g:qfix_win") && a:forced == 0 | |
cclose | |
unlet g:qfix_win | |
else | |
copen 10 | |
let g:qfix_win = bufnr("$") | |
endif | |
endf | |
nnoremap <silent> <LocalLeader>q :QFix<CR> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modified from original rhino.js | |
// rhino.js | |
// 2009-09-11 | |
/* | |
Copyright (c) 2002 Douglas Crockford (www.JSLint.com) Rhino Edition | |
*/ | |
// This is the Rhino companion to fulljslint.js. | |
/*global JSLINT, environment */ | |
/*jslint rhino: true, strict: false, forin: true */ | |
var home = environment['user.home']; | |
load(home + '/scripts/fulljslint.js'); | |
load(home + '/scripts/json2.js'); | |
(function (a) { | |
var e, i, key, input, output, spaces = [], | |
options, default_options, extra_options; | |
if (!a[0]) { | |
print("Usage: jslint.js file.js"); | |
quit(1); | |
} | |
input = readFile(a[0]); | |
if (!input) { | |
print("jslint: Can't open file '" + a[0] + "'."); | |
quit(1); | |
} | |
// utility function for option extending. | |
function _append(obj, objEx) { | |
var key; | |
for (key in objEx) { | |
obj[key] = objEx[key]; | |
} | |
} | |
// Accept arguments[1] (JSON string) as JSLint options. | |
options = {}; | |
// default_options was modified from original rhino.js. | |
// added : browser:true, devel:true, indent:2, predef:[window] | |
// removed: rhino:true, nomen:true | |
default_options = { | |
bitwise: true, | |
eqeqeq: true, | |
immed: true, | |
newcap: true, | |
onevar: true, | |
plusplus: true, | |
regexp: true, | |
undef: true, | |
white: true, | |
browser: true, | |
devel: true, | |
indent: 2, | |
predef: [ | |
"window" | |
// 這裡可以放一些全域變數, | |
// 例如 MooTools 1.3 用到的: https://gist.github.com/675391 | |
] | |
}; | |
_append(options, default_options); | |
if (a[1]) { | |
extra_options = JSON.parse(a[1]); | |
_append(options, extra_options); | |
} | |
if (!JSLINT(input, options)) { | |
for (i = 0; i < JSLINT.errors.length; i += 1) { | |
e = JSLINT.errors[i]; | |
if (e) { | |
// Similar to format of closure-compiler, friendly for Vim's errorformat. | |
output = [ a[0], '|', e.line, '| ', e.reason ]; | |
if (e.evidence && e.evidence.replace(/\s/g, '') !== '') { | |
spaces.length = e.character; | |
output.push( | |
'\n', e.evidence, '\n', spaces.join(' ') + '^' | |
); | |
} | |
print(output.join('')); | |
} | |
} | |
quit(2); | |
} else { | |
quit(); | |
} | |
}(arguments)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment