Ticket #216: jslint.diff

File jslint.diff, 4.3 kB (added by dthomas, 7 months ago)

adding additonal messages for errors and warnings

  • JslLintTask.php

    old new  
    3333    protected $file;  // the source file (from xml attribute) 
    3434    protected $filesets = array(); // all fileset objects assigned to this task 
    3535 
     36    protected $showWarnings = true; 
    3637    protected $haltOnFailure = false; 
    3738    protected $hasErrors = false; 
    3839    private $badFiles = array(); 
    3940 
    4041    /** 
     42     * Sets the flag if warnings should be shown 
     43     * @param boolean $show 
     44     */ 
     45    public function setShowWarnings($show) { 
     46      $this->showWarnings = StringHelper::booleanValue($show); 
     47    } 
     48 
     49    /** 
    4150     * The haltonfailure property 
    4251     * @param boolean $aValue 
    4352     */ 
     
    99108      exec('jsl', $output); 
    100109      if (!preg_match('/JavaScript\sLint/', implode('', $output))) throw new BuildException('Javascript Lint not found'); 
    101110     
    102       $command = 'jsl -process '; 
     111      $command = 'jsl -output-format file:__FILE__;line:__LINE__;message:__ERROR__ -process '; 
    103112 
    104113      if(file_exists($file)) 
    105114      { 
    106115        if(is_readable($file)) 
    107116        { 
    108           $message = array(); 
    109           exec($command.'"'.$file.'"', $message); 
     117          $messages = array(); 
     118          exec($command.'"'.$file.'"', $messages); 
    110119 
    111           $summary = $message[sizeof($message) - 1]; 
     120          $summary = $messages[sizeof($messages) - 1]; 
    112121 
    113           preg_match('/^(.*)\serror/', $summary, $matches); 
    114           $errors = $matches[0]; 
     122          preg_match('/(\d+)\serror/', $summary, $matches); 
     123          $errorCount = $matches[1]; 
    115124           
    116           preg_match('/^(.*)\swarning/', $summary, $matches); 
    117           $warnings = $matches[0]; 
     125          preg_match('/(\d+)\swarning/', $summary, $matches); 
     126          $warningCount = $matches[1]; 
    118127 
    119           if(0 != $warnings) 
     128          $errors = array(); 
     129          $warnings = array(); 
     130          if ($errorCount > 0 || $warningCount > 0) { 
     131            $last = false; 
     132            foreach ($messages as $message) { 
     133              $matches = array(); 
     134              if (preg_match('/^(\.*)\^$/', $message)) { 
     135                $column = strlen($message); 
     136                if ($last == 'error') { 
     137                  $errors[count($errors) - 1]['column'] = $column; 
     138                } else if ($last == 'warning') { 
     139                  $warnings[count($warnings) - 1]['column'] = $column; 
     140                } 
     141                $last = false; 
     142              } 
     143              if (!preg_match('/^file:(.+);line:(\d+);message:(.+)$/', $message, $matches)) continue; 
     144              $msg = $matches[3]; 
     145              $data = array('filename' => $matches[1], 'line' => $matches[2], 'message' => $msg); 
     146              if (preg_match('/^.*error:.+$/i', $msg)) { 
     147                $errors[] = $data; 
     148                $last = 'error'; 
     149              } else if (preg_match('/^.*warning:.+$/i', $msg)) { 
     150                $warnings[] = $data; 
     151                $last = 'warning'; 
     152              } 
     153            } 
     154          } 
     155 
     156          if($this->showWarnings && $warningCount > 0) 
    120157          { 
    121             $this->log($file . ': ' . $warnings . ' warnings detected', Project::MSG_INFO); 
     158            $this->log($file . ': ' . $warningCount . ' warnings detected', Project::MSG_WARN); 
     159            foreach ($warnings as $warning) { 
     160              $this->log('- line ' . $warning['line'] . (isset($warning['column']) ? ' column ' . $warning['column'] : '') . ': ' . $warning['message'], Project::MSG_WARN); 
     161            } 
    122162          } 
    123163             
    124           if(0 != $errors
     164          if($errorCount > 0
    125165          { 
    126             $this->log($file . ': ' . $errors . ' errors detected', Project::MSG_ERR); 
     166            $this->log($file . ': ' . $errorCount . ' errors detected', Project::MSG_ERR); 
     167            foreach ($errors as $error) { 
     168              $this->log('- line ' . $error['line'] . (isset($error['column']) ? ' column ' . $error['column'] : '') . ': ' . $error['message'], Project::MSG_ERR); 
     169            } 
    127170            $this->badFiles[] = $file; 
    128171            $this->hasErrors = true; 
    129           } else
     172          } else if (!$this->showWarnings || $warningCount == 0)
    130173            $this->log($file . ': No syntax errors detected', Project::MSG_INFO); 
    131174          } 
    132175        } else {