4
* Uses JSHint to detect errors and potential problems in JavaScript code.
6
final class ArcanistJSHintLinter extends ArcanistExternalLinter {
11
public function getInfoName() {
15
public function getInfoURI() {
16
return 'http://www.jshint.com';
19
public function getInfoDescription() {
20
return pht('Use `jshint` to detect issues with JavaScript source files.');
23
public function getLinterName() {
27
public function getLinterConfigurationName() {
31
protected function getDefaultMessageSeverity($code) {
32
if (preg_match('/^W/', $code)) {
33
return ArcanistLintSeverity::SEVERITY_WARNING;
34
} else if (preg_match('/^E043$/', $code)) {
35
// TODO: If JSHint encounters a large number of errors, it will quit
36
// prematurely and add an additional "Too Many Errors" error. Ideally, we
37
// should be able to pass some sort of `--force` option to `jshint`.
39
// See https://github.com/jshint/jshint/issues/180
40
return ArcanistLintSeverity::SEVERITY_DISABLED;
42
return ArcanistLintSeverity::SEVERITY_ERROR;
46
public function getDefaultBinary() {
47
$prefix = $this->getDeprecatedConfiguration('lint.jshint.prefix');
48
$bin = $this->getDeprecatedConfiguration('lint.jshint.bin', 'jshint');
51
return $prefix.'/'.$bin;
57
public function getVersion() {
58
// NOTE: `jshint --version` emits version information on stderr, not stdout.
59
list($stdout, $stderr) = execx(
61
$this->getExecutableCommand());
64
$regex = '/^jshint v(?P<version>\d+\.\d+\.\d+)$/';
65
if (preg_match($regex, $stderr, $matches)) {
66
return $matches['version'];
72
public function getInstallInstructions() {
73
return pht('Install JSHint using `npm install -g jshint`.');
76
public function shouldExpectCommandErrors() {
80
public function supportsReadDataFromStdin() {
84
public function getReadDataFromStdinFilename() {
88
protected function getMandatoryFlags() {
91
$options[] = '--reporter='.dirname(realpath(__FILE__)).'/reporter.js';
93
if ($this->jshintrc) {
94
$options[] = '--config='.$this->jshintrc;
97
if ($this->jshintignore) {
98
$options[] = '--exclude-path='.$this->jshintignore;
104
public function getLinterConfigurationOptions() {
106
'jshint.jshintignore' => array(
107
'type' => 'optional string',
108
'help' => pht('Pass in a custom jshintignore file path.'),
110
'jshint.jshintrc' => array(
111
'type' => 'optional string',
112
'help' => pht('Custom configuration file.'),
116
return $options + parent::getLinterConfigurationOptions();
119
public function setLinterConfigurationValue($key, $value) {
121
case 'jshint.jshintignore':
122
$this->jshintignore = $value;
125
case 'jshint.jshintrc':
126
$this->jshintrc = $value;
130
return parent::setLinterConfigurationValue($key, $value);
133
protected function getDefaultFlags() {
134
$options = $this->getDeprecatedConfiguration(
135
'lint.jshint.options',
138
$config = $this->getDeprecatedConfiguration('lint.jshint.config');
140
$options[] = '--config='.$config;
146
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
147
$errors = json_decode($stdout, true);
149
if (!is_array($errors)) {
150
// Something went wrong and we can't decode the output. Exit abnormally.
151
throw new ArcanistUsageException(
152
"JSHint returned unparseable output.\n".
153
"stdout:\n\n{$stdout}".
154
"stderr:\n\n{$stderr}");
158
foreach ($errors as $err) {
159
$message = new ArcanistLintMessage();
160
$message->setPath($path);
161
$message->setLine(idx($err, 'line'));
162
$message->setChar(idx($err, 'col'));
163
$message->setCode(idx($err, 'code'));
164
$message->setName('JSHint'.idx($err, 'code'));
165
$message->setDescription(idx($err, 'reason'));
166
$message->setSeverity($this->getLintMessageSeverity(idx($err, 'code')));
168
$messages[] = $message;
174
protected function getLintCodeFromLinterConfigurationKey($code) {
175
if (!preg_match('/^(E|W)\d+$/', $code)) {
178
'Unrecognized lint message code "%s". Expected a valid JSHint '.
179
'lint code like "%s" or "%s".',