Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix config with multiple overrides for a single file #550

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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"
]
}
]
}
31 changes: 31 additions & 0 deletions tests/LinterConfigTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,35 @@ final class LinterConfigTest extends TestCase {
'specified an unsupported config type',
);
}

public function testSingleOverride(): void {
$lrc = static::getLintRunConfig();
$config = $lrc->getConfigForFile('single_override/test.hack');

expect($config)->toNotBeNull('Config could not be fetched');
expect($config)->toEqual(shape(
'linters' => keyset [
'Facebook\\HHAST\\FinalOrAbstractClassLinter',
'Facebook\\HHAST\\Tests\\ConfigTypeIsNotSupportedByTypeAssertLinter',
'Facebook\\HHAST\\NoEmptyStatementsLinter',
'Facebook\\HHAST\\UseStatementWIthoutKindLinter',
],
'autoFixBlacklist' => keyset [],
));
}

public function testMultipleOverrides(): void {
$lrc = static::getLintRunConfig();
$config = $lrc->getConfigForFile('multiple_overrides/test.hack');

expect($config)->toNotBeNull('Config could not be fetched');
expect($config)->toEqual(shape(
'linters' => keyset [
'Facebook\\HHAST\\NoEmptyStatementsLinter',
'Facebook\\HHAST\\UseStatementWIthoutKindLinter',
'Facebook\\HHAST\\NoFinalMethodInFinalClassLinter',
],
'autoFixBlacklist' => keyset [],
));
}
}