PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file ESLintSniff.php

Documentation is available at ESLintSniff.php

  1. <?php
  2. /**
  3.  * Runs eslint on the file.
  4.  *
  5.  * @author    Ryan McCue <ryan+gh@hmn.md>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Debug;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Config;
  15.  
  16. class ESLintSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A list of tokenizers this sniff supports.
  21.      *
  22.      * @var array 
  23.      */
  24.     public $supportedTokenizers = array('JS');
  25.  
  26.  
  27.     /**
  28.      * ESLint configuration file path.
  29.      *
  30.      * @var string|nullPath to eslintrc. Null to autodetect.
  31.      */
  32.     public $configFile = null;
  33.  
  34.  
  35.     /**
  36.      * Returns the token types that this sniff is interested in.
  37.      *
  38.      * @return int[] 
  39.      */
  40.     public function register()
  41.     {
  42.         return array(T_OPEN_TAG);
  43.  
  44.     }//end register()
  45.  
  46.  
  47.     /**
  48.      * Processes the tokens that this sniff is interested in.
  49.      *
  50.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  51.      * @param int                         $stackPtr  The position in the stack where
  52.      *                                                the token was found.
  53.      *
  54.      * @return void 
  55.      * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If jshint.js could not be run
  56.      */
  57.     public function process(File $phpcsFile$stackPtr)
  58.     {
  59.         $eslintPath = Config::getExecutablePath('eslint');
  60.         if ($eslintPath === null{
  61.             return;
  62.         }
  63.  
  64.         $filename $phpcsFile->getFilename();
  65.  
  66.         $configFile $this->configFile;
  67.         if (empty($configFile=== true{
  68.             // Attempt to autodetect.
  69.             $candidates glob('.eslintrc{.js,.yaml,.yml,.json}'GLOB_BRACE);
  70.             if (empty($candidates=== false{
  71.                 $configFile $candidates[0];
  72.             }
  73.         }
  74.  
  75.         $eslintOptions = array('--format json');
  76.         if (empty($configFile=== false{
  77.             $eslintOptions['--config '.escapeshellarg($configFile);
  78.         }
  79.  
  80.         $cmd escapeshellcmd(escapeshellarg($eslintPath).' '.implode(' '$eslintOptions).' '.escapeshellarg($filename));
  81.  
  82.         // Execute!
  83.         exec($cmd$stdout$code);
  84.  
  85.         if ($code <= 0{
  86.             // No errors, continue.
  87.             return ($phpcsFile->numTokens + 1);
  88.         }
  89.  
  90.         $data json_decode(implode("\n"$stdout));
  91.         if (json_last_error(!== JSON_ERROR_NONE{
  92.             // Ignore any errors.
  93.             return ($phpcsFile->numTokens + 1);
  94.         }
  95.  
  96.         // Data is a list of files, but we only pass a single one.
  97.         $messages $data[0]->messages;
  98.         foreach ($messages as $error{
  99.             $message 'eslint says: '.$error->message;
  100.             if (empty($error->fatal=== false || $error->severity === 2{
  101.                 $phpcsFile->addErrorOnLine($message$error->line'ExternalTool');
  102.             else {
  103.                 $phpcsFile->addWarningOnLine($message$error->line'ExternalTool');
  104.             }
  105.         }
  106.  
  107.         // Ignore the rest of the file.
  108.         return ($phpcsFile->numTokens + 1);
  109.  
  110.     }//end process()
  111.  
  112.  
  113. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.