2012-01-14 00:21:57 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-01-31 21:07:05 +01:00
|
|
|
final class ArcanistJSHintLinter extends ArcanistLinter {
|
2012-01-14 00:21:57 +01:00
|
|
|
|
|
|
|
const JSHINT_ERROR = 1;
|
|
|
|
|
|
|
|
public function getLinterName() {
|
|
|
|
return 'JSHint';
|
|
|
|
}
|
|
|
|
|
2013-10-03 04:02:40 +02:00
|
|
|
public function getLinterConfigurationName() {
|
|
|
|
return 'jshint';
|
|
|
|
}
|
|
|
|
|
2012-01-14 00:21:57 +01:00
|
|
|
public function getLintSeverityMap() {
|
|
|
|
return array(
|
|
|
|
self::JSHINT_ERROR => ArcanistLintSeverity::SEVERITY_ERROR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-04 15:29:47 +02:00
|
|
|
// placeholder if/until we get a map code -> name map
|
|
|
|
// jshint only offers code -> description right now (parsed as 'reason')
|
|
|
|
public function getLintMessageName($code) {
|
|
|
|
return "JSHint".$code;
|
|
|
|
}
|
|
|
|
|
2012-01-14 00:21:57 +01:00
|
|
|
public function getLintNameMap() {
|
|
|
|
return array(
|
|
|
|
self::JSHINT_ERROR => "JSHint Error"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getJSHintOptions() {
|
2013-10-23 00:34:06 +02:00
|
|
|
$config_manager = $this->getEngine()->getConfigurationManager();
|
2012-01-14 00:21:57 +01:00
|
|
|
$options = '--reporter '.dirname(realpath(__FILE__)).'/reporter.js';
|
2013-10-23 00:34:06 +02:00
|
|
|
$config = $config_manager->getConfigFromAnySource('lint.jshint.config');
|
2012-01-14 00:21:57 +01:00
|
|
|
|
2013-10-23 00:34:06 +02:00
|
|
|
$working_copy = $this->getEngine()->getWorkingCopy();
|
2012-01-14 00:21:57 +01:00
|
|
|
if ($config !== null) {
|
2012-11-22 02:16:52 +01:00
|
|
|
$config = Filesystem::resolvePath(
|
|
|
|
$config,
|
|
|
|
$working_copy->getProjectRoot());
|
2012-01-14 00:21:57 +01:00
|
|
|
|
|
|
|
if (!Filesystem::pathExists($config)) {
|
|
|
|
throw new ArcanistUsageException(
|
2012-11-22 02:16:52 +01:00
|
|
|
"Unable to find custom options file defined by ".
|
|
|
|
"'lint.jshint.config'. Make sure that the path is correct.");
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$options .= ' --config '.$config;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getJSHintPath() {
|
2013-10-23 00:34:06 +02:00
|
|
|
$config_manager = $this->getEngine()->getConfigurationManager();
|
|
|
|
$prefix = $config_manager->getConfigFromAnySource('lint.jshint.prefix');
|
|
|
|
$bin = $config_manager->getConfigFromAnySource('lint.jshint.bin');
|
2012-01-14 00:21:57 +01:00
|
|
|
|
|
|
|
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 ".
|
2012-11-22 02:16:52 +01:00
|
|
|
"correctly. If you'd rather use a copy of JSHint installed ".
|
|
|
|
"globally, you can just remove these keys from your .arcconfig");
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $bin;
|
|
|
|
}
|
|
|
|
|
2013-05-31 06:03:21 +02:00
|
|
|
if (!Filesystem::binaryExists($bin)) {
|
2012-01-14 00:21:57 +01:00
|
|
|
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) {
|
2013-02-15 00:49:22 +01:00
|
|
|
if (!$this->isCodeEnabled(self::JSHINT_ERROR)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-14 00:21:57 +01:00
|
|
|
$jshint_bin = $this->getJSHintPath();
|
|
|
|
$jshint_options = $this->getJSHintOptions();
|
|
|
|
$futures = array();
|
|
|
|
|
|
|
|
foreach ($paths as $path) {
|
|
|
|
$filepath = $this->getEngine()->getFilePathOnDisk($path);
|
2012-11-22 02:16:52 +01:00
|
|
|
$futures[$path] = new ExecFuture(
|
|
|
|
"%s %s %C",
|
|
|
|
$jshint_bin,
|
|
|
|
$filepath,
|
|
|
|
$jshint_options);
|
2012-01-14 00:21:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (Futures($futures)->limit(8) as $path => $future) {
|
|
|
|
$this->results[$path] = $future->resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function lintPath($path) {
|
2013-02-15 00:49:22 +01:00
|
|
|
if (!$this->isCodeEnabled(self::JSHINT_ERROR)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-20 15:12:24 +02:00
|
|
|
list($rc, $stdout, $stderr) = $this->results[$path];
|
2012-01-14 00:21:57 +01:00
|
|
|
|
|
|
|
if ($rc === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$errors = json_decode($stdout);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($errors as $err) {
|
|
|
|
$this->raiseLintAtLine(
|
|
|
|
$err->line,
|
|
|
|
$err->col,
|
2013-10-04 15:29:47 +02:00
|
|
|
$err->code,
|
2012-01-14 00:21:57 +01:00
|
|
|
$err->reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|