forked from hhvm/hack-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support auto-fixing AST-based linters
fixes hhvm#79
- Loading branch information
1 parent
dd72a26
commit fb12b4b
Showing
14 changed files
with
265 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?hh // strict | ||
/* | ||
* 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\Linters; | ||
|
||
use type Facebook\HHAST\EditableNode; | ||
use namespace Facebook\HHAST; | ||
|
||
interface LSPAutoFixingLinter<Terror as FixableLintError> extends AutoFixingLinter<Terror> { | ||
public function getCodeActionForError( | ||
Terror $err, | ||
): ?\Facebook\HHAST\__Private\LSP\CodeAction; | ||
} |
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,44 @@ | ||
<?hh // strict | ||
/* | ||
* 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\LSPImpl; | ||
|
||
use type Facebook\HHAST\__Private\{ | ||
LintRunConfig, | ||
LintRunLSPCodeActionEventHandler, | ||
}; | ||
use namespace Facebook\HHAST\__Private\{LSP, LSPLib}; | ||
use namespace HH\Lib\Str; | ||
|
||
final class CodeActionCommand extends LSPLib\CodeActionCommand { | ||
const type TResponse = vec<LSP\CodeAction>; | ||
|
||
public function __construct( | ||
private LSPLib\Client $client, | ||
private ?LintRunConfig $config, | ||
) { | ||
} | ||
|
||
<<__Override>> | ||
public async function executeAsync( | ||
self::TParams $p, | ||
): Awaitable<this::TExecuteResult> { | ||
$uri = $p['textDocument']['uri']; | ||
|
||
$handler = new LintRunLSPCodeActionEventHandler( | ||
$this->client, | ||
$p['context']['diagnostics'], | ||
); | ||
|
||
await relint_uri_async($handler, $this->config, $uri); | ||
|
||
return self::success($handler->getCodeActions()); | ||
} | ||
} |
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,20 @@ | ||
<?hh // strict | ||
/* | ||
* 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\LSPLib; | ||
|
||
use namespace Facebook\HHAST\__Private\LSP; | ||
|
||
abstract class CodeActionCommand extends ServerCommand { | ||
const string METHOD = 'textDocument/codeAction'; | ||
const type TParams = LSP\CodeActionParams; | ||
const type TErrorCode = int; | ||
const type TErrorData = mixed; | ||
} |
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,87 @@ | ||
<?hh // strict | ||
/* | ||
* 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; | ||
|
||
use namespace Facebook\HHAST\Linters; | ||
use namespace Facebook\HHAST\__Private\{LSP, LSPLib}; | ||
use namespace HH\Lib\{C, Dict, Str, Vec}; | ||
|
||
final class LintRunLSPCodeActionEventHandler implements LintRunEventHandler { | ||
private vec<LSP\CodeAction> $codeActions = vec[]; | ||
|
||
public function __construct( | ||
private LSPLib\Client $client, | ||
private vec<LSP\Diagnostic> $diagnostics, | ||
) { | ||
} | ||
|
||
public function linterRaisedErrors( | ||
Linters\BaseLinter $linter, | ||
LintRunConfig::TFileConfig $_config, | ||
Traversable<Linters\LintError> $errors, | ||
): LintAutoFixResult { | ||
if (!$linter instanceof Linters\LSPAutoFixingLinter) { | ||
return LintAutoFixResult::SOME_UNFIXED; | ||
} | ||
|
||
$linter_class = \get_class($linter); | ||
foreach ($errors as $error) { | ||
$d = $this->findDiagnostic($linter, $error); | ||
if ($d === null) { | ||
continue; | ||
} | ||
|
||
$action = $linter->getCodeActionForError($error); | ||
if ($action === null) { | ||
continue; | ||
} | ||
$action['diagnostics'] = vec[$d]; | ||
$this->codeActions[] = $action; | ||
} | ||
|
||
return LintAutoFixResult::SOME_UNFIXED; | ||
} | ||
|
||
private function findDiagnostic( | ||
Linters\BaseLinter $linter, | ||
Linters\LintError $error, | ||
): ?LSP\Diagnostic { | ||
$linter = \get_class($linter) | ||
|> Str\split($$, "\\") | ||
|> C\lastx($$) | ||
|> Str\strip_suffix($$, "Linter"); | ||
$pos = $error->getPosition() ?? tuple(0, 0); | ||
$start = shape( | ||
'line' => $pos[0] - 1, | ||
'character' => $pos[1], | ||
); | ||
foreach ($this->diagnostics as $d) { | ||
if (($d['code'] ?? '') !== $linter) { | ||
continue; | ||
} | ||
if ($d['range']['start'] !== $start) { | ||
continue; | ||
} | ||
return $d; | ||
} | ||
return null; | ||
} | ||
|
||
public function finishedFile(string $_path, LintRunResult $_result): void { | ||
} | ||
|
||
public function finishedRun(LintRunResult $_): void { | ||
} | ||
|
||
public function getCodeActions(): vec<LSP\CodeAction> { | ||
return $this->codeActions; | ||
} | ||
} |
Oops, something went wrong.