Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sym3tri
Forked from erikfried/README.markdown
Created January 24, 2012 07:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sym3tri/1668719 to your computer and use it in GitHub Desktop.
Save sym3tri/1668719 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment