1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-22 23:02:41 +01:00

Modernize ArcanistJSHintLinter.

Summary: Modernize `ArcanistJSHintLinter` by extending from `ArcanistExternalLinter` instead of `ArcanistLinter`.

Test Plan: Wrote and executed unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D8965
This commit is contained in:
Joshua Spence 2014-05-05 14:22:26 -07:00 committed by epriestley
parent 96d3416218
commit a7327ca0e9
8 changed files with 132 additions and 144 deletions

View file

@ -83,6 +83,7 @@ phutil_register_library_map(array(
'ArcanistInlinesWorkflow' => 'workflow/ArcanistInlinesWorkflow.php', 'ArcanistInlinesWorkflow' => 'workflow/ArcanistInlinesWorkflow.php',
'ArcanistInstallCertificateWorkflow' => 'workflow/ArcanistInstallCertificateWorkflow.php', 'ArcanistInstallCertificateWorkflow' => 'workflow/ArcanistInstallCertificateWorkflow.php',
'ArcanistJSHintLinter' => 'lint/linter/ArcanistJSHintLinter.php', 'ArcanistJSHintLinter' => 'lint/linter/ArcanistJSHintLinter.php',
'ArcanistJSHintLinterTestCase' => 'lint/linter/__tests__/ArcanistJSHintLinterTestCase.php',
'ArcanistLandWorkflow' => 'workflow/ArcanistLandWorkflow.php', 'ArcanistLandWorkflow' => 'workflow/ArcanistLandWorkflow.php',
'ArcanistLiberateWorkflow' => 'workflow/ArcanistLiberateWorkflow.php', 'ArcanistLiberateWorkflow' => 'workflow/ArcanistLiberateWorkflow.php',
'ArcanistLintConsoleRenderer' => 'lint/renderer/ArcanistLintConsoleRenderer.php', 'ArcanistLintConsoleRenderer' => 'lint/renderer/ArcanistLintConsoleRenderer.php',
@ -241,7 +242,8 @@ phutil_register_library_map(array(
'ArcanistHgServerChannel' => 'PhutilProtocolChannel', 'ArcanistHgServerChannel' => 'PhutilProtocolChannel',
'ArcanistInlinesWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistInlinesWorkflow' => 'ArcanistBaseWorkflow',
'ArcanistInstallCertificateWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistInstallCertificateWorkflow' => 'ArcanistBaseWorkflow',
'ArcanistJSHintLinter' => 'ArcanistLinter', 'ArcanistJSHintLinter' => 'ArcanistExternalLinter',
'ArcanistJSHintLinterTestCase' => 'ArcanistArcanistLinterTestCase',
'ArcanistLandWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistLandWorkflow' => 'ArcanistBaseWorkflow',
'ArcanistLiberateWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistLiberateWorkflow' => 'ArcanistBaseWorkflow',
'ArcanistLintConsoleRenderer' => 'ArcanistLintRenderer', 'ArcanistLintConsoleRenderer' => 'ArcanistLintRenderer',

View file

@ -1,44 +1,11 @@
<?php <?php
/** /**
* Uses "JSHint" to detect errors and potential problems in JavaScript code. * Uses JSHint to detect errors and potential problems in JavaScript code.
* To use this linter, you must install jshint through NPM (Node Package
* Manager). You can configure different JSHint options on a per-file basis.
*
* If you have NodeJS installed you should be able to install jshint with
* ##npm install jshint -g## (don't forget the -g flag or NPM will install
* the package locally). If your system is unusual, you can manually specify
* the location of jshint and its dependencies by configuring these keys in
* your .arcconfig:
*
* lint.jshint.prefix
* lint.jshint.bin
*
* If you want to configure custom options for your project, create a JSON
* file with these options and add the path to the file to your .arcconfig
* by configuring this key:
*
* lint.jshint.config
*
* Example JSON file (config.json):
*
* {
* "predef": [ // Custom globals
* "myGlobalVariable",
* "anotherGlobalVariable"
* ],
*
* "es5": true, // Allow ES5 syntax
* "strict": true // Require strict mode
* }
*
* For more options see http://www.jshint.com/options/.
* *
* @group linter * @group linter
*/ */
final class ArcanistJSHintLinter extends ArcanistLinter { final class ArcanistJSHintLinter extends ArcanistExternalLinter {
const JSHINT_ERROR = 1;
public function getLinterName() { public function getLinterName() {
return 'JSHint'; return 'JSHint';
@ -48,116 +15,79 @@ final class ArcanistJSHintLinter extends ArcanistLinter {
return 'jshint'; return 'jshint';
} }
public function getLintSeverityMap() { protected function getDefaultMessageSeverity($code) {
if (preg_match('/^W/', $code)) {
return ArcanistLintSeverity::SEVERITY_WARNING;
} else {
return ArcanistLintSeverity::SEVERITY_ERROR;
}
}
public function getDefaultBinary() {
$config = $this->getEngine()->getConfigurationManager();
$prefix = $config->getConfigFromAnySource('lint.jshint.prefix');
$bin = $config->getConfigFromAnySource('lint.jshint.bin', 'jshint');
if ($prefix) {
return $prefix.'/'.$bin;
} else {
return $bin;
}
}
public function getCacheVersion() {
list($stdout) = execx('%C --version', $this->getExecutableCommand());
// 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);
}
return $version . '-' . md5(json_encode($this->getCommandFlags()));
}
public function getInstallInstructions() {
return pht('Install JSHint using `npm install -g jshint`.');
}
public function shouldExpectCommandErrors() {
return true;
}
public function supportsReadDataFromStdin() {
return true;
}
public function getReadDataFromStdinFilename() {
return '-';
}
public function getMandatoryFlags() {
return array( return array(
self::JSHINT_ERROR => ArcanistLintSeverity::SEVERITY_ERROR '--reporter='.dirname(realpath(__FILE__)).'/reporter.js',
); );
} }
// placeholder if/until we get a map code -> name map public function getDefaultFlags() {
// jshint only offers code -> description right now (parsed as 'reason')
public function getLintMessageName($code) {
return "JSHint".$code;
}
public function getLintNameMap() {
return array(
self::JSHINT_ERROR => "JSHint Error"
);
}
public function getJSHintOptions() {
$config_manager = $this->getEngine()->getConfigurationManager(); $config_manager = $this->getEngine()->getConfigurationManager();
$options = '--reporter '.dirname(realpath(__FILE__)).'/reporter.js'; $options = $config_manager->getConfigFromAnySource(
'lint.jshint.options',
array());
$config = $config_manager->getConfigFromAnySource('lint.jshint.config'); $config = $config_manager->getConfigFromAnySource('lint.jshint.config');
if ($config) {
$working_copy = $this->getEngine()->getWorkingCopy(); $options[] = '--config='.$config;
if ($config !== null) {
$config = Filesystem::resolvePath(
$config,
$working_copy->getProjectRoot());
if (!Filesystem::pathExists($config)) {
throw new ArcanistUsageException(
"Unable to find custom options file defined by ".
"'lint.jshint.config'. Make sure that the path is correct.");
}
$options .= ' --config '.$config;
} }
return $options; return $options;
} }
private function getJSHintPath() { protected function parseLinterOutput($path, $err, $stdout, $stderr) {
$config_manager = $this->getEngine()->getConfigurationManager();
$prefix = $config_manager->getConfigFromAnySource('lint.jshint.prefix');
$bin = $config_manager->getConfigFromAnySource('lint.jshint.bin');
if ($bin === null) {
$bin = "jshint";
}
if ($prefix !== null) {
$bin = $prefix."/".$bin;
if (!Filesystem::pathExists($bin)) {
throw new ArcanistUsageException(
"Unable to find JSHint binary in a specified directory. Make sure ".
"that 'lint.jshint.prefix' and 'lint.jshint.bin' keys are set ".
"correctly. If you'd rather use a copy of JSHint installed ".
"globally, you can just remove these keys from your .arcconfig");
}
return $bin;
}
if (!Filesystem::binaryExists($bin)) {
throw new ArcanistUsageException(
"JSHint does not appear to be installed on this system. Install it ".
"(e.g., with 'npm install jshint -g') or configure ".
"'lint.jshint.prefix' in your .arcconfig to point to the directory ".
"where it resides.");
}
return $bin;
}
public function willLintPaths(array $paths) {
if (!$this->isCodeEnabled(self::JSHINT_ERROR)) {
return;
}
$jshint_bin = $this->getJSHintPath();
$jshint_options = $this->getJSHintOptions();
$futures = array();
foreach ($paths as $path) {
$filepath = $this->getEngine()->getFilePathOnDisk($path);
$futures[$path] = new ExecFuture(
"%s %s %C",
$jshint_bin,
$filepath,
$jshint_options);
}
foreach (Futures($futures)->limit(8) as $path => $future) {
$this->results[$path] = $future->resolve();
}
}
public function lintPath($path) {
if (!$this->isCodeEnabled(self::JSHINT_ERROR)) {
return;
}
list($rc, $stdout, $stderr) = $this->results[$path];
if ($rc === 0) {
return;
}
$errors = json_decode($stdout); $errors = json_decode($stdout);
if (!is_array($errors)) { if (!is_array($errors)) {
// Something went wrong and we can't decode the output. Exit abnormally. // Something went wrong and we can't decode the output. Exit abnormally.
throw new ArcanistUsageException( throw new ArcanistUsageException(
@ -166,12 +96,35 @@ final class ArcanistJSHintLinter extends ArcanistLinter {
"stderr:\n\n{$stderr}"); "stderr:\n\n{$stderr}");
} }
$messages = array();
foreach ($errors as $err) { foreach ($errors as $err) {
$this->raiseLintAtLine( $message = new ArcanistLintMessage();
$err->line, $message->setPath($path);
$err->col, $message->setLine($err->line);
$err->code, $message->setChar($err->col);
$err->reason); $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'));
}
return $code;
} }
} }

View file

@ -0,0 +1,12 @@
<?php
final class ArcanistJSHintLinterTestCase
extends ArcanistArcanistLinterTestCase {
public function testJSHintLinter() {
$this->executeTestsInDirectory(
dirname(__FILE__).'/jshint/',
new ArcanistJSHintLinter());
}
}

View file

@ -0,0 +1,6 @@
var args = {};
args['foo'] = 'bar';
args['bar'] = 'baz';
~~~~~~~~~~
warning:2:5
warning:3:5

View file

@ -0,0 +1,6 @@
var foo;
if (foo = 'bar') {
return true;
}
~~~~~~~~~~
warning:2:16

View file

@ -0,0 +1,3 @@
console.log('foobar')
~~~~~~~~~~
warning:1:22

View file

@ -0,0 +1,5 @@
function main() {
return 'Hello, World!';
};
~~~~~~~~~~
warning:3:2

View file

@ -10,6 +10,7 @@ module.exports = {
'col' : error.character, 'col' : error.character,
'reason' : error.reason, 'reason' : error.reason,
'code' : error.code, 'code' : error.code,
'evidence': error.evidence,
}); });
}); });