From 186d1fc5d5bf69caebaacac5c80a69e3c08f50d4 Mon Sep 17 00:00:00 2001 From: Lexidor Digital <31805625+lexidor@users.noreply.github.com> Date: Mon, 30 May 2022 00:17:39 +0200 Subject: [PATCH] Get typeAliasses from types if not available --- src/TypeAssert.hack | 11 +++++++---- src/builders/FactParseScanner.hack | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/TypeAssert.hack b/src/TypeAssert.hack index 27363d4..1a3c226 100644 --- a/src/TypeAssert.hack +++ b/src/TypeAssert.hack @@ -82,18 +82,21 @@ function is_nullable_enum>( 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 string)> { - $msg = $field.'should be an vec string)>'; +): varray string, 'kindOf' => string)> { + $msg = + $field.'should be a vec 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; } diff --git a/src/builders/FactParseScanner.hack b/src/builders/FactParseScanner.hack index 14e2f69..7d18a2c 100644 --- a/src/builders/FactParseScanner.hack +++ b/src/builders/FactParseScanner.hack @@ -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 varray string, + 'kindOf' => string, )>, 'constants' => varray, 'functions' => varray, @@ -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', @@ -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;