Fix missing diagnostics for nodes (#484)
* Fix missing diagnostics for nodes * Refactor TreeAnalyzerpull/486/head
parent
a4739430f8
commit
d4443465bb
|
@ -49,34 +49,31 @@ class TreeAnalyzer
|
||||||
|
|
||||||
// TODO - docblock errors
|
// TODO - docblock errors
|
||||||
|
|
||||||
$this->collectDefinitionsAndReferences($this->sourceFileNode);
|
$this->traverse($this->sourceFileNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function collectDefinitionsAndReferences(Node $sourceFileNode)
|
/**
|
||||||
|
* Collects Parser diagnostic messages for the Node/Token
|
||||||
|
* and transforms them into LSP Format
|
||||||
|
*
|
||||||
|
* @param Node|Token $node
|
||||||
|
* @return Diagnostic
|
||||||
|
*/
|
||||||
|
private function collectDiagnostics($node)
|
||||||
{
|
{
|
||||||
foreach ($sourceFileNode::CHILD_NAMES as $name) {
|
|
||||||
$node = $sourceFileNode->$name;
|
|
||||||
|
|
||||||
if ($node === null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (\is_array($node)) {
|
|
||||||
foreach ($node as $child) {
|
|
||||||
if ($child instanceof Node) {
|
|
||||||
$this->update($child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($node instanceof Node) {
|
|
||||||
$this->update($node);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (($error = PhpParser\DiagnosticsProvider::checkDiagnostics($node)) !== null) {
|
if (($error = PhpParser\DiagnosticsProvider::checkDiagnostics($node)) !== null) {
|
||||||
$range = PhpParser\PositionUtilities::getRangeFromPosition($error->start, $error->length, $this->sourceFileNode->fileContents);
|
$range = PhpParser\PositionUtilities::getRangeFromPosition($error->start, $error->length, $this->sourceFileNode->fileContents);
|
||||||
|
|
||||||
|
switch ($error->kind) {
|
||||||
|
case \Microsoft\PhpParser\DiagnosticKind::Error:
|
||||||
|
$severity = DiagnosticSeverity::ERROR;
|
||||||
|
break;
|
||||||
|
case \Microsoft\PhpParser\DiagnosticKind::Warning:
|
||||||
|
default:
|
||||||
|
$severity = DiagnosticSeverity::WARNING;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$this->diagnostics[] = new Diagnostic(
|
$this->diagnostics[] = new Diagnostic(
|
||||||
$error->message,
|
$error->message,
|
||||||
new Range(
|
new Range(
|
||||||
|
@ -84,11 +81,43 @@ class TreeAnalyzer
|
||||||
new Position($range->end->line, $range->start->character)
|
new Position($range->end->line, $range->start->character)
|
||||||
),
|
),
|
||||||
null,
|
null,
|
||||||
DiagnosticSeverity::ERROR,
|
$severity,
|
||||||
'php'
|
'php'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursive AST traversal to collect definitions/references and diagnostics
|
||||||
|
*
|
||||||
|
* @param Node|Token $currentNode The node/token to process
|
||||||
|
*/
|
||||||
|
private function traverse($currentNode)
|
||||||
|
{
|
||||||
|
$this->collectDiagnostics($currentNode);
|
||||||
|
|
||||||
|
// Only update/descend into Nodes, Tokens are leaves
|
||||||
|
if ($currentNode instanceof Node) {
|
||||||
|
$this->collectDefinitionsAndReferences($currentNode);
|
||||||
|
|
||||||
|
foreach ($currentNode::CHILD_NAMES as $name) {
|
||||||
|
$child = $currentNode->$name;
|
||||||
|
|
||||||
|
if ($child === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (\is_array($child)) {
|
||||||
|
foreach ($child as $actualChild) {
|
||||||
|
if ($actualChild !== null) {
|
||||||
|
$this->traverse($actualChild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->traverse($child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,7 +125,7 @@ class TreeAnalyzer
|
||||||
*
|
*
|
||||||
* @param Node $node
|
* @param Node $node
|
||||||
*/
|
*/
|
||||||
private function update(Node $node)
|
private function collectDefinitionsAndReferences(Node $node)
|
||||||
{
|
{
|
||||||
$fqn = ($this->definitionResolver)::getDefinedFqn($node);
|
$fqn = ($this->definitionResolver)::getDefinedFqn($node);
|
||||||
// Only index definitions with an FQN (no variables)
|
// Only index definitions with an FQN (no variables)
|
||||||
|
@ -152,7 +181,6 @@ class TreeAnalyzer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->collectDefinitionsAndReferences($node);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue