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

Commit

Permalink
Add hhastMarkers method, which scans a source file for all markers
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry authored Nov 9, 2021
1 parent 71e8e75 commit dc850e3
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/File.hack
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Facebook\HHAST;

use namespace HH\Lib\Str;
use namespace HH\Lib\{Dict, Keyset, Regex, Str, Vec};

final class File {
private function __construct(
Expand All @@ -22,6 +22,48 @@ final class File {
return $this->isDirty;
}

private function getLines(): vec<string> {
return Str\split($this->contents, '\n');
}

const type TLinterOrLintRuleName = string;
const type TLineNumber1Base = int;

/**
* Returns all lint markers in the file, indexed by the name of linter or
* lint rule, and then the line number.
*/
<<__Memoize>>
public function hhastMarkers(): dict<
self::TLinterOrLintRuleName,
dict<self::TLineNumber1Base, keyset<LintMarker>>,
> {
return $this->getLines()
|> Vec\map_with_key(
$$,
($line_index, $line_content) ==> Regex\every_match(
$line_content,
re"/(?<marker>\w+)\[(?<linter_or_lint_rule>\w+)\]/",
)
|> Vec\filter($$, $match ==> LintMarker::isValid($match['marker']))
|> Vec\map($$, $match ==> {
$match['line_number'] = $line_index + 1;
$match['marker'] = LintMarker::assert($match['marker']);
return $match;
}),
)
|> Vec\flatten($$)
|> Dict\group_by($$, $match ==> $match['linter_or_lint_rule'])
|> Dict\map(
$$,
$matches ==> Dict\group_by($matches, $match ==> $match['line_number'])
|> Dict\map(
$$,
$matches ==> Keyset\map($matches, $match ==> $match['marker']),
),
);
}

public function getPath(): string {
return $this->path;
}
Expand Down

0 comments on commit dc850e3

Please sign in to comment.