Permalink
Browse files

JavaScriptLint feature

  • Loading branch information...
1 parent a182506 commit 005b526b25f8a9e0da53520dcce4d549d2a892d8 Neeraj Singh committed Dec 2, 2010
Showing with 314 additions and 1 deletion.
  1. +17 −1 README.md
  2. BIN jsl-0.3.0-mac/jsl
  3. +28 −0 jsl-0.3.0-mac/jsl-test.js
  4. +127 −0 jsl-0.3.0-mac/jsl.default.conf
  5. +126 −0 plugin/javaScriptLint.vim
  6. +16 −0 vimrc
View
@@ -11,7 +11,23 @@
ln -s /Users/nsingh/dev/vim/gvimrc ~/.gvimrc
ln -s /Users/nsingh/dev/vim ~/.vim
-## Installing exuberant ctags (optional) ##
+##Installing JavaScriptLint##
+
+* [JavaScriptLint](http://www.javascriptlint.com) is a tool that inspects validity of
+JavaScript code every time a JavaScript file is saved. It will flag you if you have a missing semicolon.
+
+* In order to get it working, you need to install spidermonkey. On mac if you have
+port installed then you can do this <tt>sudo port install spidermonkey</tt> . To check if port is properly
+installed just go to command prompt and type <tt>js</tt> .
+
+* [JavaScriptLint](http://www.javascriptlint.com/download.htm) has been included in this vim setting. Add the following line to your <tt>~/.bashrc</tt> so that at command prompt you can type <tt>jsl</tt> .
+
+ export PATH="/Users/nsingh/dev/vim/jsl-0.3.0-mac:$PATH"
+
+* Notice that the full path to where this vim is stored is mentioned in _vimrc_ . Search for _jslint_command_options_ and ensure that this option has right value. [Read this blog](http://neeraj.name/2009/09/08/integrating-javascriptlint-with-mvim-and-getting-rid-of-annoying-warnings.html) for more information.
+
+
+##Installing exuberant ctags (optional)##
cd ~
mkdir src
View
Binary file not shown.
View
@@ -0,0 +1,28 @@
+// JavaScript Lint (http://www.JavaScriptLint.com/)
+//
+// This function is supposed to find the difference between two numbers,
+// but it has some bugs! (And JavaScript Lint can help you find them!)
+
+/*jsl:option explicit*/
+function subtract(x,y) {
+ // optimize for equal values
+ if (x = y) {
+ return 0;
+ }
+
+ // don't allow negative return value
+ if (x < x) {
+ return null;
+ }
+
+ // optimize for some common subtractions
+ switch (y) {
+ case 0: z = x;
+ case 1: z = --x;
+ return z;
+ }
+
+ // finally, resort to actual subtraction!
+ return; x-y;
+}
+
@@ -0,0 +1,127 @@
+#
+# Configuration File for JavaScript Lint 0.3.0
+# Developed by Matthias Miller (http://www.JavaScriptLint.com)
+#
+# This configuration file can be used to lint a collection of scripts, or to enable
+# or disable warnings for scripts that are linted via the command line.
+#
+
+### Warnings
+# Enable or disable warnings based on requirements.
+# Use "+WarningName" to display or "-WarningName" to suppress.
+#
++no_return_value # function {0} does not always return a value
++duplicate_formal # duplicate formal argument {0}
++equal_as_assign # test for equality (==) mistyped as assignment (=)?{0}
++var_hides_arg # variable {0} hides argument
++redeclared_var # redeclaration of {0} {1}
++anon_no_return_value # anonymous function does not always return a value
++missing_semicolon # missing semicolon
++meaningless_block # meaningless block; curly braces have no impact
++comma_separated_stmts # multiple statements separated by commas (use semicolons?)
++unreachable_code # unreachable code
++missing_break # missing break statement
++missing_break_for_last_case # missing break statement for last case in switch
++comparison_type_conv # comparisons against null, 0, true, false, or an empty string allowing implicit type conversion (use === or !==)
++inc_dec_within_stmt # increment (++) and decrement (--) operators used as part of greater statement
++useless_void # use of the void type may be unnecessary (void is always undefined)
++multiple_plus_minus # unknown order of operations for successive plus (e.g. x+++y) or minus (e.g. x---y) signs
++use_of_label # use of label
+-block_without_braces # block statement without curly braces
++leading_decimal_point # leading decimal point may indicate a number or an object member
++trailing_decimal_point # trailing decimal point may indicate a number or an object member
++octal_number # leading zeros make an octal number
++nested_comment # nested comment
++misplaced_regex # regular expressions should be preceded by a left parenthesis, assignment, colon, or comma
+#neerajdotname changed from + to -
+-ambiguous_newline # unexpected end of line; it is ambiguous whether these lines are part of the same statement
++empty_statement # empty statement or extra semicolon
+-missing_option_explicit # the "option explicit" control comment is missing
++partial_option_explicit # the "option explicit" control comment, if used, must be in the first script tag
++dup_option_explicit # duplicate "option explicit" control comment
++useless_assign # useless assignment
++ambiguous_nested_stmt # block statements containing block statements should use curly braces to resolve ambiguity
++ambiguous_else_stmt # the else statement could be matched with one of multiple if statements (use curly braces to indicate intent)
++missing_default_case # missing default case in switch statement
++duplicate_case_in_switch # duplicate case in switch statements
++default_not_at_end # the default case is not at the end of the switch statement
++legacy_cc_not_understood # couldn't understand control comment using /*@keyword@*/ syntax
++jsl_cc_not_understood # couldn't understand control comment using /*jsl:keyword*/ syntax
++useless_comparison # useless comparison; comparing identical expressions
++with_statement # with statement hides undeclared variables; use temporary variable instead
++trailing_comma_in_array # extra comma is not recommended in array initializers
++assign_to_function_call # assignment to a function call
++parseint_missing_radix # parseInt missing radix parameter
+
+
+### Output format
+# Customize the format of the error message.
+# __FILE__ indicates current file path
+# __FILENAME__ indicates current file name
+# __LINE__ indicates current line
+# __ERROR__ indicates error message
+#
+# Visual Studio syntax (default):
++output-format __FILE__(__LINE__): __ERROR__
+# Alternative syntax:
+#+output-format __FILE__:__LINE__: __ERROR__
+
+
+### Context
+# Show the in-line position of the error.
+# Use "+context" to display or "-context" to suppress.
+#
++context
+
+
+### Semicolons
+# By default, assignments of an anonymous function to a variable or
+# property (such as a function prototype) must be followed by a semicolon.
+#
++lambda_assign_requires_semicolon
+
+
+### Control Comments
+# Both JavaScript Lint and the JScript interpreter confuse each other with the syntax for
+# the /*@keyword@*/ control comments and JScript conditional comments. (The latter is
+# enabled in JScript with @cc_on@). The /*jsl:keyword*/ syntax is preferred for this reason,
+# although legacy control comments are enabled by default for backward compatibility.
+#
++legacy_control_comments
+
+
+### JScript Function Extensions
+# JScript allows member functions to be defined like this:
+# function MyObj() { /*constructor*/ }
+# function MyObj.prototype.go() { /*member function*/ }
+#
+# It also allows events to be attached like this:
+# function window::onload() { /*init page*/ }
+#
+# This is a Microsoft-only JavaScript extension. Enable this setting to allow them.
+#
+-jscript_function_extensions
+
+
+### Defining identifiers
+# By default, "option explicit" is enabled on a per-file basis.
+# To enable this for all files, use "+always_use_option_explicit"
+-always_use_option_explicit
+
+# Define certain identifiers of which the lint is not aware.
+# (Use this in conjunction with the "undeclared identifier" warning.)
+#
+# Common uses for webpages might be:
+#+define window
+#+define document
+
+
+### Files
+# Specify which files to lint
+# Use "+recurse" to enable recursion (disabled by default).
+# To add a set of files, use "+process FileName", "+process Folder\Path\*.js",
+# or "+process Folder\Path\*.htm".
+#
+# neerajdotname changed from + to -
+#process jsl-test.js
+
View
@@ -0,0 +1,126 @@
+" File: javascriptLint.vim
+" Author: Joe Stelmach (joe@zenbe.com)
+" Version: 0.1
+" Description: javascriptLint.vim allows the JavaScript Lint (jsl) program
+" from http://www.javascriptlint.com/ to be tightly integrated
+" with vim. The contents of a javascript file will be passed
+" through the jsl program after the file's buffer is saved.
+" Any lint warnings will be placed in the quickfix window.
+" JavaScript Lint must be installed on your system for this
+" plugin to work properly. This page should get you started:
+" http://www.javascriptlint.com/docs/index.htm
+" Last Modified: May 5, 2009
+
+if !exists("jslint_command")
+ let jslint_command = 'jsl'
+endif
+
+if !exists("jslint_command_options")
+ let jslint_command_options = '-nofilelisting -nocontext -nosummary -nologo -process '
+endif
+
+if !exists("jslint_highlight_color")
+ let jslint_highlight_color = 'DarkMagenta'
+endif
+
+" set up auto commands
+autocmd BufWritePost,FileWritePost *.js call JavascriptLint()
+autocmd BufWinLeave * call s:MaybeClearCursorLineColor()
+
+" Runs the current file through javascript lint and
+" opens a quickfix window with any warnings
+function JavascriptLint()
+ " run javascript lint on the current file
+ let current_file = shellescape(expand('%:p'))
+ let cmd_output = system(g:jslint_command . ' ' . g:jslint_command_options . ' ' . current_file)
+
+ " if some warnings were found, we process them
+ if (strlen(cmd_output) > 0) && (g:jslint_status == 'enabled')
+
+ " ensure proper error format
+ let s:errorformat = "%f:%l:%v:%*\\d:%*\\d:%*\\s%m"
+ set errorformat=%f(%l):\ %m
+
+ " write quickfix errors to a temp file
+ let quickfix_tmpfile_name = tempname()
+ exe "redir! > " . quickfix_tmpfile_name
+ silent echon cmd_output
+ redir END
+
+ " read in the errors temp file
+ execute "silent! cfile " . quickfix_tmpfile_name
+
+ " change the cursor line to something hard to miss
+ call s:SetCursorLineColor()
+
+ " open the quicfix window
+ botright copen
+ let s:qfix_buffer = bufnr("$")
+
+ " delete the temp file
+ call delete(quickfix_tmpfile_name)
+
+ " if no javascript warnings are found, we revert the cursorline color
+ " and close the quick fix window
+ else
+ call s:ClearCursorLineColor()
+ if(exists("s:qfix_buffer"))
+ cclose
+ unlet s:qfix_buffer
+ endif
+ endif
+endfunction
+
+" sets the cursor line highlight color to the error highlight color
+function s:SetCursorLineColor()
+ " check for disabled cursor line
+ if(!exists("g:jslint_highlight_color") || strlen(g:jslint_highlight_color) == 0)
+ return
+ endif
+
+ call s:ClearCursorLineColor()
+ let s:highlight_on = 1
+
+ " find the current cursor line highlight info
+ redir => l:highlight_info
+ silent highlight CursorLine
+ redir END
+
+ " find the guibg property within the highlight info (if it exists)
+ let l:start_index = match(l:highlight_info, "guibg")
+ if(l:start_index > 0)
+ let s:previous_cursor_guibg = strpart(l:highlight_info, l:start_index)
+
+ elseif(exists("s:previous_cursor_guibg"))
+ unlet s:previous_cursor_guibg
+ endif
+
+ execute "highlight CursorLine guibg=" . g:jslint_highlight_color
+endfunction
+
+" Conditionally reverts the cursor line color based on the presence
+" of the quickfix window
+function s:MaybeClearCursorLineColor()
+ if(exists("s:qfix_buffer") && s:qfix_buffer == bufnr("%"))
+ call s:ClearCursorLineColor()
+ endif
+endfunction
+
+" Reverts the cursor line color
+function s:ClearCursorLineColor()
+ " only revert if our highlight is currently enabled
+ if(exists("s:highlight_on") && s:highlight_on)
+ let s:highlight_on = 0
+
+ " if a previous cursor guibg color was recorded, we use it
+ if(exists("s:previous_cursor_guibg"))
+ execute "highlight CursorLine " . s:previous_cursor_guibg
+ unlet s:previous_cursor_guibg
+
+ " otherwise, we clear the curor line highlight entirely
+ else
+ highlight clear CursorLine
+ endif
+ endif
+endfunction
+
View
16 vimrc
@@ -135,3 +135,19 @@ nmap <leader>d :NERDTreeToggle<CR>
" Use \v to edit .vimrc file
nmap <leader>v :tabedit $MYVIMRC<CR>
+
+
+" hit \j to enable/disable jslint check
+let g:jslint_status = 'enabled'
+function Jscheck()
+ if g:jslint_status == 'enabled'
+ let g:jslint_status = 'disabled'
+ echo 'jslint is disabled'
+ else
+ let g:jslint_status = 'enabled'
+ echo 'jslint is enabled'
+ endif
+endfunction
+nmap <leader>j :call Jscheck()<CR>
+
+let jslint_command_options = '-conf "/Users/nsingh/dev/vim/jsl-0.3.0-mac/jsl.default.conf" -nofilelisting -nocontext -nosummary -nologo -process'

0 comments on commit 005b526

Please sign in to comment.