1
0
Fork 0

Fix missing diagnostics for nodes (#484)

* Fix missing diagnostics for nodes

* Refactor TreeAnalyzer
pull/486/head
Stephan Unverwerth 2017-09-28 21:53:12 +02:00 committed by Felix Becker
parent a4739430f8
commit d4443465bb
1 changed files with 60 additions and 32 deletions

View File

@ -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);
} }
/** /**