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

Commit

Permalink
fix stringify_typehint() bugs (#35)
Browse files Browse the repository at this point in the history
...mostly by delegating to the new ScannedTypehint::getTypeText(), but also a few other fixes, e.g. a few places were calling stringify_typehint() incorrectly.

This is the diff when running on itself: https://gist.github.com/jjergus/bcf87815d8ca29f661c60dc02be4f396 (all changes are good)
  • Loading branch information
jjergus authored Sep 9, 2019
1 parent 1e88409 commit afeede0
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sudo: required
language: generic
services: docker
env:
- HHVM_VERSION=4.14-latest
- HHVM_VERSION=4.21-latest
- HHVM_VERSION=latest
- HHVM_VERSION=nightly
install:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"hhvm/hsl": "^4.0",
"facebook/fbmarkdown": "^1.0",
"facebook/hh-clilib": "^2.1.0",
"facebook/definition-finder": "^2.0.0",
"facebook/definition-finder": "^2.12.3",
"hhvm/hhast": "^4.14"
},
"require-dev": {
Expand Down
8 changes: 4 additions & 4 deletions src/PageSections/InterfaceSynopsis.hack
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ final class InterfaceSynopsis extends PageSection {

$ret .= $c->getShortName();

$p = $c->getParentClassName();
$p = $c->getParentClassInfo();
if ($p !== null) {
$ret .= ' extends '._Private\ns_normalize_type($ns, $p);
$ret .= ' extends '._Private\stringify_typehint($ns, $p);
}
if ($interfaces = $c->getInterfaceNames()) {
if ($interfaces = $c->getInterfaceInfo()) {
$ret .= $interfaces
|> Vec\map($$, $i ==> _Private\ns_normalize_type($ns, $i))
|> Vec\map($$, $i ==> _Private\stringify_typehint($ns, $i))
|> Str\join($$, ', ')
|> ' implements '.$$;
}
Expand Down
23 changes: 14 additions & 9 deletions src/PageSections/TypeDeclaration.hack
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,24 @@ final class TypeDeclaration extends PageSection {
$code .= 'newtype ';
}

$code .= _Private\ns_normalize_type($ns, $t->getName());
$code .= $t->getShortName();

if ($t is ScannedType) {
$code .= ' = '.
_Private\stringify_typehint(
$t->getNamespaceName(),
$t->getAliasedType(),
).
';';
} else {
$code .= ';';
$code .= ' = ';
// We want custom multi-line formatting for shapes here, so not calling
// stringify_typehint() for those. Note that we still use the default
// stringify_typehint() for shapes in other places, e.g. as function
// arguments.
$code .= $t->getAliasedType()->isShape()
? _Private\stringify_shape($ns, $t->getAliasedType()->getShapeFields())
: _Private\stringify_typehint(
$t->getNamespaceName(),
$t->getAliasedType(),
);
}

$code .= ';';

return Str\format("```Hack\n%s\n```", $code);
}
}
43 changes: 0 additions & 43 deletions src/PageSections/_Private/AUTO_IMPORT_TYPES.hack

This file was deleted.

33 changes: 0 additions & 33 deletions src/PageSections/_Private/ns_normalize_type.hack

This file was deleted.

7 changes: 5 additions & 2 deletions src/PageSections/_Private/stringify_generic.hack
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ function stringify_generic(
} else {
$base = '';
}
$base .= ns_normalize_type($ns, $generic->getName());
$base .= $generic->getName();

$constraints = $generic->getConstraints();
if (C\is_empty($constraints)) {
return $base;
}

return $constraints
|> Vec\map($$, $c ==> $c['relationship'].' '.$c['type']->getTypeText())
|> Vec\map(
$$,
$c ==> $c['relationship'].' '.stringify_typehint($ns, $c['type']),
)
|> Str\join($$, ' ')
|> $base.' '.$$;
}
29 changes: 9 additions & 20 deletions src/PageSections/_Private/stringify_typehint.hack
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,18 @@

namespace Facebook\HHAPIDoc\PageSections\_Private;

use type Facebook\DefinitionFinder\ScannedTypehint;
use namespace HH\Lib\{C, Str, Vec};
use type Facebook\DefinitionFinder\{ScannedTypehint, TypeTextOptions};

/**
* Render `$type` as concisely and unambiguously as possible in the current
* namespace.
*/
function stringify_typehint(
string $ns,
ScannedTypehint $type,
): string {
$s = $type->isNullable() ? '?' : '';
if ($type->isShape()) {
return $s.stringify_shape($ns, $type->getShapeFields());
}
invariant($type->getTypeName() !== 'shape', 'got a shape thats not a shape');
$s .= ns_normalize_type($ns, $type->getTypeName());

$generics = $type->getGenericTypes();
if (C\is_empty($generics)) {
return $s;
}

$s .= $generics
|> Vec\map($$, $sub ==> stringify_typehint($ns, $sub))
|> Str\join($$, ', ')
|> '<'.$$.'>';

return $s;
// This just delegates to the implementation in ScannedTypehint, but we keep
// this function here to ensure that we always call getTypeText() consistently
// with the same options.
return $type->getTypeText($ns, TypeTextOptions::STRIP_AUTOIMPORTED_NAMESPACE);
}

0 comments on commit afeede0

Please sign in to comment.