Skip to content

Commit

Permalink
Add a YAML linter (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
v-soares authored Oct 26, 2021
1 parent b276616 commit e57a4bf
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Pinterest.
- [Python Requirements](#python-requirements)
- [Spectral](#spectral)
- [ThriftCheck](#thriftcheck)
- [YamlLinter](#yamllinter)

We also welcome additional [contributions](CONTRIBUTING.md).

Expand Down Expand Up @@ -322,6 +323,18 @@ Lints Thrift IDL files using [ThriftCheck](https://github.com/pinterest/thriftch
}
```

### YamlLinter

Lints YAML files using [YamlLinter](https://yamllint.readthedocs.io/).

```json
{
"type": "yamllint",
"include": "(\\.(yml|yaml)$)",
"exclude": []
}
```

## Installation

In short, you'll need to add this repository to your local machine and tell
Expand Down
2 changes: 2 additions & 0 deletions __phutil_library_map__.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'PythonRequirementsLinter' => 'src/PythonRequirementsLinter.php',
'SpectralLinter' => 'src/SpectralLinter.php',
'ThriftCheckLinter' => 'src/ThriftCheckLinter.php',
'YamlLinter' => 'src/YamlLinter.php',
),
'function' => array(),
'xmap' => array(
Expand All @@ -58,5 +59,6 @@
'PythonRequirementsLinter' => 'ArcanistLinter',
'SpectralLinter' => 'NodeExternalLinter',
'ThriftCheckLinter' => 'PinterestExternalLinter',
'YamlLinter' => 'ArcanistLinter',
),
));
90 changes: 90 additions & 0 deletions src/YamlLinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright 2021 Pinterest, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Lints YAML files with yamllint
*/
final class YamlLinter extends ArcanistExternalLinter {

public function getInfoName() {
return "YamlLint";
}

public function getInfoURI() {
return "https://yamllint.readthedocs.io/";
}

public function getInfoDescription() {
return pht("Verifies the syntax of yaml config files");
}

public function getLinterName() {
return "YamlLint";
}

public function getLinterConfigurationName() {
return "yamllint";
}

public function getDefaultBinary() {
return "yamllint";
}

protected function getMandatoryFlags() {
return array("-f", "auto");
}

public function getInstallInstructions() {
return pht("run `brew install yamllint` on Mac or `sudo apt-get install yamllint` on Debian based Linux or `sudo dnf install yamllint` on RedHat based Linux");
}

public function shouldExpectCommandErrors() {
return true;
}

protected function canCustomizeLintSeverities() {
return true;
}

protected function parseLinterOutput($path, $err, $stdout, $stderr) {
$lines = phutil_split_lines($stdout, false);

// 6:22 error syntax error: mapping values are not allowed here (syntax)
$regex = '/^(?P<line>\\d+):(?P<offset>\\d+) +(?P<severity>warning|error) +(?P<message>.*) +\\((?P<name>.*)\\)$/';
$messages = array();
foreach ($lines as $line) {
$line = trim($line);
$matches = null;
if (!preg_match($regex, $line, $matches)) {
continue;
}

$message = new ArcanistLintMessage();
$message->setPath($path);
$message->setLine(trim($matches[1]));
$message->setName(trim($this->getLinterName()));
$message->setCode($matches[5]);
$message->setDescription($matches[4]);
$message->setSeverity($matches[3]);

$messages[] = $message;
}

return $messages;
}

}

0 comments on commit e57a4bf

Please sign in to comment.