Skip to content

Commit

Permalink
Add PrettierLinter (MobileNativeFoundation#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
CrabDude authored and jparise committed Mar 19, 2018
1 parent bcd370a commit 7e482b8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Pinterest.
- [Checkstyle](#checkstyle)
- [ESLint](#eslint)
- [Go Vet](#go-vet)
- [Prettier](#prettier)
- [Python Imports](#python-imports)
- [Python isort](#python-isort)
- [Python Requirements](#python-requirements)
Expand Down Expand Up @@ -84,6 +85,20 @@ Lints JavaScript and JSX files using [ESLint](https://eslint.org/).
}
```


### Prettier

Formats JavaScript using [Prettier](https://prettier.io/).

```json
{
"type": "prettier",
"include": "(\\.js$)",
"bin": "./node_modules/.bin/prettier",
"prettier.cwd": "./"
}
```

### Go Vet

Uses the [Go vet command](https://golang.org/cmd/vet/) to lint for suspicious
Expand Down
2 changes: 2 additions & 0 deletions __phutil_library_map__.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'class' => array(
'ApacheThriftGeneratedLinter' => 'src/ApacheThriftGeneratedLinter.php',
'ApacheThriftLinter' => 'src/ApacheThriftLinter.php',
'PrettierLinter' => 'src/PrettierLinter.php',
'CheckstyleLinter' => 'src/CheckstyleLinter.php',
'ESLintLinter' => 'src/ESLintLinter.php',
'GoVetLinter' => 'src/GoVetLinter.php',
Expand All @@ -22,6 +23,7 @@
'xmap' => array(
'ApacheThriftGeneratedLinter' => 'ArcanistLinter',
'ApacheThriftLinter' => 'ArcanistExternalLinter',
'PrettierLinter' => 'ArcanistExternalLinter',
'CheckstyleLinter' => 'ArcanistExternalLinter',
'ESLintLinter' => 'ArcanistExternalLinter',
'GoVetLinter' => 'ArcanistExternalLinter',
Expand Down
101 changes: 101 additions & 0 deletions src/PrettierLinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Copyright 2018 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 JavaScript and JSX files using Prettier
*/
final class PrettierLinter extends ArcanistExternalLinter {
private $cwd = '';

public function getInfoName() {
return 'Prettier';
}

public function getInfoURI() {
return 'https://prettier.io/';
}

public function getInfoDescription() {
return pht('An opinionated code formatter with canonicalized AST-derived output');
}

public function getLinterName() {
return 'PRETTIER';
}

public function getLinterConfigurationName() {
return 'prettier';
}

public function getDefaultBinary() {
list($err, $stdout, $stderr) = exec_manual('yarn -s --cwd %s which prettier', $this->getProjectRoot() . '/' . $this->cwd);
$binaryPath = strtok($stdout, "\n");
return $binaryPath;
}

public function getVersion() {
list($err, $stdout, $stderr) = exec_manual('%C -v', $this->getExecutableCommand());
return $stdout;
}

public function getLinterConfigurationOptions() {
$options = array(
'prettier.cwd' => array(
'type' => 'optional string',
'help' => pht('Specify a project sub-directory for both the local prettier install and the sub-directory to lint within.'),
),
);
return $options + parent::getLinterConfigurationOptions();
}

public function setLinterConfigurationValue($key, $value) {
switch ($key) {
case 'prettier.cwd':
$this->cwd = $value;
return;
}
return parent::setLinterConfigurationValue($key, $value);
}

public function getInstallInstructions() {
return pht(
'run `%s` to install yarn globally (needed for specifying --cwd), and `%s` to add prettier to your project (configurable at prettier.cwd).',
'npm install --global yarn',
'yarn add --dev prettier'
);
}

protected function parseLinterOutput($path, $err, $stdout, $stderr) {
if ($err) {
return false;
}

$message = new ArcanistLintMessage();
$message->setPath($path);
$message->setSeverity(ArcanistLintSeverity::SEVERITY_AUTOFIX);
$message->setName('Prettier Format');
$message->setLine(1);
$message->setCode($this->getLinterName());
$message->setChar(1);
$message->setDescription('Your file has not been prettier-ified');
$message->setOriginalText($this->getData($path));
$message->setReplacementText($stdout);
$messages[] = $message;

return $messages;
}
}

0 comments on commit 7e482b8

Please sign in to comment.