This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
245 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HHAST; | ||
|
||
final class HHClientLintError implements LintError { | ||
|
||
const type TJSONError = shape( | ||
'descr' => string, | ||
'severity' => HHClientLintSeverity, | ||
'path' => string, | ||
'line' => int, | ||
'start' => int, | ||
'end' => int, | ||
'code' => int, | ||
'bypass_changed_lines' => bool, | ||
'original' => string, | ||
'replacement' => string, | ||
); | ||
|
||
public function __construct( | ||
private File $file, | ||
private this::TJSONError $error, | ||
private ?string $blame_code = null | ||
) { | ||
} | ||
|
||
public function getFile(): File { | ||
return $this->file; | ||
} | ||
|
||
public function getDescription(): string { | ||
return $this->error['descr']; | ||
} | ||
|
||
public function getPosition(): (int, int) { | ||
return tuple($this->error['line'], $this->error['end']); | ||
} | ||
|
||
public function getRange(): ((int, int), (int, int)) { | ||
return tuple( | ||
tuple($this->error['line'], $this->error['start']), | ||
tuple($this->error['line'], $this->error['end']), | ||
); | ||
} | ||
|
||
public function getBlameCode(): ?string { | ||
return $this->blame_code; | ||
} | ||
|
||
public function getPrettyBlame(): ?string { | ||
return $this->getBlameCode(); | ||
} | ||
|
||
<<__Memoize>> | ||
public function getLintRule(): LintRule { | ||
return new HHClientLintRule($this->error['code']); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HHAST; | ||
|
||
/** | ||
* The lint rule of an error code reported by the hh_client | ||
*/ | ||
final class HHClientLintRule implements LintRule { | ||
public function getName(): string { | ||
return 'Linter: '.$this->code; | ||
} | ||
|
||
public function getErrorCode(): string { | ||
return (string)$this->code; | ||
} | ||
|
||
public function __construct(private int $code) {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HHAST; | ||
enum HHClientLintSeverity: string as string { | ||
ADVICE = 'advice'; | ||
AUTOFIX = 'autofix'; | ||
WARNING = 'warning'; | ||
ERROR = 'error'; | ||
DISABLED = 'disabled'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HHAST; | ||
|
||
use namespace Facebook\TypeAssert; | ||
use namespace HH\Lib\{C, Vec}; | ||
|
||
/** | ||
* A linter as a proxy invoking `hh_client --lint`. | ||
*/ | ||
final class HHClientLinter implements Linter { | ||
use LinterTrait; | ||
|
||
const type TConfig = shape(); | ||
|
||
const type TJSONResult = shape( | ||
'errors' => vec<HHClientLintError::TJSONError>, | ||
'version' => string, | ||
); | ||
|
||
public async function getLintErrorsAsync( | ||
): Awaitable<vec<HHClientLintError>> { | ||
$lines = await __Private\execute_async( | ||
'hh_client', | ||
'--lint', | ||
$this->getFile()->getPath(), | ||
'--json', | ||
'--from', | ||
'hhast', | ||
); | ||
$hh_client_lint_result = TypeAssert\matches<this::TJSONResult>( | ||
\json_decode( | ||
C\firstx($lines), | ||
/* assoc = */ true, | ||
/* depth = */ 512, | ||
\JSON_FB_HACK_ARRAYS, | ||
), | ||
); | ||
return Vec\map( | ||
$hh_client_lint_result['errors'], | ||
$error ==> new HHClientLintError($this->file, $error), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HHAST; | ||
use namespace HH\Lib\Str; | ||
|
||
final class HHClientLinterTest extends TestCase { | ||
use LinterTestTrait; | ||
|
||
/** | ||
* The temporary directory to include testing source files to lint. | ||
* | ||
* Note that the temporary directory must be under the current directory, | ||
* otherwise hh_client will not work. | ||
*/ | ||
const string TMP_DIR = __DIR__.'/../.var/tmp/hhast/HHClientLinterTest'; | ||
|
||
public function getCleanExamples(): vec<(string)> { | ||
return vec[ | ||
tuple("<?hh\nclass Foo {}"), | ||
]; | ||
} | ||
|
||
protected function getLinter(string $file): HHClientLinter { | ||
$ext = Str\strip_suffix($file, '.in') | ||
|> Str\ends_with($$, '.php') | ||
|> $$ ? 'php' : 'hack'; | ||
$hh_client_tmp_file = | ||
self::TMP_DIR.'/'.\bin2hex(\random_bytes(16)).'.'.$ext; | ||
\copy($file, $hh_client_tmp_file); | ||
return HHClientLinter::fromPath($hh_client_tmp_file); | ||
} | ||
|
||
<<__Override>> | ||
public static async function beforeFirstTestAsync(): Awaitable<void> { | ||
$mode = 0777; | ||
$recursive = true; | ||
\mkdir(self::TMP_DIR, $mode, $recursive); | ||
} | ||
|
||
<<__Override>> | ||
public static async function afterLastTestAsync(): Awaitable<void> { | ||
foreach (\scandir(self::TMP_DIR) as $file) { | ||
$path_file = self::TMP_DIR.'/'.$file; | ||
if (\is_file($path_file)) { | ||
\unlink($path_file); | ||
} | ||
} | ||
\rmdir(self::TMP_DIR); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"blame": null, | ||
"blame_pretty": null, | ||
"description": "Invalid null check: This expression will always return `false`.\nA value of type `int` can never be null." | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?hh // strict | ||
|
||
/** | ||
* Copyright (c) 2016, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
function invalid_null_check(): void { | ||
$cannot_be_null = 42; | ||
if ($cannot_be_null is null) { | ||
throw new Exception(); | ||
} | ||
} |