#!/usr/bin/perl -w # @(#) $Id: jslint 1323 2007-04-28 09:39:37Z dom $ use strict; use warnings; use File::Basename; use Getopt::Long; use JavaScript::JSLint qw( jslint jslint_options ); my $me = basename $0; sub slurp { my ( $file ) = @_; return do { local $/; } if $file eq '-'; open my $fh, '<', $file or die "$me: open($file): $!\n"; my $contents = do { local $/; <$fh> }; close $fh; return $contents; } sub lint_file { my ( $file, %opts ) = @_; my $src = slurp( $file ); my @errors = jslint( $src, %opts ); if ( @errors ) { foreach ( @errors ) { print join( ':', $file, $_->{ line }, $_->{ character }, $_->{ reason } ), "\n"; } } return scalar @errors; } my %all_options = jslint_options(); sub usage { my $msg = "usage: $me [--options] [file.js ...]\n"; $msg .= sprintf( " --%-08s \t%s\n", $_, $all_options{ $_ } ) foreach sort keys %all_options; die $msg; } sub version { print "$me $JavaScript::JSLint::VERSION\n"; exit 0; } my %opts; GetOptions( \%opts, 'help', 'version', map { "$_!" } sort keys %all_options ) or usage; usage if $opts{ help }; version if $opts{ version }; # Read STDIN unless args present. @ARGV = ( '-' ) unless @ARGV; my @error_files = grep { lint_file( $_, %opts ) } @ARGV; exit( @error_files > 0 ? 1 : 0 ); __END__ =head1 NAME jslint - Check a file for JavaScript errors =head1 SYNOPSIS jslint [--options] [file.js] =head1 DESCRIPTION B will check a file (or stdin) for JavaScript errors. All errors are printed on stdout and include: =over 4 =item the filename =item the line number =item the column number =item the error =back B: You can also pass in HTML files and any JavaScript found inside will be checked instead. =head2 OPTIONS All options are disabled by default. =over 4 =item --adsafe use of some browser features should be restricted. =item --bitwise bitwise operators should not be allowed. =item --browser the standard browser globals should be predefined. =item --cap upper case HTML should be allowed. =item --debug debugger statements should be allowed. =item --eqeqeq should be required. =item --evil eval should be allowed. =item --laxbreak line breaks should not be checked. =item --nomen names should be checked. =item --passfail the scan should stop on first error. =item --plusplus increment/decrement should not be allowed. =item --rhino the Rhino environment globals should be predefined. =item --undef undefined variables are errors. =item --white strict whitespace rules apply. =item --widget the Yahoo Widgets globals should be predefined. =back =head1 SEE ALSO L =head1 AUTHOR Dominic Mitchell Ecpan (at) happygiraffe.netE =head1 COPYRIGHT AND LICENSE Copyright (C) 2006 by Dominic Mitchell Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: =over 4 =item 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. =item 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. =back THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. JSLint is originally Copyright (C) 2002 Douglas Crockford. =cut