[lintian] 02/03: Detect .js file minified using line length
This is an automated email from the git hooks/post-receive script.
broucaries-guest pushed a commit to branch master
in repository lintian.
commit c847d4a0faa3cd0442fe83b8cceb5ba1f55f9d8e
Author: Bastien ROUCARIÈS <roucaries.bastien+debian@gmail.com>
Date: Tue Mar 11 21:29:11 2014 +0100
Detect .js file minified using line length
Detect minified .js usingline length as hint.
Signed-off-by: Bastien ROUCARIÈS <roucaries.bastien+debian@gmail.com>
---
checks/cruft.pm | 37 ++++++++++++++++++++++++++++++++-----
lib/Lintian/SlidingWindow.pm | 8 +++++---
2 files changed, 37 insertions(+), 8 deletions(-)
diff --git a/checks/cruft.pm b/checks/cruft.pm
index 56a1ebb..1620e97 100644
--- a/checks/cruft.pm
+++ b/checks/cruft.pm
@@ -614,7 +614,7 @@ sub find_cruft {
}
close($fd);
}
- license_check($info, $name, $info->unpacked($entry));
+ full_text_check($info, $name, $info->unpacked($entry));
}
return;
}
@@ -622,9 +622,11 @@ sub find_cruft {
# do basic license check against well known offender
# note that it does not replace licensecheck(1)
# and is only used for autoreject by ftp-master
-sub license_check {
+sub full_text_check {
my ($info, $name, $path) = @_;
+ my $isjsfile = ($name =~ m/\.js/) ? 1 : 0;
+
# license string in debian/changelog are probably just change
# Ignore these strings in d/README.{Debian,source}. If they
# appear there it is probably just "file XXX got removed
@@ -640,8 +642,10 @@ sub license_check {
return;
}
- my $sfd = Lintian::SlidingWindow->new('<:raw', $path, \&lc_block);
+ # some js file comments are really really long
+ my $sfd = Lintian::SlidingWindow->new('<:raw', $path, \&lc_block, $isjsfile ? 8092 : 4096);
my %licenseproblemhash = ();
+ my $cleanjsblock = '';
# we try to read this file in block and use a sliding window
# for efficiency. We store two blocks in @queue and the whole
@@ -651,11 +655,12 @@ sub license_check {
while (my $block = $sfd->readwindow()) {
my $cleanedblock;
my %matchedkeyword;
+ my $blocknumber = $sfd->blocknumber();
if(
_license_check(
$name, $NON_DISTRIBUTABLE_LICENSES,
- $block, $sfd->blocknumber(),
+ $block, $blocknumber,
\$cleanedblock, \%matchedkeyword,
\%licenseproblemhash
)
@@ -670,8 +675,30 @@ sub license_check {
}
_license_check($name, $NON_FREE_LICENSES,
- $block, $sfd->blocknumber(),\$cleanedblock, \%matchedkeyword,
+ $block, $blocknumber,\$cleanedblock, \%matchedkeyword,
\%licenseproblemhash);
+
+
+ # check javascript problem
+ if($isjsfile) {
+ if($blocknumber == 0) {
+ my $strip = $block;
+ # from perl faq strip comments
+ $strip =~ s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//([^\\]|[^\n][\n]?)*?\n|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $3 ? $3 : ""#gse;
+ # strip empty line
+ $strip =~ s/^\s*\n//mg;
+ # remove last \n
+ $strip =~ s/\n\Z//m;
+ # compute now means line length
+ my $total = length($strip);
+ if($total > 0) {
+ my $linelength = $total/($strip =~ tr/\n// + 1);
+ if($linelength > 255) {
+ tag 'source-contains-prebuilt-javascript-object', 'line length is about', $linelength, 'characters';
+ }
+ }
+ }
+ }
}
return;
}
diff --git a/lib/Lintian/SlidingWindow.pm b/lib/Lintian/SlidingWindow.pm
index 294e37b..c60d0a8 100644
--- a/lib/Lintian/SlidingWindow.pm
+++ b/lib/Lintian/SlidingWindow.pm
@@ -27,13 +27,15 @@ use Carp qw(croak);
use Lintian::Util qw(strip);
sub new {
- my ($class, $mode, $file, $blocksub) = @_;
+ my ($class, $mode, $file, $blocksub, $blocksize) = @_;
open(my $handle, $mode, $file);
+ $blocksize //= 4096;
+
my $self = {
'_handle' => $handle,
'_queue' => [q{}, q{}],
- '_blocksize' => 4096,
+ '_blocksize' => $blocksize,
'_blocksub' => $blocksub,
'_blocknumber' => -1,
};
@@ -127,7 +129,7 @@ file in memory.
=over 4
-=item new(mode,file,[blocksub])
+=item new(mode,file,[blocksub], [block size])
Create a new sliding window for file file using mode mode. Optionally run blocksub against
each block. Note that blocksub should apply transform byte by byte and does not depend of context.
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/lintian/lintian.git
Reply to: