| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Usage: |
| 4 | # ./lint.sh [file.js] |
| 5 | # |
| 6 | # The zero-argument form lints everything. |
| 7 | |
| 8 | # See jshint/build/jshint-rhino.js for documentation on these parameters. |
| 9 | # devel defines logging globals (i.e. "console.log") |
| 10 | # browser defines standard web browser globals (i.e. "document") |
| 11 | # shadow disables warnings on multiple var definitions in one scope (i.e. two |
| 12 | # loops with "var i") |
| 13 | jsc_opts='maxerr:10000,devel:true,browser:true,shadow:true' |
| 14 | rhino_opts='maxerr=10000,devel=true,browser=true,shadow=true' |
| 15 | |
| 16 | if [ $# -gt 1 ]; then |
| 17 | echo "Usage: $0 [file.js]" |
| 18 | exit 1 |
| 19 | fi |
| 20 | |
| 21 | if [ $# -eq 0 ]; then |
| 22 | files=$(ls dygraph*.js plugins/*.js | grep -v combined | grep -v dev.js| grep -v externs) |
| 23 | else |
| 24 | files=$1 |
| 25 | fi |
| 26 | |
| 27 | if [ -e /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc ]; then |
| 28 | # use JSC (Safari/JavaScriptCore) to run JSHint -- much faster than Rhino. |
| 29 | echo 'Running JSHint w/ JavaScriptCore (jsc)...' |
| 30 | for file in $files; do |
| 31 | ./jshint/env/jsc.sh $file $jsc_opts |
| 32 | done |
| 33 | else |
| 34 | # fall back to Rhino. |
| 35 | echo 'Running JSHint w/ Rhino...' |
| 36 | |
| 37 | for FILE in $files; do |
| 38 | LINT_RESULT=$(java -jar ./jsdoc-toolkit/java/classes/js.jar ./jshint/build/jshint-rhino.js $rhino_opts $FILE) |
| 39 | ERRORS=$(echo ${LINT_RESULT} | egrep [^\s] -c) |
| 40 | if [[ ${ERRORS} -ne 0 ]]; then |
| 41 | echo "[jshint] Error(s) in ${FILE}:" |
| 42 | printf "%s\n" "${LINT_RESULT}" |
| 43 | else |
| 44 | echo "[jshint] ${FILE} passed!" |
| 45 | fi |
| 46 | done |
| 47 | fi |