Skip to content

Instantly share code, notes, and snippets.

@erikfried
Created March 7, 2011 10:21
Show Gist options
  • Save erikfried/858343 to your computer and use it in GitHub Desktop.
Save erikfried/858343 to your computer and use it in GitHub Desktop.
shell script to invoke jslint via jsc on Mac OS X

I just realized that there is a rather well hidden javascript interpreter in the mac os x terminal out of the box. /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc See http://www.phpied.com/javascript-shell-scripting/

Then i figured it coud be quite easy to set up a command line util to run jslint from anywhere. Thought i´d share how.

Setup

  • Download and unzip this gist.
  • Download Douglas Crockfords jslint from here https://github.com/douglascrockford/JSLint/raw/master/fulljslint.js.
  • Edit the 'jslint' shell script and set the paths to where you saved the .js files.
  • Chmod the 'jslint' shell script (chmod +x )
  • (optional) add an alias to your .bashrc : alias jslint <path-to-files>/jslint
  • Try it!

Invocation

Examples of valid invocations jslint filename.js , jslint filename1.js filename2.js , jslint folder/*.js folder2/*.js ,

/*
This script is just a wrapper around Douglas Crockfords jslint, intended to be invoked by jsc.
The script is depending on jslint, which means jslint has to be loaded before this script.
*/
/*global JSLINT, print, quit, arguments */
(function (source) {
if (typeof JSLINT === 'undefined') {
print('ERROR: JSLINT does not appear to be properly loaded.');
}
if (typeof JSLINT === 'undefined' || !source) {
print('usage:\n $ jsc jslint.js jsc_jslint.js -- "`cat source.js`"');
quit();
}
var
// Options from www.jslint.com when using "The good parts"
goodPartsOptions = {devel: true,
browser: true,
white: true,
onevar: true,
undef: true,
newcap: true,
nomen: true,
regexp: true,
plusplus: true,
bitwise: true,
maxerr: 50,
indent: 4 },
result = JSLINT(source, goodPartsOptions),
errors = [],
error = {},
i;
if (result) {
print('\tNo errors :-)');
} else {
print('ERRORS:');
errors = JSLINT.data().errors;
for (i = 0; i < errors.length; i += 1) {
error = errors[i];
print(' ' + error.line + ':' + error.character + '\t' + error.reason);
print('\t' + error.evidence.trim());
print(' ---');
}
print('Total ' + errors.length + ' errors');
}
quit();
}(arguments[0]));
#!/bin/sh
#Dependencies
#Set paths appropriately to the 'core' jslint javascript file and to the jslinter wrapper script
JSLINT=<path-to-file>/fulljslint.js
JSC_WRAPPER=<path-to-file>/jsc_jslint_wrapper.js
if [ $# = "0" ]
then
echo 'Usage: \njslint file(s)'
echo 'Examples:'
echo 'jslint file.js file2.js'
echo 'jslint folder/*.js folder2/*.js'
else
#Loop over arguments, invoking jslint for each file separetely
for sourcefile in "$@"
do
echo "\n=== $sourcefile ==="
#Check that file exists
if [ ! -r $sourcefile -o ! -s $sourcefile ]
then
echo "FIle is not readable or empty"
continue
fi
#Invoke jslint via jsc
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc $JSLINT $JSC_WRAPPER -- "=`cat $sourcefile`"
echo "==="
done
fi
@sym3tri
Copy link

sym3tri commented Jan 23, 2012

The path and filename for Crockford's JS Lint has changed.

Also the shell script kept failing on my system (Lion) b/c of the equals sign on line 29. Seems to work after removing it.

@JeNeSuisPasDave
Copy link

Thanks to sym3tri for posting about that bug. I've posted a fork of the original gist that includes that fix and a bit of cleanup on the help output.

https://gist.github.com/2064959

Too bad we can't do pull requests on gists. :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment