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,44 +49,73 @@ 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) { if (($error = PhpParser\DiagnosticsProvider::checkDiagnostics($node)) !== null) {
$node = $sourceFileNode->$name; $range = PhpParser\PositionUtilities::getRangeFromPosition($error->start, $error->length, $this->sourceFileNode->fileContents);
if ($node === null) { switch ($error->kind) {
continue; case \Microsoft\PhpParser\DiagnosticKind::Error:
$severity = DiagnosticSeverity::ERROR;
break;
case \Microsoft\PhpParser\DiagnosticKind::Warning:
default:
$severity = DiagnosticSeverity::WARNING;
break;
} }
if (\is_array($node)) { $this->diagnostics[] = new Diagnostic(
foreach ($node as $child) { $error->message,
if ($child instanceof Node) { new Range(
$this->update($child); new Position($range->start->line, $range->start->character),
} new Position($range->end->line, $range->start->character)
),
null,
$severity,
'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;
} }
continue;
}
if ($node instanceof Node) { if (\is_array($child)) {
$this->update($node); foreach ($child as $actualChild) {
} if ($actualChild !== null) {
$this->traverse($actualChild);
if (($error = PhpParser\DiagnosticsProvider::checkDiagnostics($node)) !== null) { }
$range = PhpParser\PositionUtilities::getRangeFromPosition($error->start, $error->length, $this->sourceFileNode->fileContents); }
} else {
$this->diagnostics[] = new Diagnostic( $this->traverse($child);
$error->message, }
new Range(
new Position($range->start->line, $range->start->character),
new Position($range->end->line, $range->start->character)
),
null,
DiagnosticSeverity::ERROR,
'php'
);
} }
} }
} }
@ -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);
} }
/** /**