Elements. Migrate lib/error/listener.dart
Change-Id: I22ce676d84ff95cf8de485bf2fc91e31f0c27807
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412983
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/error/listener.dart b/pkg/analyzer/lib/error/listener.dart
index da8e662..af8d318 100644
--- a/pkg/analyzer/lib/error/listener.dart
+++ b/pkg/analyzer/lib/error/listener.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// ignore_for_file: analyzer_use_new_elements
-
import 'package:analyzer/dart/ast/ast.dart'
show AstNode, ConstructorDeclaration;
import 'package:analyzer/dart/ast/syntactic_entity.dart';
@@ -18,7 +16,6 @@
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/diagnostic/diagnostic.dart';
import 'package:analyzer/src/utilities/extensions/collection.dart';
-import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:meta/meta.dart';
import 'package:source_span/source_span.dart';
@@ -102,6 +99,7 @@
/// Report an error with the given [errorCode] and [arguments].
/// The [element] is used to compute the location of the error.
+ @Deprecated('Use atElement2() instead')
void atElement(
Element element,
ErrorCode errorCode, {
@@ -124,15 +122,17 @@
/// The [element] is used to compute the location of the error.
@experimental
void atElement2(
- Element2 element,
+ Element2 element2,
ErrorCode errorCode, {
List<Object>? arguments,
List<DiagnosticMessage>? contextMessages,
Object? data,
}) {
- atElement(
- element.asElement!,
- errorCode,
+ var nonSynthetic = element2.nonSynthetic2;
+ atOffset(
+ errorCode: errorCode,
+ offset: nonSynthetic.firstFragment.nameOffset2 ?? -1,
+ length: nonSynthetic.name3?.length ?? 0,
arguments: arguments,
contextMessages: contextMessages,
data: data,
@@ -195,7 +195,6 @@
var invalid = arguments
.whereNotType<String>()
.whereNotType<DartType>()
- .whereNotType<Element>()
.whereNotType<Element2>()
.whereNotType<int>()
.whereNotType<Uri>();
@@ -374,7 +373,7 @@
}
/// Used by [ErrorReporter._convertTypeNames] to keep track of an error argument
-/// that is an [Element], that is being converted to a display string.
+/// that is an [Element2], that is being converted to a display string.
class _ElementToConvert implements _ToConvert {
@override
final int index;
diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
index 2492d56..d31cf84 100644
--- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart
+++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
@@ -421,8 +421,8 @@
// TODO(paulberry): It would be really nice if we could extract enough
// information from the 'cycle' argument to provide the user with a
// description of the cycle.
- errorReporter.atElement(
- constant,
+ errorReporter.atElement2(
+ constant.asElement2!,
CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT,
);
(constant as VariableElementImpl).evaluationResult =
diff --git a/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart b/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart
index c075995..6324a93 100644
--- a/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart
+++ b/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart
@@ -484,14 +484,14 @@
errorCode =
CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER;
}
- _errorReporter.atElement(
- constructor,
+ _errorReporter.atElement2(
+ constructor.asElement2,
errorCode,
arguments: [name],
);
} else if (staticMember is MethodElement) {
- _errorReporter.atElement(
- constructor,
+ _errorReporter.atElement2(
+ constructor.asElement2,
CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_METHOD,
arguments: [name],
);
@@ -614,8 +614,8 @@
var baseName = accessor.displayName;
var inherited = _getInheritedMember(declarationElement, baseName);
if (inherited is MethodElement) {
- _errorReporter.atElement(
- accessor,
+ _errorReporter.atElement2(
+ accessor.asElement2,
CompileTimeErrorCode.CONFLICTING_FIELD_AND_METHOD,
arguments: [
declarationElement.displayName,
@@ -636,8 +636,8 @@
var baseName = method.displayName;
var inherited = _getInheritedMember(declarationElement, baseName);
if (inherited is PropertyAccessorElement) {
- _errorReporter.atElement(
- method,
+ _errorReporter.atElement2(
+ method.asElement2,
CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD,
arguments: [
declarationElement.displayName,
@@ -662,8 +662,8 @@
if (accessor.isStatic) {
var instance = _getInterfaceMember(declarationElement, baseName);
if (instance != null && baseName != 'values') {
- _errorReporter.atElement(
- accessor,
+ _errorReporter.atElement2(
+ accessor.asElement2,
CompileTimeErrorCode.CONFLICTING_STATIC_AND_INSTANCE,
arguments: [declarationName, baseName, declarationName],
);
@@ -679,8 +679,8 @@
if (method.isStatic) {
var instance = _getInterfaceMember(declarationElement, baseName);
if (instance != null) {
- _errorReporter.atElement(
- method,
+ _errorReporter.atElement2(
+ method.asElement2,
CompileTimeErrorCode.CONFLICTING_STATIC_AND_INSTANCE,
arguments: [declarationName, baseName, declarationName],
);
diff --git a/pkg/analyzer/lib/src/error/inheritance_override.dart b/pkg/analyzer/lib/src/error/inheritance_override.dart
index 9e0b4a6..7634299 100644
--- a/pkg/analyzer/lib/src/error/inheritance_override.dart
+++ b/pkg/analyzer/lib/src/error/inheritance_override.dart
@@ -611,8 +611,8 @@
buffer.write(separator);
}
buffer.write(element.displayName);
- reporter.atElement(
- classElement,
+ reporter.atElement2(
+ classElement.asElement2,
CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
arguments: [className, buffer.toString()],
);
@@ -622,8 +622,8 @@
// RECURSIVE_INTERFACE_INHERITANCE_BASE_CASE_IMPLEMENTS or
// RECURSIVE_INTERFACE_INHERITANCE_ON or
// RECURSIVE_INTERFACE_INHERITANCE_BASE_CASE_WITH
- reporter.atElement(
- classElement,
+ reporter.atElement2(
+ classElement.asElement2,
_getRecursiveErrorCode(element),
arguments: [className],
);
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index 702a7c3..317fb92 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -2412,8 +2412,8 @@
if (method.isStatic) {
void reportStaticConflict(ExecutableElement inherited) {
- errorReporter.atElement(
- method,
+ errorReporter.atElement2(
+ method.asElement2,
CompileTimeErrorCode.CONFLICTING_STATIC_AND_INSTANCE,
arguments: [
enclosingClass.displayName,
@@ -2440,8 +2440,8 @@
}
void reportFieldConflict(PropertyAccessorElement inherited) {
- errorReporter.atElement(
- method,
+ errorReporter.atElement2(
+ method.asElement2,
CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD,
arguments: [
enclosingClass.displayName,
@@ -2473,8 +2473,8 @@
enclosingClass, Name(libraryUri, '$name='));
if (accessor.isStatic && inherited != null) {
- errorReporter.atElement(
- accessor,
+ errorReporter.atElement2(
+ accessor.asElement2,
CompileTimeErrorCode.CONFLICTING_STATIC_AND_INSTANCE,
arguments: [
enclosingClass.displayName,
@@ -2488,8 +2488,8 @@
if (enclosingClass is ExtensionTypeElement) {
continue;
}
- errorReporter.atElement(
- accessor,
+ errorReporter.atElement2(
+ accessor.asElement2,
CompileTimeErrorCode.CONFLICTING_FIELD_AND_METHOD,
arguments: [
enclosingClass.displayName,
@@ -2513,8 +2513,8 @@
var setterName = methodName.forSetter;
var setter = inherited[setterName];
if (setter is PropertyAccessorElementOrMember) {
- errorReporter.atElement(
- enclosingClass,
+ errorReporter.atElement2(
+ enclosingClass.asElement2,
CompileTimeErrorCode.CONFLICTING_INHERITED_METHOD_AND_SETTER,
arguments: [
enclosingClass.kind.displayName,
@@ -2600,8 +2600,8 @@
var name = typeParameter.name;
// name is same as the name of the enclosing enum
if (element.name == name) {
- errorReporter.atElement(
- typeParameter,
+ errorReporter.atElement2(
+ typeParameter.asElement2,
CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_ENUM,
arguments: [name],
);
@@ -2610,8 +2610,8 @@
if (element.getMethod(name) != null ||
element.getGetter(name) != null ||
element.getSetter(name) != null) {
- errorReporter.atElement(
- typeParameter,
+ errorReporter.atElement2(
+ typeParameter.asElement2,
CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER_ENUM,
arguments: [name],
);
@@ -2628,8 +2628,8 @@
var name = typeParameter.name;
// name is same as the name of the enclosing class
if (element.name == name) {
- errorReporter.atElement(
- typeParameter,
+ errorReporter.atElement2(
+ typeParameter.asElement2,
CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_EXTENSION_TYPE,
arguments: [name],
);
@@ -2639,8 +2639,8 @@
element.getMethod(name) != null ||
element.getGetter(name) != null ||
element.getSetter(name) != null) {
- errorReporter.atElement(
- typeParameter,
+ errorReporter.atElement2(
+ typeParameter.asElement2,
CompileTimeErrorCode
.CONFLICTING_TYPE_VARIABLE_AND_MEMBER_EXTENSION_TYPE,
arguments: [name],
@@ -4699,8 +4699,8 @@
var superUnnamedConstructor = superElement.unnamedConstructor;
if (superUnnamedConstructor != null) {
if (superUnnamedConstructor.isFactory) {
- errorReporter.atElement(
- element,
+ errorReporter.atElement2(
+ element.asElement2,
CompileTimeErrorCode.NON_GENERATIVE_IMPLICIT_CONSTRUCTOR,
arguments: [
superElement.name,
@@ -4718,8 +4718,8 @@
if (!_typeProvider.isNonSubtypableClass(superType.element)) {
// Don't report this diagnostic for non-subtypable classes because the
// real problem was already reported.
- errorReporter.atElement(
- element,
+ errorReporter.atElement2(
+ element.asElement2,
CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT,
arguments: [superType, element.displayName],
);
@@ -6000,8 +6000,8 @@
// position.
if (!superVariance.greaterThanOrEqual(typeParameter.variance)) {
if (!typeParameter.isLegacyCovariant) {
- errorReporter.atElement(
- typeParameter,
+ errorReporter.atElement2(
+ typeParameter.asElement2,
CompileTimeErrorCode
.WRONG_EXPLICIT_TYPE_PARAMETER_VARIANCE_IN_SUPERINTERFACE,
arguments: [
@@ -6012,8 +6012,8 @@
],
);
} else {
- errorReporter.atElement(
- typeParameter,
+ errorReporter.atElement2(
+ typeParameter.asElement2,
CompileTimeErrorCode
.WRONG_TYPE_PARAMETER_VARIANCE_IN_SUPERINTERFACE,
arguments: [typeParameter.name, superInterface],
diff --git a/pkg/linter/lib/src/rules/analyzer_use_new_elements.dart b/pkg/linter/lib/src/rules/analyzer_use_new_elements.dart
index e63d98a..3b5e6b4 100644
--- a/pkg/linter/lib/src/rules/analyzer_use_new_elements.dart
+++ b/pkg/linter/lib/src/rules/analyzer_use_new_elements.dart
@@ -166,26 +166,32 @@
}
if (_isOldModelType(node.staticType)) {
+ _isDeprecatedNode(node);
rule.reportLint(node);
}
}
/// Returns whether [node] is or inside a deprecated node.
bool _isDeprecatedNode(AstNode? node) {
- if (node != null) {
- if (_deprecatedNodes[node] case var result?) {
- return result;
- }
- if (node is Declaration) {
- var element = node.declaredFragment?.element;
- if (element case Annotatable annotatable) {
- var hasDeprecated = annotatable.metadata2.hasDeprecated;
- return _deprecatedNodes[node] = hasDeprecated;
+ if (node == null) {
+ return false;
+ }
+
+ if (_deprecatedNodes[node] case var result?) {
+ return result;
+ }
+
+ if (node is Declaration) {
+ var element = node.declaredFragment?.element;
+ if (element case Annotatable annotatable) {
+ var hasDeprecated = annotatable.metadata2.hasDeprecated;
+ if (hasDeprecated) {
+ return _deprecatedNodes[node] = true;
}
}
- return _isDeprecatedNode(node.parent);
}
- return false;
+
+ return _deprecatedNodes[node] = _isDeprecatedNode(node.parent);
}
}
diff --git a/pkg/linter/test/rules/analyzer_use_new_elements_test.dart b/pkg/linter/test/rules/analyzer_use_new_elements_test.dart
index 45e8b7a..7a64344 100644
--- a/pkg/linter/test/rules/analyzer_use_new_elements_test.dart
+++ b/pkg/linter/test/rules/analyzer_use_new_elements_test.dart
@@ -117,6 +117,18 @@
''');
}
+ test_methodInvocation_inDeprecated2() async {
+ await assertNoDiagnostics(r'''
+import 'package:analyzer/dart/element/element.dart';
+
+@deprecated
+void f(Element element) {
+ var foo = element.nonSynthetic;
+ print(foo);
+}
+''');
+ }
+
test_namedType() async {
await assertDiagnostics(
r'''