diff --git a/pkg/analyzer/lib/error/listener.dart b/pkg/analyzer/lib/error/listener.dart index 05d960ba75f..08963223861 100644 --- a/pkg/analyzer/lib/error/listener.dart +++ b/pkg/analyzer/lib/error/listener.dart @@ -67,6 +67,35 @@ class ErrorReporter { Source get source => _source; + /// Report a diagnostic with the given [errorCode] and [arguments]. + /// The location of the diagnostic will be the name of the [node]. + void atConstructorDeclaration( + ConstructorDeclaration node, + ErrorCode errorCode, { + List? arguments, + List? contextMessages, + Object? data, + }) { + // TODO(brianwilkerson): Consider extending this method to take any + // declaration and compute the correct range for the name of that + // declaration. This might make it easier to be consistent. + if (node.name case var nameToken?) { + var offset = node.returnType.offset; + atOffset( + offset: offset, + length: nameToken.end - offset, + errorCode: errorCode, + arguments: arguments, + ); + } else { + atNode( + node.returnType, + errorCode, + arguments: arguments, + ); + } + } + /// Report an error with the given [errorCode] and [arguments]. /// The [element] is used to compute the location of the error. void atElement( @@ -211,30 +240,6 @@ class ErrorReporter { ); } - /// Report a diagnostic with the given [code] and [arguments]. The - /// location of the diagnostic will be the name of the [constructor]. - void reportErrorForName(ErrorCode code, ConstructorDeclaration constructor, - {List? arguments}) { - // TODO(brianwilkerson): Consider extending this method to take any - // declaration and compute the correct range for the name of that - // declaration. This might make it easier to be consistent. - if (constructor.name != null) { - var offset = constructor.returnType.offset; - atOffset( - offset: offset, - length: constructor.name!.end - offset, - errorCode: code, - arguments: arguments, - ); - } else { - atNode( - constructor.returnType, - code, - arguments: arguments, - ); - } - } - /// Report an error with the given [errorCode] and [arguments]. /// The [node] is used to compute the location of the error. @Deprecated('Use atNode() instead') diff --git a/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart b/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart index 868862dce69..228c0bec0b3 100644 --- a/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart +++ b/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart @@ -341,12 +341,16 @@ class MemberDuplicateDefinitionVerifier { } if (!constructorNames.add(name)) { if (name.isEmpty) { - _errorReporter.reportErrorForName( - CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_DEFAULT, member); + _errorReporter.atConstructorDeclaration( + member, + CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_DEFAULT, + ); } else { - _errorReporter.reportErrorForName( - CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_NAME, member, - arguments: [name]); + _errorReporter.atConstructorDeclaration( + member, + CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_NAME, + arguments: [name], + ); } } case FieldDeclaration(): diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index d0e882fdfcd..299c9f11eff 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -2909,9 +2909,10 @@ class ErrorVerifier extends RecursiveAstVisitor if (classElement is! ClassElement || !classElement.hasNonFinalField) { return; } - errorReporter.reportErrorForName( - CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD, - constructor); + errorReporter.atConstructorDeclaration( + constructor, + CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD, + ); } /// Verify that the given 'const' instance creation [expression] is not @@ -4710,9 +4711,9 @@ class ErrorVerifier extends RecursiveAstVisitor if (_enclosingClass is EnumElement && node.constKeyword == null && node.factoryKeyword == null) { - errorReporter.reportErrorForName( - CompileTimeErrorCode.NON_CONST_GENERATIVE_ENUM_CONSTRUCTOR, + errorReporter.atConstructorDeclaration( node, + CompileTimeErrorCode.NON_CONST_GENERATIVE_ENUM_CONSTRUCTOR, ); } } diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart index 01f512501b4..581e9ddbb7a 100644 --- a/pkg/analyzer/lib/src/generated/resolver.dart +++ b/pkg/analyzer/lib/src/generated/resolver.dart @@ -569,9 +569,9 @@ class ResolverVisitor extends ThrowingAstVisitor } } if (errorNode is ConstructorDeclaration) { - errorReporter.reportErrorForName( - errorCode, + errorReporter.atConstructorDeclaration( errorNode, + errorCode, arguments: [returnType], ); } else if (errorNode is BlockFunctionBody) {