This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration for top-level requires (#197)
* Add migration for top-level requires() Adds a HH_FIXME[1002] if it can't find an entrypoint function. * Auto-generate lambdas in TopLevelRequiresMigration The function that requires a file like: ``` <?hh // partial $foo = 123; ``` ... now has the variable `$foo` set to 123. Limit the scope * migrate self * Remove FIXMEs - if it's strict and has requires, it probably has an entrypoint - if it's partial, it can have top-level requires * Don't migrate files that have classes with parents This kind of file needs manual refactoring: ``` require_once('Bar.php'); class Foo extends Bar {} ```
- Loading branch information
1 parent
201bc7b
commit 0ee8177
Showing
25 changed files
with
329 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HHAST; | ||
|
||
use namespace HH\Lib\{C, Str, Vec}; | ||
|
||
/** Move requires to top-level `<<__EntryPoint>>` functions */ | ||
final class TopLevelRequiresMigration extends BaseMigration { | ||
<<__Override>> | ||
public function migrateFile(string $_path, Script $script): Script { | ||
return \HH\Asio\join($this->migrateFileAsync($script)); | ||
} | ||
|
||
private async function migrateFileAsync(Script $script): Awaitable<Script> { | ||
$decls = $script->getDeclarations(); | ||
$includes = $decls->getChildrenOfType(InclusionDirective::class); | ||
if (C\is_empty($includes)) { | ||
return $script; | ||
} | ||
|
||
$entrypoint = $decls->getChildrenOfType(FunctionDeclaration::class) | ||
|> Vec\filter( | ||
$$, | ||
$f ==> C\any( | ||
$f->getAttributeSpec()?->getAttributes()?->getChildrenOfItems() ?? | ||
vec[], | ||
$attr ==> | ||
($attr->getType() as NameToken)->getText() === '__EntryPoint', | ||
), | ||
) | ||
|> C\first($$); | ||
|
||
if (!$entrypoint) { | ||
return $script; | ||
} | ||
$classes = $script->getDescendantsOfType(ClassishDeclaration::class); | ||
if ( | ||
C\any( | ||
$classes, | ||
$c ==> $c->hasExtendsKeyword() || $c->hasImplementsKeyword(), | ||
) | ||
) { | ||
/* This kind of file needs to be manually refactored: | ||
* | ||
* require_once('Bar.php'); | ||
* class Foo extends Bar {} | ||
*/ | ||
return $script; | ||
} | ||
|
||
// Figure out leading whitespace | ||
$body = $entrypoint->getBody(); | ||
if (!$body is CompoundStatement) { | ||
// Invalid, but e.g. `<<__EntryPoint>> function foo(): void;` | ||
return $script; | ||
} | ||
|
||
$leading = $body->getStatements()?->toVec() ?? vec[] | ||
|> C\first($$) | ||
|> $$?->getFirstTokenx()?->getLeadingWhitespace() | ||
|> $$?->getText() ?? ' '; | ||
|
||
// Generate a lambda so that if the require()'d file sets variables in | ||
// the psuedomain, they don't affect the entrypoint | ||
$includes_text = Vec\map( | ||
$includes, | ||
$incl ==> { | ||
$t = $incl->getFirstTokenx(); | ||
return $incl->replace( | ||
$t, | ||
$t->withLeading(new NodeList(vec[new WhiteSpace($leading.$leading)])), | ||
) | ||
->getCode(); | ||
}, | ||
) | ||
|> Str\join($$, ''); | ||
$lambda = await self::statementFromCodeAsync( | ||
$leading. | ||
"(() ==> {\n". | ||
$leading. | ||
$leading. | ||
"// HHAST-generated to avoid pseudomain local leaks\n". | ||
$includes_text. | ||
$leading. | ||
"})();\n\n", | ||
); | ||
|
||
|
||
$body = $body->withStatements( | ||
new NodeList( | ||
Vec\concat(vec[$lambda], $body->getStatements()?->getChildren() ?? []), | ||
), | ||
); | ||
|
||
return $script->withDeclarations( | ||
$script->getDeclarations() | ||
->filterChildren($decl ==> !C\contains($includes, $decl)) | ||
->replaceChild($entrypoint, $entrypoint->withBody($body)), | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HHAST; | ||
|
||
use namespace HH\Lib\{Str, Vec}; | ||
|
||
final class TopLevelRequiresMigrationTest extends MigrationTest { | ||
const type TMigration = TopLevelRequiresMigration; | ||
|
||
<<__Override>> | ||
public function getExamples(): vec<(string)> { | ||
$prefix = __DIR__.'/examples/'; | ||
$dir = $prefix.'migrations/TopLevelRequires/'; | ||
return Vec\concat(\glob($dir.'*.php.in'), \glob($dir.'*.hack.in')) | ||
|> Vec\map( | ||
$$, | ||
$file ==> | ||
tuple(Str\strip_suffix(Str\strip_prefix($file, $prefix), '.in')), | ||
); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/examples/migrations/TopLevelRequires/all_types.php.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?hh | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
(() ==> { | ||
// HHAST-generated to avoid pseudomain local leaks | ||
include('a'); | ||
include_once('b'); | ||
require('c'); | ||
require_once('d'); | ||
})(); | ||
foo(); | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/examples/migrations/TopLevelRequires/all_types.php.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?hh | ||
|
||
include('a'); | ||
include_once('b'); | ||
require('c'); | ||
require_once('d'); | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
foo(); | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/examples/migrations/TopLevelRequires/eight_spaces.php.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?hh | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
(() ==> { | ||
// HHAST-generated to avoid pseudomain local leaks | ||
require_once('foo'); | ||
})(); | ||
bar(); | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/examples/migrations/TopLevelRequires/eight_spaces.php.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?hh | ||
|
||
require_once('foo'); | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
bar(); | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/examples/migrations/TopLevelRequires/empty_entrypoint.php.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?hh | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
(() ==> { | ||
// HHAST-generated to avoid pseudomain local leaks | ||
require_once('foo'); | ||
})(); | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/examples/migrations/TopLevelRequires/empty_entrypoint.php.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?hh | ||
|
||
require_once('foo'); | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/examples/migrations/TopLevelRequires/has_class_extends.hack.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require_once('Foo.hack'); | ||
|
||
class Bar extends Foo {} | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
} |
Oops, something went wrong.