2012-01-14 00:21:57 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2014-05-05 23:22:26 +02:00
|
|
|
* Uses JSHint to detect errors and potential problems in JavaScript code.
|
2012-01-14 00:21:57 +01:00
|
|
|
*
|
|
|
|
* @group linter
|
|
|
|
*/
|
2014-05-05 23:22:26 +02:00
|
|
|
final class ArcanistJSHintLinter extends ArcanistExternalLinter {
|
2012-01-14 00:21:57 +01:00
|
|
|
|
|
|
|
public function getLinterName() {
|
|
|
|
return 'JSHint';
|
|
|
|
}
|
|
|
|
|
2013-10-03 04:02:40 +02:00
|
|
|
public function getLinterConfigurationName() {
|
|
|
|
return 'jshint';
|
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
protected function getDefaultMessageSeverity($code) {
|
|
|
|
if (preg_match('/^W/', $code)) {
|
|
|
|
return ArcanistLintSeverity::SEVERITY_WARNING;
|
|
|
|
} else {
|
|
|
|
return ArcanistLintSeverity::SEVERITY_ERROR;
|
|
|
|
}
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
public function getDefaultBinary() {
|
|
|
|
$config = $this->getEngine()->getConfigurationManager();
|
|
|
|
$prefix = $config->getConfigFromAnySource('lint.jshint.prefix');
|
|
|
|
$bin = $config->getConfigFromAnySource('lint.jshint.bin', 'jshint');
|
2013-10-04 15:29:47 +02:00
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
if ($prefix) {
|
|
|
|
return $prefix.'/'.$bin;
|
|
|
|
} else {
|
|
|
|
return $bin;
|
|
|
|
}
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
public function getCacheVersion() {
|
|
|
|
list($stdout) = execx('%C --version', $this->getExecutableCommand());
|
2012-01-14 00:21:57 +01:00
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
// Extract version number from standard output.
|
|
|
|
$matches = array();
|
|
|
|
if (preg_match('/^jshint v(\d+\.\d+\.\d+)$/', $stdout, $matches)) {
|
|
|
|
$version = $matches[1];
|
|
|
|
} else {
|
|
|
|
$version = md5($stdout);
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
return $version . '-' . md5(json_encode($this->getCommandFlags()));
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
public function getInstallInstructions() {
|
|
|
|
return pht('Install JSHint using `npm install -g jshint`.');
|
|
|
|
}
|
2012-01-14 00:21:57 +01:00
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
public function shouldExpectCommandErrors() {
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-14 00:21:57 +01:00
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
public function supportsReadDataFromStdin() {
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-14 00:21:57 +01:00
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
public function getReadDataFromStdinFilename() {
|
|
|
|
return '-';
|
|
|
|
}
|
2012-01-14 00:21:57 +01:00
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
public function getMandatoryFlags() {
|
|
|
|
return array(
|
|
|
|
'--reporter='.dirname(realpath(__FILE__)).'/reporter.js',
|
|
|
|
);
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
public function getDefaultFlags() {
|
|
|
|
$config_manager = $this->getEngine()->getConfigurationManager();
|
|
|
|
$options = $config_manager->getConfigFromAnySource(
|
|
|
|
'lint.jshint.options',
|
|
|
|
array());
|
2013-02-15 00:49:22 +01:00
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
$config = $config_manager->getConfigFromAnySource('lint.jshint.config');
|
|
|
|
if ($config) {
|
|
|
|
$options[] = '--config='.$config;
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
return $options;
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
|
2012-01-14 00:21:57 +01:00
|
|
|
$errors = json_decode($stdout);
|
2014-05-05 23:22:26 +02:00
|
|
|
|
2012-01-14 00:21:57 +01:00
|
|
|
if (!is_array($errors)) {
|
|
|
|
// Something went wrong and we can't decode the output. Exit abnormally.
|
|
|
|
throw new ArcanistUsageException(
|
2012-10-20 15:12:24 +02:00
|
|
|
"JSHint returned unparseable output.\n".
|
|
|
|
"stdout:\n\n{$stdout}".
|
|
|
|
"stderr:\n\n{$stderr}");
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
$messages = array();
|
2012-01-14 00:21:57 +01:00
|
|
|
foreach ($errors as $err) {
|
2014-05-05 23:22:26 +02:00
|
|
|
$message = new ArcanistLintMessage();
|
|
|
|
$message->setPath($path);
|
|
|
|
$message->setLine($err->line);
|
|
|
|
$message->setChar($err->col);
|
|
|
|
$message->setCode($err->code);
|
|
|
|
$message->setName('JSHint'.$err->code);
|
|
|
|
$message->setDescription($err->reason);
|
|
|
|
$message->setSeverity($this->getLintMessageSeverity($err->code));
|
|
|
|
$message->setOriginalText($err->evidence);
|
|
|
|
|
|
|
|
$messages[] = $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getLintCodeFromLinterConfigurationKey($code) {
|
|
|
|
if (!preg_match('/^(E|W)\d+$/', $code)) {
|
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'Unrecognized lint message code "%s". Expected a valid JSHint '.
|
|
|
|
'lint code like "%s" or "%s".',
|
|
|
|
$code,
|
|
|
|
'E033',
|
|
|
|
'W093'));
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
2014-05-05 23:22:26 +02:00
|
|
|
|
|
|
|
return $code;
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
}
|