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

Commit

Permalink
Get typeAliasses from types if not available
Browse files Browse the repository at this point in the history
  • Loading branch information
lexidor authored and Atry committed May 30, 2022
1 parent 178ad28 commit 186d1fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/TypeAssert.hack
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,21 @@ function is_nullable_enum<Tval as arraykey, T as \HH\BuiltinEnum<Tval>>(
return $value;
}

function is_array_of_shapes_with_name_field(
function is_array_of_shapes_with_name_field_and_kind(
mixed $value,
string $field,
): varray<shape('name' => string)> {
$msg = $field.'should be an vec<shape(\'name\' => string)>';
): varray<shape('name' => string, 'kindOf' => string)> {
$msg =
$field.'should be a vec<shape(\'name\' => string, \'kindOf\' => string)>';
invariant($value is Traversable<_>, '%s', $msg);
$out = varray[];
foreach ($value as $it) {
invariant($it is KeyedContainer<_, _>, '%s', $msg);
$name = $it['name'] ?? null;
invariant($name is string, '%s', $msg);
$out[] = shape('name' => $name);
$kind_of = $it['kindOf'] ?? null;
invariant($kind_of is string, '%s', $msg);
$out[] = shape('name' => $name, 'kindOf' => $kind_of);
}
return $out;
}
19 changes: 15 additions & 4 deletions src/builders/FactParseScanner.hack
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
namespace Facebook\AutoloadMap;

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

/** Create an autoload map from a directory using `ext_factparse`. */
final class FactParseScanner implements Builder {
const type TFacts = darray<string, shape(
'types' => varray<shape(
'name' => string,
'kindOf' => string,
)>,
'constants' => varray<string>,
'functions' => varray<string>,
Expand All @@ -38,11 +40,12 @@ final class FactParseScanner implements Builder {
);

try {
$types = TypeAssert\is_array_of_shapes_with_name_field_and_kind(
$facts['types'] ?? vec[],
'FactParse types',
);
$out[$file] = shape(
'types' => TypeAssert\is_array_of_shapes_with_name_field(
$facts['types'] ?? vec[],
'FactParse types',
),
'types' => $types,
'constants' => TypeAssert\is_array_of_strings(
$facts['constants'] ?? vec[],
'FactParse constants',
Expand All @@ -56,6 +59,14 @@ final class FactParseScanner implements Builder {
'FactParse typeAliases',
),
);

// On hhvm >4.160, typeAliases may not be present,
// we can extract type aliases from `types` where `kindOf` === `typeAlias`.
if (!C\contains_key($facts, 'typeAliases')) {
$out[$file]['typeAliases'] =
Vec\filter($types, $shape ==> $shape['kindOf'] === 'typeAlias')
|> Vec\map($$, $shape ==> $shape['name']);
}
} catch (\Exception $e) {
$error_level = \error_reporting(0);
$file_is_empty = \filesize($file) === 0;
Expand Down

0 comments on commit 186d1fc

Please sign in to comment.