From: Anton Kovalyov Date: Fri, 13 Jan 2012 23:21:57 +0000 (-0800) Subject: Adds ArcanistJSHintLinter to check JS files for errors and potential problems. X-Git-Tag: conduit-4~188 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=e5bda0520060ee73f590132adb1f0ffa30fcbc66;p=arcanist.git Adds ArcanistJSHintLinter to check JS files for errors and potential problems. This patch adds ArcanistJSHintLinter class and three new .arcconfig options: * lint.jshint.prefix - directory where JSHint binary resides * lint.jshint.bin - JSHint binary name (if different from 'jshint') * lint.jshint.config - a JSON file with JSHint project-wide options By default, this linter assumes that JSHint is installed on user's system as an NPM package. Test Plan: (1) Run `npm install jshint -g` to install JSHint and add ArcanistJSHintLinter to your Lint Engine (I didn't see PEP8 or PyFlakes in the PhutilLintEngine so I decided to not to put JSHint in there). After that you can do `arc lint my.js`. (2) Create a new file, config.json, and add `{ "white": true }` in it. Add `"lint.jshint.config": "/path/to/config.json"` to your .arcconfig and run `arc lint my.js`. After that, unless your name is Douglas Crockford, you'll see tons of JSHint warnings about minor PEP8-like things (spacing, etc.) --- diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 467d61e1..8e540872 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -46,6 +46,7 @@ phutil_register_library_map(array( 'ArcanistHelpWorkflow' => 'workflow/help', 'ArcanistHookAPI' => 'repository/hookapi/base', 'ArcanistInstallCertificateWorkflow' => 'workflow/install-certificate', + 'ArcanistJSHintLinter' => 'lint/linter/jshint', 'ArcanistLiberateLintEngine' => 'lint/engine/liberate', 'ArcanistLiberateWorkflow' => 'workflow/liberate', 'ArcanistLicenseLinter' => 'lint/linter/license', @@ -127,6 +128,7 @@ phutil_register_library_map(array( 'ArcanistGitHookPreReceiveWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistHelpWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistInstallCertificateWorkflow' => 'ArcanistBaseWorkflow', + 'ArcanistJSHintLinter' => 'ArcanistLinter', 'ArcanistLiberateLintEngine' => 'ArcanistLintEngine', 'ArcanistLiberateWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistLicenseLinter' => 'ArcanistLinter', diff --git a/src/lint/linter/jshint/ArcanistJSHintLinter.php b/src/lint/linter/jshint/ArcanistJSHintLinter.php new file mode 100644 index 00000000..db4c53a2 --- /dev/null +++ b/src/lint/linter/jshint/ArcanistJSHintLinter.php @@ -0,0 +1,170 @@ + ArcanistLintSeverity::SEVERITY_ERROR + ); + } + + public function getLintNameMap() { + return array( + self::JSHINT_ERROR => "JSHint Error" + ); + } + + public function getJSHintOptions() { + $working_copy = $this->getEngine()->getWorkingCopy(); + $options = '--reporter '.dirname(realpath(__FILE__)).'/reporter.js'; + $config = $working_copy->getConfig('lint.jshint.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; + } + + private function getJSHintPath() { + $working_copy = $this->getEngine()->getWorkingCopy(); + $prefix = $working_copy->getConfig('lint.jshint.prefix'); + $bin = $working_copy->getConfig('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; + } + + // Look for globally installed JSHint + list($err) = exec_manual('which %s', $bin); + if ($err) { + 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) { + $jshint_bin = $this->getJSHintPath(); + $jshint_options = $this->getJSHintOptions(); + $futures = array(); + + foreach ($paths as $path) { + $filepath = $this->getEngine()->getFilePathOnDisk($path); + $futures[$path] = new ExecFuture("{$jshint_bin} {$filepath} ${jshint_options}"); + } + + foreach (Futures($futures)->limit(8) as $path => $future) { + $this->results[$path] = $future->resolve(); + } + } + + public function lintPath($path) { + list($rc, $stdout) = $this->results[$path]; + + 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( + "JSHint returned output we can't parse. Check that your JSHint installation.\n". + "Output:\n". + $stdout); + } + + foreach ($errors as $err) { + $this->raiseLintAtLine( + $err->line, + $err->col, + self::JSHINT_ERROR, + $err->reason); + } + } +} diff --git a/src/lint/linter/jshint/__init__.php b/src/lint/linter/jshint/__init__.php new file mode 100644 index 00000000..80349853 --- /dev/null +++ b/src/lint/linter/jshint/__init__.php @@ -0,0 +1,16 @@ +