From 9bfc687c22142bd0e595d443ad6d394c2671d70c Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Tue, 17 Feb 2026 13:57:10 -0800 Subject: [PATCH] [messages] Use literate diagnostic reporting for data driven fixes. Changes the code that parses the YAML description of data driven fixes so that it reports errors using the literate diagnostic reporting API. Change-Id: I6a6a6964e8f78eb5fd313e3ee5b84d1b6cdac10d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/481180 Commit-Queue: Paul Berry Reviewed-by: Brian Wilkerson --- pkg/analysis_server/lib/src/diagnostic.g.dart | 77 ++++++++++--------- .../fix/data_driven/code_fragment_parser.dart | 77 +++++++++---------- .../fix/data_driven/transform_set_parser.dart | 31 ++++---- pkg/analysis_server/messages.yaml | 30 ++++---- 4 files changed, 109 insertions(+), 106 deletions(-) diff --git a/pkg/analysis_server/lib/src/diagnostic.g.dart b/pkg/analysis_server/lib/src/diagnostic.g.dart index f3cae37798f..2d669179b14 100644 --- a/pkg/analysis_server/lib/src/diagnostic.g.dart +++ b/pkg/analysis_server/lib/src/diagnostic.g.dart @@ -83,9 +83,9 @@ invalidChangeForKind = DiagnosticWithArguments( ); /// Parameters: -/// Object p0: the character that is invalid +/// String text: the character that is invalid const DiagnosticWithArguments< - LocatableDiagnostic Function({required Object p0}) + LocatableDiagnostic Function({required String text}) > invalidCharacter = DiagnosticWithArguments( name: 'invalid_character', @@ -93,7 +93,7 @@ invalidCharacter = DiagnosticWithArguments( type: DiagnosticType.COMPILE_TIME_ERROR, uniqueName: 'invalid_character', withArguments: _withArgumentsInvalidCharacter, - expectedTypes: [ExpectedType.object], + expectedTypes: [ExpectedType.string], ); /// Parameters: @@ -217,9 +217,9 @@ const DiagnosticWithoutArguments missingTemplateEnd = ); /// Parameters: -/// Object p0: a description of the expected kinds of tokens +/// String validKinds: a description of the expected kinds of tokens const DiagnosticWithArguments< - LocatableDiagnostic Function({required Object p0}) + LocatableDiagnostic Function({required String validKinds}) > missingToken = DiagnosticWithArguments( name: 'missing_token', @@ -227,7 +227,7 @@ missingToken = DiagnosticWithArguments( type: DiagnosticType.COMPILE_TIME_ERROR, uniqueName: 'missing_token', withArguments: _withArgumentsMissingToken, - expectedTypes: [ExpectedType.object], + expectedTypes: [ExpectedType.string], ); /// No parameters. @@ -240,9 +240,9 @@ const DiagnosticWithoutArguments missingUri = DiagnosticWithoutArgumentsImpl( ); /// Parameters: -/// Object p0: the missing key +/// String key: the missing key const DiagnosticWithArguments< - LocatableDiagnostic Function({required Object p0}) + LocatableDiagnostic Function({required String key}) > undefinedVariable = DiagnosticWithArguments( name: 'undefined_variable', @@ -250,13 +250,13 @@ undefinedVariable = DiagnosticWithArguments( type: DiagnosticType.COMPILE_TIME_ERROR, uniqueName: 'undefined_variable', withArguments: _withArgumentsUndefinedVariable, - expectedTypes: [ExpectedType.object], + expectedTypes: [ExpectedType.string], ); /// Parameters: -/// Object p0: the token that was unexpectedly found +/// String tokenKind: the token that was unexpectedly found const DiagnosticWithArguments< - LocatableDiagnostic Function({required Object p0}) + LocatableDiagnostic Function({required String tokenKind}) > unexpectedTransformSetToken = DiagnosticWithArguments( name: 'unexpected_transform_set_token', @@ -264,13 +264,13 @@ unexpectedTransformSetToken = DiagnosticWithArguments( type: DiagnosticType.COMPILE_TIME_ERROR, uniqueName: 'unexpected_transform_set_token', withArguments: _withArgumentsUnexpectedTransformSetToken, - expectedTypes: [ExpectedType.object], + expectedTypes: [ExpectedType.string], ); /// Parameters: -/// Object p0: a description of the expected kind of token +/// String accessor: a description of the expected kind of token const DiagnosticWithArguments< - LocatableDiagnostic Function({required Object p0}) + LocatableDiagnostic Function({required String accessor}) > unknownAccessor = DiagnosticWithArguments( name: 'unknown_accessor', @@ -278,7 +278,7 @@ unknownAccessor = DiagnosticWithArguments( type: DiagnosticType.COMPILE_TIME_ERROR, uniqueName: 'unknown_accessor', withArguments: _withArgumentsUnknownAccessor, - expectedTypes: [ExpectedType.object], + expectedTypes: [ExpectedType.string], ); /// Parameters: @@ -318,10 +318,13 @@ const DiagnosticWithoutArguments unsupportedVersion = ); /// Parameters: -/// Object p0: a description of the expected kind of token -/// Object p1: a description of the actual kind of token +/// String validKinds: a description of the expected kind of token +/// String actualKind: a description of the actual kind of token const DiagnosticWithArguments< - LocatableDiagnostic Function({required Object p0, required Object p1}) + LocatableDiagnostic Function({ + required String validKinds, + required String actualKind, + }) > wrongToken = DiagnosticWithArguments( name: 'wrong_token', @@ -329,13 +332,13 @@ wrongToken = DiagnosticWithArguments( type: DiagnosticType.COMPILE_TIME_ERROR, uniqueName: 'wrong_token', withArguments: _withArgumentsWrongToken, - expectedTypes: [ExpectedType.object, ExpectedType.object], + expectedTypes: [ExpectedType.string, ExpectedType.string], ); /// Parameters: -/// Object p0: the message produced by the YAML parser +/// String message: the message produced by the YAML parser const DiagnosticWithArguments< - LocatableDiagnostic Function({required Object p0}) + LocatableDiagnostic Function({required String message}) > yamlSyntaxError = DiagnosticWithArguments( name: 'yaml_syntax_error', @@ -343,7 +346,7 @@ yamlSyntaxError = DiagnosticWithArguments( type: DiagnosticType.COMPILE_TIME_ERROR, uniqueName: 'yaml_syntax_error', withArguments: _withArgumentsYamlSyntaxError, - expectedTypes: [ExpectedType.object], + expectedTypes: [ExpectedType.string], ); LocatableDiagnostic _withArgumentsConflictingKey({ @@ -373,8 +376,8 @@ LocatableDiagnostic _withArgumentsInvalidChangeForKind({ ]); } -LocatableDiagnostic _withArgumentsInvalidCharacter({required Object p0}) { - return LocatableDiagnosticImpl(diag.invalidCharacter, [p0]); +LocatableDiagnostic _withArgumentsInvalidCharacter({required String text}) { + return LocatableDiagnosticImpl(diag.invalidCharacter, [text]); } LocatableDiagnostic _withArgumentsInvalidKey({required String keyType}) { @@ -416,22 +419,22 @@ LocatableDiagnostic _withArgumentsMissingOneOfMultipleKeys({ return LocatableDiagnosticImpl(diag.missingOneOfMultipleKeys, [validKeys]); } -LocatableDiagnostic _withArgumentsMissingToken({required Object p0}) { - return LocatableDiagnosticImpl(diag.missingToken, [p0]); +LocatableDiagnostic _withArgumentsMissingToken({required String validKinds}) { + return LocatableDiagnosticImpl(diag.missingToken, [validKinds]); } -LocatableDiagnostic _withArgumentsUndefinedVariable({required Object p0}) { - return LocatableDiagnosticImpl(diag.undefinedVariable, [p0]); +LocatableDiagnostic _withArgumentsUndefinedVariable({required String key}) { + return LocatableDiagnosticImpl(diag.undefinedVariable, [key]); } LocatableDiagnostic _withArgumentsUnexpectedTransformSetToken({ - required Object p0, + required String tokenKind, }) { - return LocatableDiagnosticImpl(diag.unexpectedTransformSetToken, [p0]); + return LocatableDiagnosticImpl(diag.unexpectedTransformSetToken, [tokenKind]); } -LocatableDiagnostic _withArgumentsUnknownAccessor({required Object p0}) { - return LocatableDiagnosticImpl(diag.unknownAccessor, [p0]); +LocatableDiagnostic _withArgumentsUnknownAccessor({required String accessor}) { + return LocatableDiagnosticImpl(diag.unknownAccessor, [accessor]); } LocatableDiagnostic _withArgumentsUnsupportedKey({required String key}) { @@ -439,12 +442,12 @@ LocatableDiagnostic _withArgumentsUnsupportedKey({required String key}) { } LocatableDiagnostic _withArgumentsWrongToken({ - required Object p0, - required Object p1, + required String validKinds, + required String actualKind, }) { - return LocatableDiagnosticImpl(diag.wrongToken, [p0, p1]); + return LocatableDiagnosticImpl(diag.wrongToken, [validKinds, actualKind]); } -LocatableDiagnostic _withArgumentsYamlSyntaxError({required Object p0}) { - return LocatableDiagnosticImpl(diag.yamlSyntaxError, [p0]); +LocatableDiagnostic _withArgumentsYamlSyntaxError({required String message}) { + return LocatableDiagnosticImpl(diag.yamlSyntaxError, [message]); } diff --git a/pkg/analysis_server/lib/src/services/correction/fix/data_driven/code_fragment_parser.dart b/pkg/analysis_server/lib/src/services/correction/fix/data_driven/code_fragment_parser.dart index 877a191c8fd..b9679dd6c2f 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix/data_driven/code_fragment_parser.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix/data_driven/code_fragment_parser.dart @@ -7,7 +7,7 @@ import 'package:analysis_server/src/services/correction/fix/data_driven/accessor import 'package:analysis_server/src/services/correction/fix/data_driven/expression.dart'; import 'package:analysis_server/src/services/correction/fix/data_driven/variable_scope.dart'; import 'package:analysis_server/src/services/refactoring/framework/formal_parameter.dart'; -import 'package:analyzer/error/listener.dart'; +import 'package:analyzer/src/error/listener.dart'; import 'package:analyzer/src/utilities/extensions/string.dart'; // Several "report" functions intentionally return a `Null`-typed value. @@ -83,11 +83,13 @@ class CodeFragmentParser { } accessors.add(accessor); } else { - diagnosticReporter.atOffset( - offset: token.offset + delta, - length: token.length, - diagnosticCode: diag.wrongToken, - arguments: ['.', token.kind.displayName], + diagnosticReporter.report( + diag.wrongToken + .withArguments( + validKinds: '.', + actualKind: token.kind.displayName, + ) + .atOffset(offset: token.offset + delta, length: token.length), ); return null; } @@ -116,11 +118,10 @@ class CodeFragmentParser { var expression = _parseLogicalAndExpression(); if (currentIndex < _tokens.length) { var token = _tokens[currentIndex]; - diagnosticReporter.atOffset( - offset: token.offset + delta, - length: token.length, - diagnosticCode: diag.unexpectedTransformSetToken, - arguments: [token.kind.displayName], + diagnosticReporter.report( + diag.unexpectedTransformSetToken + .withArguments(tokenKind: token.kind.displayName) + .atOffset(offset: token.offset + delta, length: token.length), ); return null; } @@ -154,20 +155,21 @@ class CodeFragmentParser { offset = last.offset; length = last.length; } - diagnosticReporter.atOffset( - offset: offset + delta, - length: length, - diagnosticCode: diag.missingToken, - arguments: [validKindsDisplayString()], + diagnosticReporter.report( + diag.missingToken + .withArguments(validKinds: validKindsDisplayString()) + .atOffset(offset: offset + delta, length: length), ); return null; } if (!validKinds.contains(token.kind)) { - diagnosticReporter.atOffset( - offset: token.offset + delta, - length: token.length, - diagnosticCode: diag.wrongToken, - arguments: [validKindsDisplayString(), token.kind.displayName], + diagnosticReporter.report( + diag.wrongToken + .withArguments( + validKinds: validKindsDisplayString(), + actualKind: token.kind.displayName, + ) + .atOffset(offset: token.offset + delta, length: token.length), ); return null; } @@ -236,11 +238,10 @@ class CodeFragmentParser { advance(); return TypeArgumentAccessor(argumentIndex); } else { - diagnosticReporter.atOffset( - offset: token.offset + delta, - length: token.length, - diagnosticCode: diag.unknownAccessor, - arguments: [identifier], + diagnosticReporter.report( + diag.unknownAccessor + .withArguments(accessor: identifier) + .atOffset(offset: token.offset + delta, length: token.length), ); return null; } @@ -317,11 +318,10 @@ class CodeFragmentParser { var variableName = token.lexeme; var generator = variableScope.lookup(variableName); if (generator == null) { - diagnosticReporter.atOffset( - offset: token.offset + delta, - length: token.length, - diagnosticCode: diag.undefinedVariable, - arguments: [variableName], + diagnosticReporter.report( + diag.undefinedVariable + .withArguments(key: variableName) + .atOffset(offset: token.offset + delta, length: token.length), ); return null; } @@ -348,10 +348,8 @@ class CodeFragmentParser { offset = token.offset + delta; length = token.length; } - diagnosticReporter.atOffset( - offset: offset, - length: length, - diagnosticCode: diag.expectedPrimary, + diagnosticReporter.report( + diag.expectedPrimary.atOffset(offset: offset, length: length), ); return null; } @@ -475,11 +473,10 @@ class _CodeFragmentScanner { /// Report the presence of an invalid character at the given [offset]. Null _reportInvalidCharacter(int offset) { - _diagnosticReporter.atOffset( - offset: offset + delta, - length: 1, - diagnosticCode: diag.invalidCharacter, - arguments: [content.substring(offset, offset + 1)], + _diagnosticReporter.report( + diag.invalidCharacter + .withArguments(text: content.substring(offset, offset + 1)) + .atOffset(offset: offset + delta, length: 1), ); return null; } diff --git a/pkg/analysis_server/lib/src/services/correction/fix/data_driven/transform_set_parser.dart b/pkg/analysis_server/lib/src/services/correction/fix/data_driven/transform_set_parser.dart index abeab85ca23..dae192f8b9d 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix/data_driven/transform_set_parser.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix/data_driven/transform_set_parser.dart @@ -210,10 +210,11 @@ class TransformSetParser { } var endIndex = template.indexOf(_closeComponent, variableStart + 2); if (endIndex < 0) { - _diagnosticReporter.atOffset( - offset: templateOffset + variableStart, - length: 2, - diagnosticCode: diag.missingTemplateEnd, + _diagnosticReporter.report( + diag.missingTemplateEnd.atOffset( + offset: templateOffset + variableStart, + length: 2, + ), ); // Ignore the invalid component, treating it as if it extended to the // end of the template. @@ -222,11 +223,14 @@ class TransformSetParser { var name = template.substring(variableStart + 2, endIndex).trim(); var generator = variableScope.lookup(name); if (generator == null) { - _diagnosticReporter.atOffset( - offset: templateOffset + template.indexOf(name, variableStart), - length: name.length, - diagnosticCode: diag.undefinedVariable, - arguments: [name], + _diagnosticReporter.report( + diag.undefinedVariable + .withArguments(key: name) + .atOffset( + offset: + templateOffset + template.indexOf(name, variableStart), + length: name.length, + ), ); // Ignore the invalid component. } else { @@ -276,11 +280,10 @@ class TransformSetParser { var span = e.span; var offset = span?.start.offset ?? 0; var length = span?.length ?? 0; - _diagnosticReporter.atOffset( - offset: offset, - length: length, - diagnosticCode: diag.yamlSyntaxError, - arguments: [e.message], + _diagnosticReporter.report( + diag.yamlSyntaxError + .withArguments(message: e.message) + .atOffset(offset: offset, length: length), ); } return null; diff --git a/pkg/analysis_server/messages.yaml b/pkg/analysis_server/messages.yaml index b7c55a6b6bf..11436b36077 100644 --- a/pkg/analysis_server/messages.yaml +++ b/pkg/analysis_server/messages.yaml @@ -41,8 +41,8 @@ TransformSetErrorCode: invalidCharacter: type: compileTimeError parameters: - Object p0: the character that is invalid - problemMessage: "Invalid character '#p0'." + String text: the character that is invalid + problemMessage: "Invalid character '#text'." hasPublishedDocs: false invalidKey: type: compileTimeError @@ -96,8 +96,8 @@ TransformSetErrorCode: missingToken: type: compileTimeError parameters: - Object p0: a description of the expected kinds of tokens - problemMessage: "Expected to find #p0." + String validKinds: a description of the expected kinds of tokens + problemMessage: "Expected to find #validKinds." hasPublishedDocs: false missingUri: type: compileTimeError @@ -107,20 +107,20 @@ TransformSetErrorCode: undefinedVariable: type: compileTimeError parameters: - Object p0: the missing key - problemMessage: "The variable '#p0' isn't defined." + String key: the missing key + problemMessage: "The variable '#key' isn't defined." hasPublishedDocs: false unexpectedTransformSetToken: type: compileTimeError parameters: - Object p0: the token that was unexpectedly found - problemMessage: "Didn't expect to find #p0." + String tokenKind: the token that was unexpectedly found + problemMessage: "Didn't expect to find #tokenKind." hasPublishedDocs: false unknownAccessor: type: compileTimeError parameters: - Object p0: a description of the expected kind of token - problemMessage: "The accessor '#p0' is invalid." + String accessor: a description of the expected kind of token + problemMessage: "The accessor '#accessor' is invalid." hasPublishedDocs: false unsupportedKey: type: compileTimeError @@ -141,13 +141,13 @@ TransformSetErrorCode: wrongToken: type: compileTimeError parameters: - Object p0: a description of the expected kind of token - Object p1: a description of the actual kind of token - problemMessage: "Expected to find #p0, but found #p1." + String validKinds: a description of the expected kind of token + String actualKind: a description of the actual kind of token + problemMessage: "Expected to find #validKinds, but found #actualKind." hasPublishedDocs: false yamlSyntaxError: type: compileTimeError parameters: - Object p0: the message produced by the YAML parser - problemMessage: "Parse error: #p0" + String message: the message produced by the YAML parser + problemMessage: "Parse error: #message" hasPublishedDocs: false