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

Commit afeede0

Browse files
authored
fix stringify_typehint() bugs (#35)
...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)
1 parent 1e88409 commit afeede0

8 files changed

+34
-113
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ sudo: required
22
language: generic
33
services: docker
44
env:
5-
- HHVM_VERSION=4.14-latest
5+
- HHVM_VERSION=4.21-latest
66
- HHVM_VERSION=latest
77
- HHVM_VERSION=nightly
88
install:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"hhvm/hsl": "^4.0",
88
"facebook/fbmarkdown": "^1.0",
99
"facebook/hh-clilib": "^2.1.0",
10-
"facebook/definition-finder": "^2.0.0",
10+
"facebook/definition-finder": "^2.12.3",
1111
"hhvm/hhast": "^4.14"
1212
},
1313
"require-dev": {

src/PageSections/InterfaceSynopsis.hack

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ final class InterfaceSynopsis extends PageSection {
143143

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

146-
$p = $c->getParentClassName();
146+
$p = $c->getParentClassInfo();
147147
if ($p !== null) {
148-
$ret .= ' extends '._Private\ns_normalize_type($ns, $p);
148+
$ret .= ' extends '._Private\stringify_typehint($ns, $p);
149149
}
150-
if ($interfaces = $c->getInterfaceNames()) {
150+
if ($interfaces = $c->getInterfaceInfo()) {
151151
$ret .= $interfaces
152-
|> Vec\map($$, $i ==> _Private\ns_normalize_type($ns, $i))
152+
|> Vec\map($$, $i ==> _Private\stringify_typehint($ns, $i))
153153
|> Str\join($$, ', ')
154154
|> ' implements '.$$;
155155
}

src/PageSections/TypeDeclaration.hack

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,24 @@ final class TypeDeclaration extends PageSection {
4040
$code .= 'newtype ';
4141
}
4242

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

4545
if ($t is ScannedType) {
46-
$code .= ' = '.
47-
_Private\stringify_typehint(
48-
$t->getNamespaceName(),
49-
$t->getAliasedType(),
50-
).
51-
';';
52-
} else {
53-
$code .= ';';
46+
$code .= ' = ';
47+
// We want custom multi-line formatting for shapes here, so not calling
48+
// stringify_typehint() for those. Note that we still use the default
49+
// stringify_typehint() for shapes in other places, e.g. as function
50+
// arguments.
51+
$code .= $t->getAliasedType()->isShape()
52+
? _Private\stringify_shape($ns, $t->getAliasedType()->getShapeFields())
53+
: _Private\stringify_typehint(
54+
$t->getNamespaceName(),
55+
$t->getAliasedType(),
56+
);
5457
}
5558

59+
$code .= ';';
60+
5661
return Str\format("```Hack\n%s\n```", $code);
5762
}
5863
}

src/PageSections/_Private/AUTO_IMPORT_TYPES.hack

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/PageSections/_Private/ns_normalize_type.hack

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/PageSections/_Private/stringify_generic.hack

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ function stringify_generic(
2323
} else {
2424
$base = '';
2525
}
26-
$base .= ns_normalize_type($ns, $generic->getName());
26+
$base .= $generic->getName();
2727

2828
$constraints = $generic->getConstraints();
2929
if (C\is_empty($constraints)) {
3030
return $base;
3131
}
3232

3333
return $constraints
34-
|> Vec\map($$, $c ==> $c['relationship'].' '.$c['type']->getTypeText())
34+
|> Vec\map(
35+
$$,
36+
$c ==> $c['relationship'].' '.stringify_typehint($ns, $c['type']),
37+
)
3538
|> Str\join($$, ' ')
3639
|> $base.' '.$$;
3740
}

src/PageSections/_Private/stringify_typehint.hack

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,18 @@
99

1010
namespace Facebook\HHAPIDoc\PageSections\_Private;
1111

12-
use type Facebook\DefinitionFinder\ScannedTypehint;
13-
use namespace HH\Lib\{C, Str, Vec};
12+
use type Facebook\DefinitionFinder\{ScannedTypehint, TypeTextOptions};
1413

14+
/**
15+
* Render `$type` as concisely and unambiguously as possible in the current
16+
* namespace.
17+
*/
1518
function stringify_typehint(
1619
string $ns,
1720
ScannedTypehint $type,
1821
): string {
19-
$s = $type->isNullable() ? '?' : '';
20-
if ($type->isShape()) {
21-
return $s.stringify_shape($ns, $type->getShapeFields());
22-
}
23-
invariant($type->getTypeName() !== 'shape', 'got a shape thats not a shape');
24-
$s .= ns_normalize_type($ns, $type->getTypeName());
25-
26-
$generics = $type->getGenericTypes();
27-
if (C\is_empty($generics)) {
28-
return $s;
29-
}
30-
31-
$s .= $generics
32-
|> Vec\map($$, $sub ==> stringify_typehint($ns, $sub))
33-
|> Str\join($$, ', ')
34-
|> '<'.$$.'>';
35-
36-
return $s;
22+
// This just delegates to the implementation in ScannedTypehint, but we keep
23+
// this function here to ensure that we always call getTypeText() consistently
24+
// with the same options.
25+
return $type->getTypeText($ns, TypeTextOptions::STRIP_AUTOIMPORTED_NAMESPACE);
3726
}

0 commit comments

Comments
 (0)