mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 01:45:06 +00:00
Completion. Remove localVariableDistance(), and usage in RelevanceDataCollector.
Change-Id: I4ad00d1820fa996dac0abdb13649418a2fa97ed4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/356121 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
parent
68c74e2a97
commit
8705a06e21
|
@ -381,100 +381,6 @@ class FeatureComputer {
|
|||
return range.upper;
|
||||
}
|
||||
|
||||
/// Return the distance between the [reference] and [variable].
|
||||
///
|
||||
/// For [LocalVariableElement] the distance is the number of variable
|
||||
/// declarations between [variable] and the reference.
|
||||
///
|
||||
/// For [ParameterElement] the first one is considered to be "closer".
|
||||
/// Plus we add any local variables declared on the path to [reference].
|
||||
int localVariableDistance(AstNode reference, VariableElement variable) {
|
||||
var distance = 0;
|
||||
AstNode? node = reference;
|
||||
while (node != null) {
|
||||
if (node is FunctionExpression) {
|
||||
if (node.parameters case var parameterList?) {
|
||||
for (var parameter in parameterList.parameters) {
|
||||
if (parameter.declaredElement == variable) {
|
||||
return distance;
|
||||
}
|
||||
distance++;
|
||||
}
|
||||
}
|
||||
} else if (node is MethodDeclaration) {
|
||||
if (node.parameters case var parameterList?) {
|
||||
for (var parameter in parameterList.parameters) {
|
||||
if (parameter.declaredElement == variable) {
|
||||
return distance;
|
||||
}
|
||||
distance++;
|
||||
}
|
||||
}
|
||||
} else if (node is ForStatement || node is ForElement) {
|
||||
var loopParts = node is ForStatement
|
||||
? node.forLoopParts
|
||||
: (node as ForElement).forLoopParts;
|
||||
if (loopParts is ForPartsWithDeclarations) {
|
||||
for (var declaredVariable in loopParts.variables.variables.reversed) {
|
||||
if (declaredVariable.declaredElement == variable) {
|
||||
return distance;
|
||||
}
|
||||
distance++;
|
||||
}
|
||||
} else if (loopParts is ForEachPartsWithDeclaration) {
|
||||
if (loopParts.loopVariable.declaredElement == variable) {
|
||||
return distance;
|
||||
}
|
||||
distance++;
|
||||
}
|
||||
} else if (node is VariableDeclaration) {
|
||||
var parent = node.parent;
|
||||
if (parent is VariableDeclarationList) {
|
||||
var variables = parent.variables;
|
||||
var index = variables.indexOf(node);
|
||||
for (var i = index - 1; i >= 0; i--) {
|
||||
var declaredVariable = variables[i];
|
||||
if (declaredVariable.declaredElement == variable) {
|
||||
return distance;
|
||||
}
|
||||
distance++;
|
||||
}
|
||||
}
|
||||
} else if (node is CatchClause) {
|
||||
if (node.exceptionParameter?.declaredElement == variable ||
|
||||
node.stackTraceParameter?.declaredElement == variable) {
|
||||
return distance;
|
||||
}
|
||||
}
|
||||
if (node is Statement) {
|
||||
var parent = node.parent;
|
||||
var statements = const <Statement>[];
|
||||
if (parent is Block) {
|
||||
statements = parent.statements;
|
||||
} else if (parent is SwitchCase) {
|
||||
statements = parent.statements;
|
||||
} else if (parent is SwitchDefault) {
|
||||
statements = parent.statements;
|
||||
}
|
||||
var index = statements.indexOf(node);
|
||||
for (var i = index - 1; i >= 0; i--) {
|
||||
var statement = statements[i];
|
||||
if (statement is VariableDeclarationStatement) {
|
||||
for (var declaredVariable
|
||||
in statement.variables.variables.reversed) {
|
||||
if (declaredVariable.declaredElement == variable) {
|
||||
return distance;
|
||||
}
|
||||
distance++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
node = node.parent;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// Return the value of the _starts with dollar_ feature.
|
||||
double startsWithDollarFeature(String name) {
|
||||
return name.startsWith('\$') ? -1.0 : 0.0;
|
||||
|
|
|
@ -23,7 +23,6 @@ import 'package:analyzer/dart/element/element.dart'
|
|||
ExtensionElement,
|
||||
InterfaceElement,
|
||||
LibraryElement,
|
||||
LocalVariableElement,
|
||||
ParameterElement,
|
||||
PropertyAccessorElement;
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
|
@ -1692,16 +1691,6 @@ class RelevanceDataCollector extends RecursiveAstVisitor<void> {
|
|||
var definingElement = element.enclosingElement!;
|
||||
var depth = _parameterReferenceDepth(node, definingElement);
|
||||
_recordDistance('function depth of referenced parameter', depth);
|
||||
} else if (element is LocalVariableElement) {
|
||||
// TODO(brianwilkerson): This ignores the fact that nested functions can
|
||||
// reference variables declared in enclosing functions. Consider
|
||||
// additionally measuring the number of function boundaries that are
|
||||
// crossed and then reporting the distance with a label such as
|
||||
// 'local variable ($boundaryCount)'.
|
||||
var distance = node == null
|
||||
? -1
|
||||
: featureComputer.localVariableDistance(node, element);
|
||||
_recordDistance('distance to local variable', distance);
|
||||
} else if (element != null) {
|
||||
// TODO(brianwilkerson): We might want to cross reference the depth of
|
||||
// the declaration with the depth of the reference to see whether there
|
||||
|
|
Loading…
Reference in a new issue