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
|
|
|
*/
|
2014-05-05 23:22:26 +02:00
|
|
|
final class ArcanistJSHintLinter extends ArcanistExternalLinter {
|
2012-01-14 00:21:57 +01:00
|
|
|
|
2014-05-17 06:42:52 +02:00
|
|
|
private $jshintignore;
|
|
|
|
private $jshintrc;
|
|
|
|
|
2014-05-12 01:16:45 +02:00
|
|
|
public function getInfoName() {
|
|
|
|
return 'JSHint';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInfoURI() {
|
|
|
|
return 'http://www.jshint.com';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInfoDescription() {
|
2015-05-13 10:05:15 +02:00
|
|
|
return pht(
|
|
|
|
'Use `%s` to detect issues with JavaScript source files.',
|
|
|
|
'jshint');
|
2014-05-12 01:16:45 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2014-06-23 20:16:15 +02:00
|
|
|
} else if (preg_match('/^E043$/', $code)) {
|
|
|
|
// TODO: If JSHint encounters a large number of errors, it will quit
|
|
|
|
// prematurely and add an additional "Too Many Errors" error. Ideally, we
|
|
|
|
// should be able to pass some sort of `--force` option to `jshint`.
|
|
|
|
//
|
|
|
|
// See https://github.com/jshint/jshint/issues/180
|
|
|
|
return ArcanistLintSeverity::SEVERITY_DISABLED;
|
2014-05-05 23:22:26 +02:00
|
|
|
} else {
|
|
|
|
return ArcanistLintSeverity::SEVERITY_ERROR;
|
|
|
|
}
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
public function getDefaultBinary() {
|
2015-05-20 22:57:23 +02:00
|
|
|
return 'jshint';
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
Add a `getVersion` function to `ArcanistExternalLinter`.
Summary:
This method will, theoretically, allow `arc lint` to be configured to require some minimum version of an external linter (although this would probably require significantly more work).
Additionally, the existence of this method simplifies the `getCacheVersion` function which, previously, was implemented by the external linters individually. Instead, a general approach to determining the version for cacheing purposes can be used.
Fixes T4954.
Test Plan: I'm not sure how to test this.
Reviewers: chad, epriestley, #blessed_reviewers
Reviewed By: epriestley, #blessed_reviewers
Subscribers: epriestley, Korvin
Maniphest Tasks: T4954
Differential Revision: https://secure.phabricator.com/D8971
2014-05-06 00:10:20 +02:00
|
|
|
public function getVersion() {
|
2014-05-18 03:17:47 +02:00
|
|
|
// NOTE: `jshint --version` emits version information on stderr, not stdout.
|
|
|
|
list($stdout, $stderr) = execx(
|
|
|
|
'%C --version',
|
|
|
|
$this->getExecutableCommand());
|
2012-01-14 00:21:57 +01:00
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
$matches = array();
|
Add a `getVersion` function to `ArcanistExternalLinter`.
Summary:
This method will, theoretically, allow `arc lint` to be configured to require some minimum version of an external linter (although this would probably require significantly more work).
Additionally, the existence of this method simplifies the `getCacheVersion` function which, previously, was implemented by the external linters individually. Instead, a general approach to determining the version for cacheing purposes can be used.
Fixes T4954.
Test Plan: I'm not sure how to test this.
Reviewers: chad, epriestley, #blessed_reviewers
Reviewed By: epriestley, #blessed_reviewers
Subscribers: epriestley, Korvin
Maniphest Tasks: T4954
Differential Revision: https://secure.phabricator.com/D8971
2014-05-06 00:10:20 +02:00
|
|
|
$regex = '/^jshint v(?P<version>\d+\.\d+\.\d+)$/';
|
2014-05-18 03:17:47 +02:00
|
|
|
if (preg_match($regex, $stderr, $matches)) {
|
Add a `getVersion` function to `ArcanistExternalLinter`.
Summary:
This method will, theoretically, allow `arc lint` to be configured to require some minimum version of an external linter (although this would probably require significantly more work).
Additionally, the existence of this method simplifies the `getCacheVersion` function which, previously, was implemented by the external linters individually. Instead, a general approach to determining the version for cacheing purposes can be used.
Fixes T4954.
Test Plan: I'm not sure how to test this.
Reviewers: chad, epriestley, #blessed_reviewers
Reviewed By: epriestley, #blessed_reviewers
Subscribers: epriestley, Korvin
Maniphest Tasks: T4954
Differential Revision: https://secure.phabricator.com/D8971
2014-05-06 00:10:20 +02:00
|
|
|
return $matches['version'];
|
2014-05-05 23:22:26 +02:00
|
|
|
} else {
|
Add a `getVersion` function to `ArcanistExternalLinter`.
Summary:
This method will, theoretically, allow `arc lint` to be configured to require some minimum version of an external linter (although this would probably require significantly more work).
Additionally, the existence of this method simplifies the `getCacheVersion` function which, previously, was implemented by the external linters individually. Instead, a general approach to determining the version for cacheing purposes can be used.
Fixes T4954.
Test Plan: I'm not sure how to test this.
Reviewers: chad, epriestley, #blessed_reviewers
Reviewed By: epriestley, #blessed_reviewers
Subscribers: epriestley, Korvin
Maniphest Tasks: T4954
Differential Revision: https://secure.phabricator.com/D8971
2014-05-06 00:10:20 +02:00
|
|
|
return false;
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
public function getInstallInstructions() {
|
2015-05-13 10:05:15 +02:00
|
|
|
return pht('Install JSHint using `%s`.', 'npm install -g jshint');
|
2014-05-05 23:22:26 +02:00
|
|
|
}
|
2012-01-14 00:21:57 +01:00
|
|
|
|
2014-05-06 05:31:33 +02:00
|
|
|
protected function getMandatoryFlags() {
|
2014-05-17 06:42:52 +02:00
|
|
|
$options = array();
|
|
|
|
|
|
|
|
$options[] = '--reporter='.dirname(realpath(__FILE__)).'/reporter.js';
|
|
|
|
|
|
|
|
if ($this->jshintrc) {
|
|
|
|
$options[] = '--config='.$this->jshintrc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->jshintignore) {
|
|
|
|
$options[] = '--exclude-path='.$this->jshintignore;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLinterConfigurationOptions() {
|
|
|
|
$options = array(
|
|
|
|
'jshint.jshintignore' => array(
|
|
|
|
'type' => 'optional string',
|
|
|
|
'help' => pht('Pass in a custom jshintignore file path.'),
|
|
|
|
),
|
|
|
|
'jshint.jshintrc' => array(
|
|
|
|
'type' => 'optional string',
|
|
|
|
'help' => pht('Custom configuration file.'),
|
|
|
|
),
|
2014-05-05 23:22:26 +02:00
|
|
|
);
|
2014-05-17 06:42:52 +02:00
|
|
|
|
|
|
|
return $options + parent::getLinterConfigurationOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLinterConfigurationValue($key, $value) {
|
|
|
|
switch ($key) {
|
|
|
|
case 'jshint.jshintignore':
|
|
|
|
$this->jshintignore = $value;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'jshint.jshintrc':
|
|
|
|
$this->jshintrc = $value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::setLinterConfigurationValue($key, $value);
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-05 23:22:26 +02:00
|
|
|
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
|
2015-05-04 14:28:21 +02:00
|
|
|
$errors = null;
|
|
|
|
try {
|
2015-05-04 23:14:40 +02:00
|
|
|
$errors = phutil_json_decode($stdout);
|
2015-05-04 14:28:21 +02:00
|
|
|
} catch (PhutilJSONParserException $ex) {
|
2012-01-14 00:21:57 +01:00
|
|
|
// Something went wrong and we can't decode the output. Exit abnormally.
|
2015-05-04 14:28:21 +02:00
|
|
|
throw new PhutilProxyException(
|
|
|
|
pht('JSHint returned unparseable output.'),
|
|
|
|
$ex);
|
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);
|
2014-05-06 06:18:43 +02:00
|
|
|
$message->setLine(idx($err, 'line'));
|
|
|
|
$message->setChar(idx($err, 'col'));
|
|
|
|
$message->setCode(idx($err, 'code'));
|
|
|
|
$message->setName('JSHint'.idx($err, 'code'));
|
|
|
|
$message->setDescription(idx($err, 'reason'));
|
|
|
|
$message->setSeverity($this->getLintMessageSeverity(idx($err, 'code')));
|
2014-05-05 23:22:26 +02:00
|
|
|
|
|
|
|
$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
|
|
|
}
|
2014-06-20 10:26:44 +02:00
|
|
|
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|