Skip to content

Commit

Permalink
Merge branch 'main' into banish-hh-ignore-error-from-codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
lexidor committed Dec 24, 2023
2 parents e83c4e8 + 2339345 commit b5fbe55
Show file tree
Hide file tree
Showing 20 changed files with 259 additions and 44 deletions.
5 changes: 4 additions & 1 deletion codegen/inferred_relationships.hack

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions codegen/syntax/LambdaExpression.hack

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions codegen/syntax/QualifiedName.hack

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions codegen/tokens/NameToken.hack

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions src/Linters/NoEmptyStatementsLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ final class NoEmptyStatementsLinter extends AutoFixingASTLinter {
/**
* Returns whether the given token is an assignment operator.
*
* This list is all the types returned from ExpressionStatement::getOperator
* This list is all the types returned from BinaryExpression::getOperator
* that include "Equal" and are not comparison operators (==, >=, etc.);
*/
private function isAssignmentOperator(Token $op): bool {
Expand All @@ -140,9 +140,7 @@ final class NoEmptyStatementsLinter extends AutoFixingASTLinter {
$op is CaratEqualToken ||
$op is DotEqualToken ||
$op is EqualToken ||
$op is GreaterThanEqualToken ||
$op is GreaterThanGreaterThanEqualToken ||
$op is LessThanEqualToken ||
$op is LessThanLessThanEqualToken ||
$op is MinusEqualToken ||
$op is PercentEqualToken ||
Expand Down
4 changes: 2 additions & 2 deletions src/Linters/UnusedUseClauseLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ final class UnusedUseClauseLinter extends AutoFixingASTLinter {
$as = $name->getText();
} else {
invariant($name is QualifiedName, 'Unhandled name type');
$as = $name->getParts()->getChildrenOfItemsOfType(NameToken::class)
|> (C\lastx($$) as nonnull)->getText();
$as = $name->getParts()->getChildrenOfItemsByType<NameToken>()
|> C\lastx($$)->getText();
}
}
if ($kind is NamespaceToken) {
Expand Down
15 changes: 5 additions & 10 deletions src/Linters/UseStatementWIthoutKindLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ final class UseStatementWithoutKindLinter extends AutoFixingASTLinter {
}
$name = $clause->getName();
if ($name is QualifiedName) {
return (
C\lastx(
$name->getParts()->getChildrenOfItemsOfType(NameToken::class),
) as nonnull
return C\lastx(
$name->getParts()->getChildrenOfItemsByType<NameToken>(),
)->getText();
}
invariant(
Expand All @@ -73,10 +71,8 @@ final class UseStatementWithoutKindLinter extends AutoFixingASTLinter {
// We need to look at the full file to figure out if this should be a
// `use type`, or `use namespace`
$used = $this->getUnresolvedReferencedNames();
$used_as_ns = C\any(
$names,
$name ==> C\contains($used['namespaces'], $name),
);
$used_as_ns =
C\any($names, $name ==> C\contains($used['namespaces'], $name));
$used_as_type = C\any($names, $name ==> C\contains($used['types'], $name));

$leading = $node->getClauses()->getFirstTokenx()->getLeadingWhitespace();
Expand All @@ -94,8 +90,7 @@ final class UseStatementWithoutKindLinter extends AutoFixingASTLinter {
}

<<__Memoize>>
private function getUnresolvedReferencedNames(
): shape(
private function getUnresolvedReferencedNames(): shape(
'namespaces' => keyset<string>,
'types' => keyset<string>,
'functions' => keyset<string>,
Expand Down
6 changes: 3 additions & 3 deletions src/Migrations/HSLMigration.hack
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ final class HSLMigration extends BaseMigration {
}
$found_prefix = true;
foreach ($parts as $i => $token) {
if ($token?->getText() === $search[$i]) {
if ($token->getText() === $search[$i]) {
continue;
}
$found_prefix = false;
Expand Down Expand Up @@ -569,14 +569,14 @@ final class HSLMigration extends BaseMigration {
}

foreach ($parts as $i => $token) {
if ($i < 2 && $token?->getText() !== $search[$i]) {
if ($i < 2 && $token->getText() !== $search[$i]) {
break;
}

if ($i === 2) {
// we found an HH\Lib\* use statement, add the node and suffix
$nodes[] = $decl;
$ns = HslNamespace::coerce($token?->getText());
$ns = HslNamespace::coerce($token->getText());
if ($ns !== null) {
$suffixes[] = $ns;
}
Expand Down
10 changes: 5 additions & 5 deletions src/__Private/LintRunConfig.hack
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ final class LintRunConfig {
Vec\map($this->configFile['roots'], $dir ==> $this->projectRoot.'/'.$dir);
}

private function findOverride(string $file_path): ?self::TOverride {
return C\find(
private function findOverrides(string $file_path): vec<self::TOverride> {
return Vec\filter(
$this->configFile['overrides'] ?? vec[],
$override ==> C\find(
$override['patterns'],
Expand Down Expand Up @@ -206,8 +206,7 @@ final class LintRunConfig {
$blacklist = $this->configFile['disabledLinters'] ?? vec[];
$autofix_blacklist = $this->configFile['disabledAutoFixes'] ?? vec[];
$no_autofixes = $this->configFile['disableAllAutoFixes'] ?? false;
$override = $this->findOverride($file_path);
if ($override is nonnull) {
foreach ($this->findOverrides($file_path) as $override) {
if ($override['disableAllLinters'] ?? false) {
return shape(
'linters' => keyset[],
Expand Down Expand Up @@ -264,7 +263,8 @@ final class LintRunConfig {
$file_path is null ? null : $this->relativeFilePath($file_path)
|> $$ is null
? null
: $this->findOverride($$)['linterConfigs'][$classname] ?? null;
// TODO: This doesn't support multiple overrides.
: C\first($this->findOverrides($$))['linterConfigs'][$classname] ?? null;
if ($global_linter_config is null) {
if ($file_linter_config is null) {
return null;
Expand Down
5 changes: 3 additions & 2 deletions src/__Private/codegen/CodegenBase.hack
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ abstract class CodegenBase {
HHAST\ILambdaBody::class => keyset[
HHAST\IExpression::class,
HHAST\CompoundStatement::class,
// Constants are not wrapped in a name expression on the RHS of `==>`.
HHAST\NameToken::class,
HHAST\QualifiedName::class,
],
HHAST\ILambdaSignature::class => keyset[
HHAST\VariableExpression::class,
Expand All @@ -239,8 +242,6 @@ abstract class CodegenBase {
HHAST\ParameterDeclaration::class,
HHAST\PropertyDeclaration::class,
HHAST\LambdaExpression::class,
// HHAST\Php7AnonymousFunction::class : not valid in hack. No attributes
// if not hack
],
HHAST\INameishNode::class => keyset[
HHAST\NameToken::class,
Expand Down
2 changes: 1 addition & 1 deletion src/__Private/codegen/CodegenRelations.hack
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class CodegenRelations extends CodegenBase {

$relationships = new Ref(dict[]);
$queue = new Async\Semaphore(
/* limit = */ 32,
\cpu_get_count(),
async $file ==> {
try {
$links = await $this->getRelationsInFileAsync($file);
Expand Down
15 changes: 15 additions & 0 deletions src/__Private/codegen/data/LambdaBody.SyntaxExample.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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\__Private\SyntaxExamples;

function lambda_body(): void {
$_ = () ==> Qualified\CONSTANT;
$_ = () ==> CONSTANT;
$_ = () ==> 1 + CONSTANT;
}
37 changes: 31 additions & 6 deletions src/nodes/NodeList.hack
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@
namespace Facebook\HHAST;

use namespace HH\Lib\{C, Str, Vec};
use type Facebook\HHAST\_Private\SoftDeprecated;

/* HHAST_IGNORE_ALL[5624] */
final class NodeList<+Titem as Node> extends Node {
const string SYNTAX_KIND = 'list';
/**
* Use `NodeList::createMaybeEmptyList()` or
* `NodeList::createNonEmptyListNull()` instead to be explicit
* about desired behavior.
* Use `NodeList::createMaybeEmptyList(vec[])` or `null` instead of
* `new NodeList(vec[])` if you know the vec is always empty.
* A parsed Hack AST doesn't contain empty NodeLists.
*
* Side note: Places where you'd expect to find an empty NodeList:
* ```
* function no_params( ): void {}
* // ^
* ```
* The Hack parser places a "missing" node at the carat.
* HHAST uses `null` to represent them.
*/
<<__Override>>
public function __construct(
Expand All @@ -42,17 +51,33 @@ final class NodeList<+Titem as Node> extends Node {
return $this->_children;
}

public function getChildrenOfItems<T as ?Node>(
): vec<T> where Titem as ListItem<T> {
public function getChildrenOfItems<T as ?Node>(): vec<T>
where
Titem as ListItem<T> {
return Vec\map($this->getChildren(), $child ==> $child->getItem());
}

<<SoftDeprecated('$node->getChildrenOfItemsByType<T>()')>>
public function getChildrenOfItemsOfType<T as ?Node>(
classname<T> $what,
): vec<T> where Titem as ListItem<T> {
): vec<T> where Titem as ListItem<?Node> {
$out = vec[];
foreach ($this->getChildrenOfItems() as $item) {
if (\is_a($item, $what)) {
$out[] = \HH\FIXME\UNSAFE_CAST<?Node, T>(
$item,
'is_a($item, $what) ~= $item is T',
);
}
}
return $out;
}

public function getChildrenOfItemsByType<<<__Enforceable>> reify T as Node>(
): vec<T> where Titem as ListItem<?Node> {
$out = vec[];
foreach ($this->getChildrenOfItems() as $item) {
if ($item is T) {
$out[] = $item;
}
}
Expand Down
33 changes: 31 additions & 2 deletions test-data/hhast-lint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"roots": [ "." ],
"builtinLinters": "none",
"extraLinters": [
"Facebook\\HHAST\\Tests\\ValidConfigForLinter",
"Facebook\\HHAST\\Tests\\InvalidConfigForLinter",
Expand Down Expand Up @@ -27,5 +28,33 @@
"Facebook\\HHAST\\Tests\\ConfigTypeIsNotSupportedByTypeAssertLinter": {
"impossible": []
}
}
}
},
"overrides": [
{
"patterns": [
"single_override/*",
"multiple_overrides/*"
],
"extraLinters": [
"Facebook\\HHAST\\NoEmptyStatementsLinter",
"Facebook\\HHAST\\UseStatementWIthoutKindLinter"
],
"disabledLinters": [
"Facebook\\HHAST\\Tests\\ValidConfigForLinter",
"Facebook\\HHAST\\Tests\\InvalidConfigForLinter"
]
},
{
"patterns": [
"multiple_overrides/*"
],
"extraLinters": [
"Facebook\\HHAST\\NoFinalMethodInFinalClassLinter"
],
"disabledLinters": [
"Facebook\\HHAST\\FinalOrAbstractClassLinter",
"Facebook\\HHAST\\Tests\\ConfigTypeIsNotSupportedByTypeAssertLinter"
]
}
]
}
Loading

0 comments on commit b5fbe55

Please sign in to comment.