Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
Calculate blame code for the HHClientLinter
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry authored Nov 9, 2021
1 parent fef425b commit 9d5c688
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
23 changes: 21 additions & 2 deletions src/Linters/HHClientLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Facebook\HHAST;

use namespace Facebook\TypeAssert;
use namespace HH\Lib\{C, Vec};
use namespace HH\Lib\{C, Str, Vec};

/**
* A linter as a proxy invoking `hh_client --lint`.
Expand All @@ -25,6 +25,20 @@ final class HHClientLinter implements Linter {
'version' => string,
);

private static function blameCode(
vec<string> $file_lines,
HHClientLintError::TJSONError $error,
): string {
$line_number_1base = $error['line'];
$line_index_0base = $line_number_1base - 1;
$line_content = $file_lines[$line_index_0base];
return Str\slice(
$line_content,
$error['start'],
$error['end'] - $error['start'],
);
}

public async function getLintErrorsAsync(
): Awaitable<vec<HHClientLintError>> {
$lines = await __Private\execute_async(
Expand All @@ -43,9 +57,14 @@ final class HHClientLinter implements Linter {
\JSON_FB_HACK_ARRAYS,
),
);
$file_lines = Str\split($this->getFile()->getContents(), "\n");
return Vec\map(
$hh_client_lint_result['errors'],
$error ==> new HHClientLintError($this->file, $error),
$error ==> new HHClientLintError(
$this->file,
$error,
$this::blameCode($file_lines, $error),
),
);
}
}
4 changes: 2 additions & 2 deletions tests/examples/HHClientLinter/invalid_null_check.hack.expect
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"blame": null,
"blame_pretty": null,
"blame": "cannot_be_null is null",
"blame_pretty": "cannot_be_null is null",
"description": "Invalid null check: This expression will always return `false`.\nA value of type `int` can never be null."
}
]

0 comments on commit 9d5c688

Please sign in to comment.