1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2025-01-10 14:51:05 +01:00
phorge-arcanist/src/lint/linter/ArcanistJSHintLinter.php

140 lines
3.5 KiB
PHP
Raw Normal View History

<?php
/**
* Uses JSHint to detect errors and potential problems in JavaScript code.
*/
final class ArcanistJSHintLinter extends ArcanistExternalLinter {
public function getInfoName() {
return 'JSHint';
}
public function getInfoURI() {
return 'http://www.jshint.com';
}
public function getInfoDescription() {
return pht(
'Use `jshint` to detect issues with Javascript source files.');
}
public function getLinterName() {
return 'JSHint';
}
public function getLinterConfigurationName() {
return 'jshint';
}
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 getVersion() {
list($stdout) = execx('%C --version', $this->getExecutableCommand());
$matches = array();
$regex = '/^jshint v(?P<version>\d+\.\d+\.\d+)$/';
if (preg_match($regex, $stdout, $matches)) {
return $matches['version'];
} else {
return false;
}
}
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 '-';
}
protected function getMandatoryFlags() {
return array(
'--reporter='.dirname(realpath(__FILE__)).'/reporter.js',
);
}
protected function getDefaultFlags() {
$config_manager = $this->getEngine()->getConfigurationManager();
$options = $config_manager->getConfigFromAnySource(
'lint.jshint.options',
array());
$config = $config_manager->getConfigFromAnySource('lint.jshint.config');
if ($config) {
$options[] = '--config='.$config;
}
return $options;
}
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
$errors = json_decode($stdout, true);
if (!is_array($errors)) {
// Something went wrong and we can't decode the output. Exit abnormally.
throw new ArcanistUsageException(
Fix ArcanistSingleLintEngine for deleted paths Summary: Currently, ArcanisSingleLintEngine lints deleted paths and directories. These are sometimes appropriate, but SingleLintEngine is a less-sophisticated linter and should have more safe defaults. Also fix an error where JSHint reported useless messages on failure. Test Plan: Reproduced the problem: $ git show commit d71efe2b13770c8861bcd3415c15503fc377339f Author: epriestley <git@epriestley.com> Date: Fri Oct 19 12:22:50 2012 -0700 WIP diff --git a/test.js b/test.js deleted file mode 100644 index 8bd6648..0000000 --- a/test.js +++ /dev/null @@ -1 +0,0 @@ -asdf $ arc set-config --local lint.engine.single.linter ArcanistJSHintLinter Set key 'lint.engine.single.linter' = "ArcanistJSHintLinter" in local config (was null). $ arc lint --engine ArcanistSingleLintEngine --rev HEAD^ Usage Exception: JSHint returned output we can't parse. Check that your JSHint installation. Output: Applied the error message fix: $ arc lint --engine ArcanistSingleLintEngine --rev HEAD^ Usage Exception: JSHint returned unparseable output. stdout: stderr: node.js:181 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: ENOENT, No such file or directory '/INSECURE/repos/git-working-copy/test.js' at Object.statSync (fs.js:400:18) at _collect (/usr/lib/node_modules/jshint/lib/hint.js:77:12) at /usr/lib/node_modules/jshint/lib/hint.js:93:13 at Array.forEach (native) at Object.hint (/usr/lib/node_modules/jshint/lib/hint.js:92:17) at Object.interpret (/usr/lib/node_modules/jshint/lib/cli.js:137:21) at Object.<anonymous> (/usr/lib/node_modules/jshint/bin/hint:2:25) at Module._compile (module.js:420:26) at Object..js (module.js:426:10) at Module.load (module.js:336:31) Applied the remove paths fix: $ arc lint --engine ArcanistSingleLintEngine --rev HEAD^ Usage Exception: No paths are lintable. Reviewers: magazovski, btrahan, vrana Reviewed By: btrahan CC: aran, Korvin, vissi Differential Revision: https://secure.phabricator.com/D3735
2012-10-20 15:12:24 +02:00
"JSHint returned unparseable output.\n".
"stdout:\n\n{$stdout}".
"stderr:\n\n{$stderr}");
}
$messages = array();
foreach ($errors as $err) {
$message = new ArcanistLintMessage();
$message->setPath($path);
$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')));
$message->setOriginalText(idx($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;
}
}