[libcode-tidyall-perl] 201/374: add JSHint and JSBeautify

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:26:19 UTC 2013


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository libcode-tidyall-perl.

commit f9f2f9093dd793717646bfa6d3348fee34f0f993
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Sat Sep 8 07:43:12 2012 -0700

    add JSHint and JSBeautify
---
 lib/Code/TidyAll/Plugin/JSBeautify.pm   |   41 +++++++++++++++++++++++++++++++
 lib/Code/TidyAll/Plugin/JSHint.pm       |   40 ++++++++++++++++++++++++++++++
 lib/Code/TidyAll/t/Plugin.pm            |    6 ++++-
 lib/Code/TidyAll/t/Plugin/JSBeautify.pm |   19 ++++++++++++++
 lib/Code/TidyAll/t/Plugin/JSHint.pm     |   28 +++++++++++++++++++++
 lib/Code/TidyAll/t/Plugin/PerlTidy.pm   |    4 +--
 lib/Code/TidyAll/t/Plugin/foo.js        |    1 +
 xt/author/Plugin-JSBeautify.t           |    3 +++
 xt/author/Plugin-JSHint.t               |    3 +++
 9 files changed, 142 insertions(+), 3 deletions(-)

diff --git a/lib/Code/TidyAll/Plugin/JSBeautify.pm b/lib/Code/TidyAll/Plugin/JSBeautify.pm
new file mode 100644
index 0000000..01c82e9
--- /dev/null
+++ b/lib/Code/TidyAll/Plugin/JSBeautify.pm
@@ -0,0 +1,41 @@
+package Code::TidyAll::Plugin::JSBeautify;
+use IPC::System::Simple qw(run);
+use Moo;
+extends 'Code::TidyAll::Plugin';
+
+sub _build_cmd { 'js-beautify' }
+
+sub transform_file {
+    my ( $self, $file ) = @_;
+
+    run( sprintf( "%s --replace %s %s", $self->cmd, $self->argv, $file ) );
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Code::TidyAll::Plugin::JSBeautify - use jsbeautify with tidyall
+
+=head1 SYNOPSIS
+
+   # In tidyall.ini:
+
+   [JSBeautify]
+   select = static/**/*.js
+   argv = --indent-size 2 --brace-style expand
+
+=head1 INSTALLATION
+
+This plugin requires you to install the
+L<js-beautify|https://npmjs.org/package/js-beautify> npm package. Install
+L<npm|https://npmjs.org/>, then run
+
+    npm install js-beautfy -g
+
+Do not confuse this with the C<jsbeautify> package (without the dash).
+
diff --git a/lib/Code/TidyAll/Plugin/JSHint.pm b/lib/Code/TidyAll/Plugin/JSHint.pm
new file mode 100644
index 0000000..1555043
--- /dev/null
+++ b/lib/Code/TidyAll/Plugin/JSHint.pm
@@ -0,0 +1,40 @@
+package Code::TidyAll::Plugin::JSHint;
+use Capture::Tiny qw(capture_merged);
+use Moo;
+extends 'Code::TidyAll::Plugin';
+
+sub _build_cmd { 'jshint' }
+
+sub validate_file {
+    my ( $self, $file ) = @_;
+
+    my $cmd = sprintf( "%s %s %s", $self->cmd, $self->argv, $file );
+    my $output = capture_merged { system($cmd) };
+    die "$output\n" if $output =~ /\S/;
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Code::TidyAll::Plugin::JSHint - use jshint with tidyall
+
+=head1 SYNOPSIS
+
+   # In tidyall.ini:
+
+   [JSHint]
+   select = static/**/*.js
+   argv = --config $ROOT/jshint.json
+
+=head1 DESCRIPTION
+
+This plugin requires you to install L<jshint|http://www.jshint.com/platforms/>.
+The easiest way to do that at present time is to install
+L<npm|https://npmjs.org/>, then run
+
+    npm install jshint -g
diff --git a/lib/Code/TidyAll/t/Plugin.pm b/lib/Code/TidyAll/t/Plugin.pm
index 463ed3d..b6ff5b4 100644
--- a/lib/Code/TidyAll/t/Plugin.pm
+++ b/lib/Code/TidyAll/t/Plugin.pm
@@ -19,6 +19,8 @@ sub plugin_class {
     return ( split( '::', ref($self) ) )[-1];
 }
 
+sub test_filename { 'foo.txt' }
+
 sub tidyall {
     my ( $self, %p ) = @_;
 
@@ -30,16 +32,18 @@ sub tidyall {
 
     $source =~ s/\\n/\n/g;
     my $result;
-    my $output = capture_merged { $result = $ct->process_source( $source, 'foo.txt' ) };
+    my $output = capture_merged { $result = $ct->process_source( $source, $self->test_filename ) };
     $Test->diag($output) if $ENV{TEST_VERBOSE};
 
     if ( my $expect_tidy = $p{expect_tidy} ) {
         $expect_tidy =~ s/\\n/\n/g;
         is( $result->state,        'tidied',     'state=tidied' );
         is( $result->new_contents, $expect_tidy, 'new contents' );
+        is( $result->error,        undef,        'no error' );
     }
     elsif ( my $expect_ok = $p{expect_ok} ) {
         is( $result->state, 'checked', 'state=checked' );
+        is( $result->error, undef,     'no error' );
         if ( $result->new_contents ) {
             is( $result->new_contents, $source, 'same contents' );
         }
diff --git a/lib/Code/TidyAll/t/Plugin/JSBeautify.pm b/lib/Code/TidyAll/t/Plugin/JSBeautify.pm
new file mode 100644
index 0000000..b179999
--- /dev/null
+++ b/lib/Code/TidyAll/t/Plugin/JSBeautify.pm
@@ -0,0 +1,19 @@
+package Code::TidyAll::t::Plugin::JSBeautify;
+use Test::Class::Most parent => 'Code::TidyAll::t::Plugin';
+
+sub test_main : Tests {
+    my $self = shift;
+
+    my $source = 'sp.toggleResult=function(id){foo(id)}';
+    $self->tidyall(
+        source      => $source,
+        expect_tidy => 'sp.toggleResult = function(id) {\n    foo(id)\n}\n',
+    );
+    $self->tidyall(
+        source      => $source,
+        conf        => { argv => '--indent-size 2 --brace-style expand' },
+        expect_tidy => 'sp.toggleResult = function(id)\n{\n  foo(id)\n}\n',
+    );
+}
+
+1;
diff --git a/lib/Code/TidyAll/t/Plugin/JSHint.pm b/lib/Code/TidyAll/t/Plugin/JSHint.pm
new file mode 100644
index 0000000..4cb614f
--- /dev/null
+++ b/lib/Code/TidyAll/t/Plugin/JSHint.pm
@@ -0,0 +1,28 @@
+package Code::TidyAll::t::Plugin::JSHint;
+use Code::TidyAll::Util qw(write_file);
+use Test::Class::Most parent => 'Code::TidyAll::t::Plugin';
+
+sub test_filename { 'foo.js' }
+
+sub test_main : Tests {
+    my $self = shift;
+
+    my $rc_file = $self->{root_dir} . "/jshint.json";
+
+    $self->tidyall(
+        source    => 'var my_object = {};',
+        expect_ok => 1
+    );
+    $self->tidyall(
+        source       => 'var my_object = new Object();',
+        expect_error => qr/object literal notation/
+    );
+    write_file( $rc_file, '{"camelcase": true}' );
+    $self->tidyall(
+        source       => 'var my_object = {};',
+        conf         => { argv => "--config $rc_file" },
+        expect_error => qr/not in camel case/
+    );
+}
+
+1;
diff --git a/lib/Code/TidyAll/t/Plugin/PerlTidy.pm b/lib/Code/TidyAll/t/Plugin/PerlTidy.pm
index f8a656c..5a7b663 100644
--- a/lib/Code/TidyAll/t/Plugin/PerlTidy.pm
+++ b/lib/Code/TidyAll/t/Plugin/PerlTidy.pm
@@ -7,12 +7,12 @@ sub test_main : Tests {
     my $source = 'if (  $foo) {\nmy   $bar =  $baz;\n}\n';
     $self->tidyall(
         source      => $source,
-        expect_tidy => 'if ($foo) {\n    my $bar = $baz;\n}'
+        expect_tidy => 'if ($foo) {\n    my $bar = $baz;\n}\n'
     );
     $self->tidyall(
         conf        => { argv => '-bl' },
         source      => $source,
-        expect_tidy => 'if ($foo)\n{\n    my $bar = $baz;\n}'
+        expect_tidy => 'if ($foo)\n{\n    my $bar = $baz;\n}\n'
     );
     $self->tidyall(
         source    => 'if ($foo) {\n    my $bar = $baz;\n}\n',
diff --git a/lib/Code/TidyAll/t/Plugin/foo.js b/lib/Code/TidyAll/t/Plugin/foo.js
new file mode 100644
index 0000000..e4b0b3f
--- /dev/null
+++ b/lib/Code/TidyAll/t/Plugin/foo.js
@@ -0,0 +1 @@
+var sp = new Object();
diff --git a/xt/author/Plugin-JSBeautify.t b/xt/author/Plugin-JSBeautify.t
new file mode 100644
index 0000000..bc82f9f
--- /dev/null
+++ b/xt/author/Plugin-JSBeautify.t
@@ -0,0 +1,3 @@
+#!/usr/bin/perl
+use Code::TidyAll::t::Plugin::JSBeautify;
+Code::TidyAll::t::Plugin::JSBeautify->runtests;
diff --git a/xt/author/Plugin-JSHint.t b/xt/author/Plugin-JSHint.t
new file mode 100644
index 0000000..43d3d63
--- /dev/null
+++ b/xt/author/Plugin-JSHint.t
@@ -0,0 +1,3 @@
+#!/usr/bin/perl
+use Code::TidyAll::t::Plugin::JSHint;
+Code::TidyAll::t::Plugin::JSHint->runtests;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcode-tidyall-perl.git



More information about the Pkg-perl-cvs-commits mailing list