]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a native JSON linter.
authorJoshua Spence <josh@joshuaspence.com>
Sat, 21 Jun 2014 20:41:22 +0000 (06:41 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Sat, 21 Jun 2014 20:41:22 +0000 (06:41 +1000)
Summary:
Ref T5297. Utilize `PhutilJSONParser` to create a native JSON linter (native in that it is pure PHP with no external dependencies).

Since [[https://github.com/Seldaek/jsonlint | JsonLint]] is literally a PHP port of [[https://github.com/zaach/jsonlint | jsonlint]], I figured that we should also deprecate `ArcanistJSONLintLinter`. `ArcanistJSONLinter` //should// behave identically to `ArcanistJSONLintLinter`.

Depends on D9634.

Test Plan: Ran the `ArcanistJSONLintLinter` unit tests.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

Maniphest Tasks: T5297

Differential Revision: https://secure.phabricator.com/D9628

src/__phutil_library_map__.php
src/lint/linter/ArcanistJSONLinter.php [new file with mode: 0644]
src/lint/linter/__tests__/ArcanistJSONLinterTestCase.php [new file with mode: 0644]

index 1b325c6c2f6516d0cdfd136bcfe777898b8c4bed..58826e12cc42a020db295548a39d76e1ac953548 100644 (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',
+    'ArcanistJSONLinter' => 'lint/linter/ArcanistJSONLinter.php',
+    'ArcanistJSONLinterTestCase' => 'lint/linter/__tests__/ArcanistJSONLinterTestCase.php',
     'ArcanistJscsLinter' => 'lint/linter/ArcanistJscsLinter.php',
     'ArcanistJscsLinterTestCase' => 'lint/linter/__tests__/ArcanistJscsLinterTestCase.php',
     'ArcanistLandWorkflow' => 'workflow/ArcanistLandWorkflow.php',
@@ -276,6 +278,8 @@ phutil_register_library_map(array(
     'ArcanistJSHintLinterTestCase' => 'ArcanistArcanistLinterTestCase',
     'ArcanistJSONLintLinter' => 'ArcanistExternalLinter',
     'ArcanistJSONLintLinterTestCase' => 'ArcanistArcanistLinterTestCase',
+    'ArcanistJSONLinter' => 'ArcanistLinter',
+    'ArcanistJSONLinterTestCase' => 'ArcanistArcanistLinterTestCase',
     'ArcanistJscsLinter' => 'ArcanistExternalLinter',
     'ArcanistJscsLinterTestCase' => 'ArcanistArcanistLinterTestCase',
     'ArcanistLandWorkflow' => 'ArcanistBaseWorkflow',
diff --git a/src/lint/linter/ArcanistJSONLinter.php b/src/lint/linter/ArcanistJSONLinter.php
new file mode 100644 (file)
index 0000000..ff03a8c
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+/**
+ * A linter for JSON files.
+ */
+final class ArcanistJSONLinter extends ArcanistLinter {
+
+  public function getInfoName() {
+    return 'JSON Lint';
+  }
+
+  public function getInfoDescription() {
+    return pht('Detect syntax errors in JSON files.');
+  }
+
+  public function getLinterName() {
+    return 'JSON';
+  }
+
+  public function getLinterConfigurationName() {
+    return 'json';
+  }
+
+  public function lintPath($path) {
+    $data = $this->getData($path);
+
+    try {
+      $parser = new PhutilJSONParser();
+      $parser->parse($data);
+    } catch (PhutilJSONParserException $ex) {
+      $this->raiseLintAtLine(
+        $ex->getSourceLine(),
+        $ex->getSourceChar(),
+        $this->getLinterName(),
+        $ex->getMessage());
+    }
+  }
+
+}
diff --git a/src/lint/linter/__tests__/ArcanistJSONLinterTestCase.php b/src/lint/linter/__tests__/ArcanistJSONLinterTestCase.php
new file mode 100644 (file)
index 0000000..1913c0d
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistJSONLinterTestCase extends ArcanistArcanistLinterTestCase {
+
+  public function testJSONLinter() {
+    $this->executeTestsInDirectory(
+      dirname(__FILE__).'/jsonlint/',
+      new ArcanistJSONLinter());
+  }
+
+}