3
* Squiz_Sniffs_Debug_JSLintSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* Squiz_Sniffs_Debug_JSLintSniff.
18
* Runs jslint.js on the file.
21
* @package PHP_CodeSniffer
22
* @author Greg Sherwood <gsherwood@squiz.net>
23
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25
* @version Release: 1.5.5
26
* @link http://pear.php.net/package/PHP_CodeSniffer
28
class Squiz_Sniffs_Debug_JSLintSniff implements PHP_CodeSniffer_Sniff
32
* A list of tokenizers this sniff supports.
36
public $supportedTokenizers = array('JS');
40
* Returns the token types that this sniff is interested in.
44
public function register()
46
return array(T_OPEN_TAG);
52
* Processes the tokens that this sniff is interested in.
54
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
55
* @param int $stackPtr The position in the stack where
56
* the token was found.
59
* @throws PHP_CodeSniffer_Exception If jslint.js could not be run
61
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
63
$fileName = $phpcsFile->getFilename();
65
$rhinoPath = PHP_CodeSniffer::getConfigData('rhino_path');
66
$jslintPath = PHP_CodeSniffer::getConfigData('jslint_path');
67
if ($rhinoPath === null || $jslintPath === null) {
71
$cmd = "$rhinoPath \"$jslintPath\" \"$fileName\"";
72
$msg = exec($cmd, $output, $retval);
74
if (is_array($output) === true) {
75
$tokens = $phpcsFile->getTokens();
77
foreach ($output as $finding) {
79
$numMatches = preg_match('/Lint at line ([0-9]+).*:(.*)$/', $finding, $matches);
80
if ($numMatches === 0) {
84
$line = (int) $matches[1];
85
$message = 'jslint says: '.trim($matches[2]);
87
// Find the token at the start of the line.
89
foreach ($tokens as $ptr => $info) {
90
if ($info['line'] === $line) {
96
if ($lineToken !== null) {
97
$phpcsFile->addWarning($message, $lineToken, 'ExternalTool');