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

Add an ArcanistJscsLinter.

Summary: Add a linter which wraps around [[https://github.com/mdevils/node-jscs | JSCS]].

Test Plan: Currently, we can't test this linter with out existing infrastructure. Specifically, `jscs` will only lint `*.js` files. I have done a fair bit of manual testing though.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D9565
This commit is contained in:
Joshua Spence 2014-06-17 06:40:36 +10:00
parent 419a1247c2
commit b1ddd0f03b
4 changed files with 187 additions and 0 deletions

View file

@ -95,6 +95,8 @@ phutil_register_library_map(array(
'ArcanistJSHintLinterTestCase' => 'lint/linter/__tests__/ArcanistJSHintLinterTestCase.php',
'ArcanistJSONLintLinter' => 'lint/linter/ArcanistJSONLintLinter.php',
'ArcanistJSONLintLinterTestCase' => 'lint/linter/__tests__/ArcanistJSONLintLinterTestCase.php',
'ArcanistJscsLinter' => 'lint/linter/ArcanistJscsLinter.php',
'ArcanistJscsLinterTestCase' => 'lint/linter/__tests__/ArcanistJscsLinterTestCase.php',
'ArcanistLandWorkflow' => 'workflow/ArcanistLandWorkflow.php',
'ArcanistLesscLinter' => 'lint/linter/ArcanistLesscLinter.php',
'ArcanistLesscLinterTestCase' => 'lint/linter/__tests__/ArcanistLesscLinterTestCase.php',
@ -273,6 +275,8 @@ phutil_register_library_map(array(
'ArcanistJSHintLinterTestCase' => 'ArcanistArcanistLinterTestCase',
'ArcanistJSONLintLinter' => 'ArcanistExternalLinter',
'ArcanistJSONLintLinterTestCase' => 'ArcanistArcanistLinterTestCase',
'ArcanistJscsLinter' => 'ArcanistExternalLinter',
'ArcanistJscsLinterTestCase' => 'ArcanistArcanistLinterTestCase',
'ArcanistLandWorkflow' => 'ArcanistBaseWorkflow',
'ArcanistLesscLinter' => 'ArcanistExternalLinter',
'ArcanistLesscLinterTestCase' => 'ArcanistArcanistLinterTestCase',

View file

@ -0,0 +1,154 @@
<?php
final class ArcanistJscsLinter extends ArcanistExternalLinter {
private $config;
private $preset;
public function getInfoName() {
return 'JSCS';
}
public function getInfoURI() {
return 'https://github.com/mdevils/node-jscs';
}
public function getInfoDescription() {
return pht('Use `jscs` to detect issues with Javascript source files.');
}
public function getLinterName() {
return 'JSCS';
}
public function getLinterConfigurationName() {
return 'jscs';
}
public function getDefaultBinary() {
return 'jscs';
}
public function getVersion() {
list($stdout) = execx('%C --version', $this->getExecutableCommand());
$matches = array();
$regex = '/^(?P<version>\d+\.\d+\.\d+)$/';
if (preg_match($regex, $stdout, $matches)) {
return $matches['version'];
} else {
return false;
}
}
public function getInstallInstructions() {
return pht('Install JSCS using `npm install -g jscs`.');
}
public function shouldExpectCommandErrors() {
return true;
}
protected function getMandatoryFlags() {
$options = array();
$options[] = '--reporter=checkstyle';
$options[] = '--no-colors';
if ($this->config) {
$options[] = '--config='.$this->config;
}
if ($this->preset) {
$options[] = '--preset='.$this->preset;
}
return $options;
}
public function getLinterConfigurationOptions() {
$options = array(
'jscs.config' => array(
'type' => 'optional string',
'help' => pht('Pass in a custom jscsrc file path.'),
),
'jscs.preset' => array(
'type' => 'optional string',
'help' => pht('Custom preset.'),
),
);
return $options + parent::getLinterConfigurationOptions();
}
public function setLinterConfigurationValue($key, $value) {
switch ($key) {
case 'jscs.config':
$this->config = $value;
return;
case 'jscs.preset':
$this->preset = $value;
return;
}
return parent::setLinterConfigurationValue($key, $value);
}
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
$report_dom = new DOMDocument();
$ok = @$report_dom->loadXML($stdout);
if (!$ok) {
return false;
}
$messages = array();
foreach ($report_dom->getElementsByTagName('file') as $file) {
foreach ($file->getElementsByTagName('error') as $error) {
$message = new ArcanistLintMessage();
$message->setPath($path);
$message->setLine($error->getAttribute('line'));
$message->setChar($error->getAttribute('column'));
$message->setCode('JSCS');
$message->setDescription($error->getAttribute('message'));
switch ($error->getAttribute('severity')) {
case 'error':
$message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR);
break;
case 'warning':
$message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING);
break;
default:
$message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE);
break;
}
$messages[] = $message;
}
}
if ($err && !$messages) {
return false;
}
return $messages;
}
protected function getLintCodeFromLinterConfigurationKey($code) {
// NOTE: We can't figure out which rule generated each message, so we
// can not customize severities.
//
// See https://github.com/mdevils/node-jscs/issues/224
throw new Exception(
pht(
"JSCS does not currently support custom severity levels, because ".
"rules can't be identified from messages in output."));
}
}

View file

@ -0,0 +1,17 @@
<?php
final class ArcanistJscsLinterTestCase extends ArcanistArcanistLinterTestCase {
public function testJscsLinter() {
// NOTE: JSCS will only lint files with a `*.js` extension.
//
// See https://github.com/mdevils/node-jscs/issues/444
$this->assertTrue(true);
return;
$this->executeTestsInDirectory(
dirname(__FILE__).'/jscs/',
new ArcanistJscsLinter());
}
}

View file

@ -0,0 +1,12 @@
function foo() {
if (true) return 'foo'; else return 'bar';
}
~~~~~~~~~~
error:2:
~~~~~~~~~~
~~~~~~~~~~
{
"config": {
"jscs.preset": "jquery"
}
}