#!/usr/bin/perl -w
# @(#) $Id: jslint 1160 2006-06-04 10:49:10Z dom $

use strict;
use warnings;

use File::Basename;
use Getopt::Long;
use JavaScript::Lint;

my $me = basename $0;

sub slurp {
    my ( $file ) = @_;
    return do { local $/; <STDIN> } 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 = (
    browser    => 'the standard browser globals should be predefined',
    cap        => 'upper case HTML should be allowed',
    debug      => 'debugger statements should be allowed',
    evil       => 'eval should be allowed',
    jscript    => 'jscript deviations should be allowed',
    laxLineEnd => 'line breaks should not be checked',
    passfail   => 'the scan should stop on first error',
    plusplus   => 'post increment should not be allowed',
    undef      => 'undefined variables are errors',
);

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::Lint::VERSION\n";
    exit 0;
}

my %opts;
GetOptions( \%opts, 'help', 'version', map { "$_!" } sort keys %all_options );
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<jslint> 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<NB>: 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 --browser

The standard browser globals should be predefined.

=item --cap

Upper case HTML should be allowed.

=item --debug

Debugger statements should be allowed.

=item --evil

Eval should be allowed.

=item --jscript

Jscript deviations should be allowed.

=item --laxLineEnd

Line breaks should not be checked.

=item --passfail

The scan should stop on first error.

=item --plusplus

Post increment should not be allowed.

=item --undef

Undefined variables are errors

=back

=head1 SEE ALSO

L<JavaScript::Lint>

=head1 AUTHOR

Dominic Mitchell E<lt>cpan (at) happygiraffe.netE<gt>

=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
