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

Commit

Permalink
Add the lintMarkers method, which scans a source file for all lint …
Browse files Browse the repository at this point in the history
…markers
  • Loading branch information
Atry authored Nov 9, 2021
1 parent 06df759 commit 87abe3a
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 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,45 @@ final class File {
return $this->isDirty;
}

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

const type TLineNumber1Base = int;

/**
* Returns the 1-base line number of lint markers in the file.
*/
<<__Memoize>>
public function lintMarkers(): dict<string, keyset<self::TLineNumber1Base>> {
return $this->getLines()
|> Vec\map_with_key(
$$,
($line_index, $line_content) ==>
Regex\every_match($line_content, re"/(?<marker_name>\w+)\[\w+\]/")
|> Vec\filter(
$$,
$match ==> LintMarkerName::isValid($match['marker_name']),
)
|> Vec\map(
$$,
$match ==> shape(
'line_number' => $line_index + 1,
'marker' => $match[0],
),
),
)
|> Vec\flatten($$)
|> Dict\group_by($$, $line_marker ==> $line_marker['marker'])
|> Dict\map(
$$,
$line_markers ==> Keyset\map(
$line_markers,
$line_marker ==> $line_marker['line_number'],
),
);
}

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

0 comments on commit 87abe3a

Please sign in to comment.