Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
R. Mark Volkmann committed Nov 28, 2010
0 parents commit 7614365
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
41 changes: 41 additions & 0 deletions README
@@ -0,0 +1,41 @@
This is a demonstration of how to use JSLint from an Apache Ant buildfile.
In its current form it requires the Rhino JavaScript engine, but it
should be possible to modify the jslint script to change that dependency.

The bin directory contains two bash scripts, jslint and jslintall,
which can be placed in any directory that is in PATH.
The Ant buildfile build.xml uses jslintall which uses jslint.
Modify the jslint script to tell it the location of
jslint.js and Rhino's js.jar which must be downloaded separately.
See http://jslint.com and http://www.mozilla.org/rhino/.
It should be possible to write equivalent Windows .bat scripts.

The docroot directory contains sample HTML, CSS and JavaScript source files
that are checked using JSLint. To demonstrate that JSLint is being run,
introduce errors in these files. For example,
* in demo.html, change "</body>" to "</bodyx>"
* in demo.css, change "purple" to "purplex"
* in demo.js, change "===" to "=="

To run JSLint from Ant on all the files in the docroot directory,
cd to the directory containing build.xml (which is the
parent directory of the docroot directory) and enter "ant".
This will create a hidden directory in the docroot directory
named ".bookkeeping". The results of running JSLint on each
file under docroot are written to this directory.
For example, the results for "docroot/demo.js"
are written to docroot/.bookkeeping/demo.js_lint.

If JSLint finds no issues, the file written under .bookkeeping
will contain "jslint: No problems found in {filename}"
and the file will be retained.
If JSLint finds any issues, the file will be
output to the shell/terminal window and deleted.

When the jslint script is run again, if a matching .bookkeeping file
is found that is newer than the source file being checked,
the JSLint tool is not actually run again since that version of
the source file has already been found to have no issues.

Suggestions for improvements to this scheme are welcomed!
Send them to r.mark.volkmann@gmail.com.
48 changes: 48 additions & 0 deletions bin/jslint
@@ -0,0 +1,48 @@
#!/bin/bash
# This runs JSLint on a single .js, .html or .css file
# specified as a command-line argument.
# It requires the Rhino JavaScript engine.
# Copy this file to a directory in PATH.
# Usage: jslint {file-path}

JS_DIR=/Users/Mark/Documents/Programming/Languages/JavaScript
JSLINT_DIR=$JS_DIR/JSLint
RHINO_HOME=$JS_DIR/Rhino/rhino1_7R2
RHINO_JAR=$RHINO_HOME/js.jar

if [ $# -ne 1 ]; then
echo "usage: jslint {file-path}"
exit 1
fi

# Make sure the .bookkeeping subdirectory exists.
mkdir .bookkeeping 2> /dev/null

file=$1
lintfile=.bookkeeping/${file}_lint
status=0

# If the source file has been modified
# since the last time jslint was run on it ...
if [ "$file" -nt "$lintfile" ]
then
echo "running JSLint on $file"
java -cp $RHINO_JAR \
org.mozilla.javascript.tools.shell.Main \
$JSLINT_DIR/jslint.js \
$file > $lintfile
status=$?

# If the lint output file isn't empty ...
#if [[ -s $lintfile ]] ; then
lines=$(wc -l $lintfile | awk '{print $1}')
if [[ $lines > 1 ]] ; then
cat $lintfile

# Delete the lint output file so JSLint will be run on the file again
# to verify that all the issues were fixed.
rm -f $lintfile
fi
fi

exit $status
19 changes: 19 additions & 0 deletions bin/jslintall
@@ -0,0 +1,19 @@
#!/bin/bash
# This runs JSLint on all .js, .html and .css files in the current directory.

for file in *.html *.css *.js
do
case $file in
*/jquery*.css)
;; # Don't run JSLint on jQuery .css files because they don't conform.
*/jquery*.js)
;; # Don't run JSLint on jQuery .js files because they don't conform.
*)
jslint $file
status=$?
if [ $status -ne 0 ]; then
echo "fix above issues in $file"
exit $status
fi
esac
done
8 changes: 8 additions & 0 deletions build.xml
@@ -0,0 +1,8 @@
<project name="JSLintFromAnt" default="jslint">

<target name="jslint"
description="runs JSLint on all .html, .css and .js files">
<exec executable="jslintall" dir="docroot" failonerror="true"/>
</target>

</project>
7 changes: 7 additions & 0 deletions docroot/demo.css
@@ -0,0 +1,7 @@
@charset "UTF-8";

.title {
color: purple;
font-size: 24pt;
font-weight: bold;
}
11 changes: 11 additions & 0 deletions docroot/demo.html
@@ -0,0 +1,11 @@
<!DOCTYPE html SYSTEM "xhtml1-strict.dtd">
<html>
<head>
<title>Demo</title>
<link rel="stylesheet" type="text/css" href="demo.css"/>
<script src="demo.js"></script>
</head>
<body>
<div id="title" class="title">Title</div>
</body>
</html>
10 changes: 10 additions & 0 deletions docroot/demo.js
@@ -0,0 +1,10 @@
/*jslint browser: true, indent: 2 */
/*global console: true */

var ns = {}; // namespace

ns.demoFunction = function (p) {
if (p === 19) {
console.log(p);
}
};

0 comments on commit 7614365

Please sign in to comment.