| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_Debug_JSLintSniff.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
11 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
12 |
* @version CVS: $Id: JSLintSniff.php 289845 2009-10-22 05:46:25Z squiz $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Squiz_Sniffs_Debug_JSLintSniff.
|
|
|
18 |
*
|
|
|
19 |
* Runs jslint.js on the file.
|
|
|
20 |
*
|
|
|
21 |
* @category PHP
|
|
|
22 |
* @package PHP_CodeSniffer
|
|
|
23 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
24 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
25 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
26 |
* @version Release: 1.2.1
|
|
|
27 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
28 |
*/
|
|
|
29 |
class Squiz_Sniffs_Debug_JSLintSniff implements PHP_CodeSniffer_Sniff
|
|
|
30 |
{
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* A list of tokenizers this sniff supports.
|
|
|
34 |
*
|
|
|
35 |
* @var array
|
|
|
36 |
*/
|
|
|
37 |
public $supportedTokenizers = array('JS');
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Returns the token types that this sniff is interested in.
|
|
|
42 |
*
|
|
|
43 |
* @return array(int)
|
|
|
44 |
*/
|
|
|
45 |
public function register()
|
|
|
46 |
{
|
|
|
47 |
return array(T_OPEN_TAG);
|
|
|
48 |
|
|
|
49 |
}//end register()
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Processes the tokens that this sniff is interested in.
|
|
|
54 |
*
|
|
|
55 |
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
|
|
|
56 |
* @param int $stackPtr The position in the stack where
|
|
|
57 |
* the token was found.
|
|
|
58 |
*
|
|
|
59 |
* @return void
|
|
|
60 |
* @throws PHP_CodeSniffer_Exception If jslint.js could not be run
|
|
|
61 |
*/
|
|
|
62 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
63 |
{
|
|
|
64 |
$fileName = $phpcsFile->getFilename();
|
|
|
65 |
|
|
|
66 |
$rhinoPath = PHP_CodeSniffer::getConfigData('rhino_path');
|
|
|
67 |
$jslintPath = PHP_CodeSniffer::getConfigData('jslint_path');
|
|
|
68 |
if ($rhinoPath === null || $jslintPath === null) {
|
|
|
69 |
return;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$cmd = "$rhinoPath \"$jslintPath\" \"$fileName\"";
|
|
|
73 |
$msg = exec($cmd, $output, $retval);
|
|
|
74 |
|
|
|
75 |
if (is_array($output) === true) {
|
|
|
76 |
$tokens = $phpcsFile->getTokens();
|
|
|
77 |
|
|
|
78 |
foreach ($output as $finding) {
|
|
|
79 |
$matches = array();
|
|
|
80 |
$numMatches = preg_match('/^Lint at line ([0-9]+).*:(.*)$/', $finding, $matches);
|
|
|
81 |
if ($numMatches === 0) {
|
|
|
82 |
continue;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
$line = (int) $matches[1];
|
|
|
86 |
$message = 'jslint says: '.trim($matches[2]);
|
|
|
87 |
|
|
|
88 |
// Find the token at the start of the line.
|
|
|
89 |
$lineToken = null;
|
|
|
90 |
foreach ($tokens as $ptr => $info) {
|
|
|
91 |
if ($info['line'] === $line) {
|
|
|
92 |
$lineToken = $ptr;
|
|
|
93 |
break;
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if ($lineToken !== null) {
|
|
|
98 |
$phpcsFile->addWarning($message, $lineToken);
|
|
|
99 |
}
|
|
|
100 |
}//end foreach
|
|
|
101 |
}//end if
|
|
|
102 |
|
|
|
103 |
}//end process()
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
}//end class
|
|
|
107 |
|
|
|
108 |
?>
|