From f4ff72aadd58f826152ad2c6df05b678cca956be Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Thu, 21 May 2026 12:49:15 -0700 Subject: [PATCH] Migrate analyzer_utilities package to new constructor decl syntax. This change migrates the analyzer_utilities package to use the new constructor declaration syntax, described in https://github.com/dart-lang/language/blob/main/accepted/future-releases/primary-constructors/feature-specification.md#abbreviations-of-in-body-constructor-declarations. This change was performed in an automated fashion, by (a) bumping the packages' SDK constraints to `3.13.0-0`, (b) enabling the lints `unnecessary_type_name_in_constructor` and `unnecessary_const_in_enum_constructor`, (c) fixing the resulting lint failures using `dart fix`, and then (d) reformatting the affected files. To ease code review, I've reverted unrelated formatting changes. Since this change requires bumping SDK constaints to `3.13.0-0`, it was only performed on packages that are *not* published on pub. (Packages that *are* published on pub should remain on lower language versions until at least after the stable version of 3.13 is released, so that we don't block users on the stable channel from receiving updates to those packages.) Change-Id: Ib9564fe588b1118f7e810bd39ff9c6576a6a6964 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/505066 Reviewed-by: Konstantin Shcheglov Reviewed-by: Samuel Rawlins Commit-Queue: Paul Berry --- pkg/analyzer_utilities/analysis_options.yaml | 2 + .../lib/analyzer_message_constant_style.dart | 14 ++----- .../lib/analyzer_messages.dart | 18 ++++----- pkg/analyzer_utilities/lib/html_dom.dart | 6 +-- pkg/analyzer_utilities/lib/lint_messages.dart | 29 +++++++-------- pkg/analyzer_utilities/lib/located_error.dart | 2 +- pkg/analyzer_utilities/lib/messages.dart | 37 +++++++++---------- .../lib/testing/map_entry_matcher.dart | 2 +- .../lib/testing/tree_string_sink.dart | 2 +- .../lib/text_formatter.dart | 2 +- pkg/analyzer_utilities/lib/tools.dart | 6 +-- pkg/analyzer_utilities/lib/verify_tests.dart | 2 +- pkg/analyzer_utilities/pubspec.yaml | 2 +- .../use_literate_api_in_analyzer.dart | 2 +- .../tool/messages/use_new_with_arguments.dart | 2 +- 15 files changed, 57 insertions(+), 71 deletions(-) diff --git a/pkg/analyzer_utilities/analysis_options.yaml b/pkg/analyzer_utilities/analysis_options.yaml index 679aa07be87..17eceab058b 100644 --- a/pkg/analyzer_utilities/analysis_options.yaml +++ b/pkg/analyzer_utilities/analysis_options.yaml @@ -18,8 +18,10 @@ linter: - prefer_single_quotes - unawaited_futures - unnecessary_breaks + - unnecessary_const_in_enum_constructor - unnecessary_ignore - unnecessary_library_directive - unnecessary_parenthesis + - unnecessary_type_name_in_constructor - unreachable_from_main - use_null_aware_elements diff --git a/pkg/analyzer_utilities/lib/analyzer_message_constant_style.dart b/pkg/analyzer_utilities/lib/analyzer_message_constant_style.dart index 960d8a2f79a..4aaa78269d0 100644 --- a/pkg/analyzer_utilities/lib/analyzer_message_constant_style.dart +++ b/pkg/analyzer_utilities/lib/analyzer_message_constant_style.dart @@ -13,7 +13,7 @@ sealed class ConstantStyle { /// The static type of the constant that should be generated. final String staticType; - ConstantStyle({required this.concreteClassName, required this.staticType}); + new({required this.concreteClassName, required this.staticType}); } /// [ConstantStyle] object indicating that an "old style" constant should be @@ -22,10 +22,7 @@ sealed class ConstantStyle { // TODO(paulberry): finish supporting the literate API in all analyzer messages // and eliminate this. class OldConstantStyle extends ConstantStyle { - OldConstantStyle({ - required super.concreteClassName, - required super.staticType, - }); + new({required super.concreteClassName, required super.staticType}); } /// [ConstantStyle] object indicating that a constant should be generated that @@ -34,7 +31,7 @@ class WithArgumentsConstantStyle extends ConstantStyle { /// The parameters that should be accepted by the `.withArguments` getter. final String withArgumentsParams; - WithArgumentsConstantStyle({ + new({ required super.concreteClassName, required super.staticType, required this.withArgumentsParams, @@ -44,8 +41,5 @@ class WithArgumentsConstantStyle extends ConstantStyle { /// [ConstantStyle] object indicating that a constant should be generated that /// doesn't require any arguments. class WithoutArgumentsConstantStyle extends ConstantStyle { - WithoutArgumentsConstantStyle({ - required super.concreteClassName, - required super.staticType, - }); + new({required super.concreteClassName, required super.staticType}); } diff --git a/pkg/analyzer_utilities/lib/analyzer_messages.dart b/pkg/analyzer_utilities/lib/analyzer_messages.dart index 1814d72f32f..0fd52a089de 100644 --- a/pkg/analyzer_utilities/lib/analyzer_messages.dart +++ b/pkg/analyzer_utilities/lib/analyzer_messages.dart @@ -282,7 +282,7 @@ List _splitText( class AliasMessage extends AnalyzerMessage { String aliasFor; - AliasMessage( + new( super.messageYaml, { required this.aliasFor, required super.analyzerCode, @@ -351,7 +351,7 @@ enum AnalyzerDiagnosticPackage { /// the `prefer_single_quotes` lint. final bool shouldIgnorePreferSingleQuotes; - const AnalyzerDiagnosticPackage({ + new({ required this.diagnosticPathPart, required this.dirName, required this.permittedTypes, @@ -403,7 +403,7 @@ enum AnalyzerDiagnosticType { /// Base classes used for messages of this type. final DiagnosticBaseClasses baseClasses; - const AnalyzerDiagnosticType({this.baseClasses = analyzerBaseClasses}); + new({this.baseClasses = analyzerBaseClasses}); /// The representation of this type in analyzer source code. String get code => 'DiagnosticType.${name.toSnakeCase().toUpperCase()}'; @@ -426,7 +426,7 @@ class AnalyzerMessage extends Message with MessageWithAnalyzerCode { @override final AnalyzerDiagnosticType type; - factory AnalyzerMessage( + factory( MessageYaml messageYaml, { required DiagnosticCodeName analyzerCode, required AnalyzerDiagnosticPackage package, @@ -447,7 +447,7 @@ class AnalyzerMessage extends Message with MessageWithAnalyzerCode { } } - AnalyzerMessage.internal( + new internal( MessageYaml messageYaml, { required this.analyzerCode, required this.package, @@ -487,7 +487,7 @@ class DiagnosticBaseClasses { /// require arguments. final String withoutArgumentsImplClass; - const DiagnosticBaseClasses({ + const new({ required this.requiresTypeArgument, required this.withArgumentsClass, required this.withExpectedTypesClass, @@ -525,11 +525,7 @@ class DiagnosticClassInfo { /// If no documentation comment is needed, this should be the empty string. final String comment; - const DiagnosticClassInfo({ - required this.name, - required this.type, - this.comment = '', - }); + const new({required this.name, required this.type, this.comment = ''}); static DiagnosticClassInfo byName(String name) => _diagnosticClassesByName[name] ?? diff --git a/pkg/analyzer_utilities/lib/html_dom.dart b/pkg/analyzer_utilities/lib/html_dom.dart index 07d2ee90127..e174dbe78c2 100644 --- a/pkg/analyzer_utilities/lib/html_dom.dart +++ b/pkg/analyzer_utilities/lib/html_dom.dart @@ -12,7 +12,7 @@ const _htmlEscape = HtmlEscape(HtmlEscapeMode.element); class Document extends Element { static const Set selfClosing = {'br', 'link', 'meta'}; - Document() : super.tag(''); + new() : super.tag(''); /// Return the full HTML text for the document. String get outerHtml { @@ -64,7 +64,7 @@ class Element extends Node { Map attributes = {}; - Element.tag(this.name); + new tag(this.name); List get children => nodes.whereType().toList(); @@ -94,7 +94,7 @@ class Text extends Node { final String text; - Text(this.text); + new(this.text); @override List get nodes => const []; diff --git a/pkg/analyzer_utilities/lib/lint_messages.dart b/pkg/analyzer_utilities/lib/lint_messages.dart index 9e8524948eb..aded2238a28 100644 --- a/pkg/analyzer_utilities/lib/lint_messages.dart +++ b/pkg/analyzer_utilities/lib/lint_messages.dart @@ -45,22 +45,19 @@ class LintMessage extends AnalyzerMessage { final Map? state; - LintMessage( - super.messageYaml, { - required super.analyzerCode, - required super.package, - }) : categories = messageYaml.get( - 'categories', - decode: decodeCategories, - ifAbsent: () => null, - ), - deprecatedDetails = messageYaml.getOptionalString('deprecatedDetails'), - state = messageYaml.get( - 'state', - decode: decodeState, - ifAbsent: () => null, - ), - super.internal(); + new(super.messageYaml, {required super.analyzerCode, required super.package}) + : categories = messageYaml.get( + 'categories', + decode: decodeCategories, + ifAbsent: () => null, + ), + deprecatedDetails = messageYaml.getOptionalString('deprecatedDetails'), + state = messageYaml.get( + 'state', + decode: decodeState, + ifAbsent: () => null, + ), + super.internal(); static Set decodeCategories(YamlNode node) { if (node is! YamlList) throw 'Must be a list'; diff --git a/pkg/analyzer_utilities/lib/located_error.dart b/pkg/analyzer_utilities/lib/located_error.dart index 8c3dc56c10d..cfe531f8d34 100644 --- a/pkg/analyzer_utilities/lib/located_error.dart +++ b/pkg/analyzer_utilities/lib/located_error.dart @@ -9,7 +9,7 @@ class LocatedError { final SourceSpan span; final String message; - LocatedError(this.message, {required this.span}); + new(this.message, {required this.span}); @override String toString() => '${span.location}: $message'; diff --git a/pkg/analyzer_utilities/lib/messages.dart b/pkg/analyzer_utilities/lib/messages.dart index ef8039f8118..6a64fa5b988 100644 --- a/pkg/analyzer_utilities/lib/messages.dart +++ b/pkg/analyzer_utilities/lib/messages.dart @@ -155,7 +155,7 @@ abstract class CfeStyleMessage extends Message { /// `messages.yaml`. final DiagnosticCodeName frontEndCode; - CfeStyleMessage(MessageYaml messageYaml) + new(MessageYaml messageYaml) : cfeSeverity = messageYaml.get( 'severity', decode: (node) { @@ -244,7 +244,7 @@ class DiagnosticCodeName implements Comparable { /// capitalized words, with no separation between words). final String camelCaseName; - DiagnosticCodeName.fromCamelCase(this.camelCaseName) + new fromCamelCase(this.camelCaseName) : snakeCaseName = _snakeCaseExceptions[camelCaseName] ?? camelCaseName.toSnakeCase() { if (snakeCaseName.toLowerCase() != snakeCaseName) { @@ -288,7 +288,7 @@ class DiagnosticParameter { final String comment; final int index; - DiagnosticParameter({ + new({ required this.name, required this.type, required this.comment, @@ -399,7 +399,7 @@ enum DiagnosticParameterType { /// sufficient. final Conversion? cfeConversion; - const DiagnosticParameterType({ + new({ required this.messagesYamlName, this._analyzerName, this.cfeName, @@ -407,7 +407,7 @@ enum DiagnosticParameterType { }); /// Decodes a type name from `messages.yaml` into a [DiagnosticParameterType]. - factory DiagnosticParameterType.fromMessagesYamlName(String name) => + factory fromMessagesYamlName(String name) => _messagesYamlNameToValue[name] ?? (throw 'Unknown type name: $name'); String get analyzerName => @@ -444,7 +444,7 @@ class DiagnosticTables { /// Map from [DiagnosticCodeName.pascalCaseName] to front end diagnostic. final Map frontEndDiagnosticsByPascalCaseName = {}; - DiagnosticTables._(List messages) { + new _(List messages) { var frontEndCodeDuplicateChecker = _DuplicateChecker( kind: 'Front end code', ); @@ -538,7 +538,7 @@ class FrontEndMessage extends CfeStyleMessage { // codes. final String? pseudoSharedCode; - FrontEndMessage(super.messageYaml) + new(super.messageYaml) : pseudoSharedCode = messageYaml.getOptionalString('pseudoSharedCode'); } @@ -547,7 +547,7 @@ class LabelerConversion implements Conversion { /// The name of the [TypeLabeler] method to call. final String methodName; - const LabelerConversion(this.methodName); + const new(this.methodName); @override int get hashCode => Object.hash(runtimeType, methodName.hashCode); @@ -623,7 +623,7 @@ abstract class Message { final String keyString; /// Decodes a [Message] object from its YAML representation. - Message(MessageYaml messageYaml, {bool requireProblemMessage = false}) + new(MessageYaml messageYaml, {bool requireProblemMessage = false}) : comment = messageYaml.getOptionalString('comment'), correctionMessage = messageYaml.getMessageTemplate( 'correctionMessage', @@ -691,7 +691,7 @@ class MessageYaml { /// this set, an exception will be thrown to report them as unexpected keys. final Set _permittedKeys = {}; - MessageYaml._(this._key, this._map); + new _(this._key, this._map); /// The span of the YAML key node from the key/value pair that defines the /// message. @@ -894,7 +894,7 @@ class NumericConversion implements Conversion { /// used. final bool padWithZeros; - NumericConversion({ + new({ required this.fractionDigits, required this.padWidth, required this.padWithZeros, @@ -974,7 +974,7 @@ class SharedMessage extends CfeStyleMessage with MessageWithAnalyzerCode { @override final AnalyzerDiagnosticType type; - SharedMessage(super.messageYaml) + new(super.messageYaml) : analyzerCode = messageYaml.get( 'analyzerCode', decode: _decodeAnalyzerCode, @@ -1006,7 +1006,7 @@ class SimpleConversion implements Conversion { /// The name of the function to be invoked. final String functionName; - const SimpleConversion(this.functionName); + const new(this.functionName); @override int get hashCode => Object.hash(runtimeType, functionName.hashCode); @@ -1028,7 +1028,7 @@ class TemplateLiteralPart implements TemplatePart { /// The literal text. final String text; - TemplateLiteralPart(this.text); + new(this.text); } /// [TemplatePart] representing a parameter to be substituted into the @@ -1044,7 +1044,7 @@ class TemplateParameterPart implements TemplatePart { /// Builds a [TemplateParameterPart] from the given [match] of /// [placeholderPattern]. - factory TemplateParameterPart.fromMatch( + factory fromMatch( Match match, { required Map parameters, }) { @@ -1060,10 +1060,7 @@ class TemplateParameterPart implements TemplatePart { ); } - TemplateParameterPart._({ - required this.parameter, - required this.conversionOverride, - }); + new _({required this.parameter, required this.conversionOverride}); @override int get hashCode => Object.hash(parameter, conversionOverride); @@ -1085,7 +1082,7 @@ class _DuplicateChecker { final Map> _codeToMessages = {}; final String kind; - _DuplicateChecker({required this.kind}); + new({required this.kind}); void operator []=(Code code, Message message) { (_codeToMessages[code] ??= []).add(message); diff --git a/pkg/analyzer_utilities/lib/testing/map_entry_matcher.dart b/pkg/analyzer_utilities/lib/testing/map_entry_matcher.dart index eee644674cd..33e904bd969 100644 --- a/pkg/analyzer_utilities/lib/testing/map_entry_matcher.dart +++ b/pkg/analyzer_utilities/lib/testing/map_entry_matcher.dart @@ -12,7 +12,7 @@ class MapEntryMatcher extends Matcher { final Matcher keyMatcher; final Matcher valueMatcher; - MapEntryMatcher(Object? key, Object? value) + new(Object? key, Object? value) : keyMatcher = wrapMatcher(key), valueMatcher = wrapMatcher(value); diff --git a/pkg/analyzer_utilities/lib/testing/tree_string_sink.dart b/pkg/analyzer_utilities/lib/testing/tree_string_sink.dart index 21264b1b439..c61c35757de 100644 --- a/pkg/analyzer_utilities/lib/testing/tree_string_sink.dart +++ b/pkg/analyzer_utilities/lib/testing/tree_string_sink.dart @@ -9,7 +9,7 @@ class TreeStringSink { final StringSink _sink; String _indent = ''; - TreeStringSink({required this._sink, required this._indent}); + new({required this._sink, required this._indent}); void withIndent(void Function() f) { var indent = _indent; diff --git a/pkg/analyzer_utilities/lib/text_formatter.dart b/pkg/analyzer_utilities/lib/text_formatter.dart index 7dadbdf7d42..7813ab1d6fd 100644 --- a/pkg/analyzer_utilities/lib/text_formatter.dart +++ b/pkg/analyzer_utilities/lib/text_formatter.dart @@ -53,7 +53,7 @@ class _TextFormatter with CodeGenerator { /// True if the output should be Javadoc compatible. final bool javadocStyle; - _TextFormatter(this.width, this.javadocStyle); + new(this.width, this.javadocStyle); /// Process an HTML node. void add(dom.Node node) { diff --git a/pkg/analyzer_utilities/lib/tools.dart b/pkg/analyzer_utilities/lib/tools.dart index 62ff4ff2974..81cb13d903c 100644 --- a/pkg/analyzer_utilities/lib/tools.dart +++ b/pkg/analyzer_utilities/lib/tools.dart @@ -232,7 +232,7 @@ class CodeGeneratorSettings { /// String used for indenting code. String indent; - CodeGeneratorSettings({ + new({ this.languageName = 'java', this.lineCommentLineLeader = '// ', this.docCommentStartMarker = '/**', @@ -322,7 +322,7 @@ class GeneratedDirectory extends GeneratedContent { /// Callback function that computes the directory contents. final DirectoryContentsComputer directoryContentsComputer; - GeneratedDirectory(this.outputDirPath, this.directoryContentsComputer); + new(this.outputDirPath, this.directoryContentsComputer); @override Future generate(String pkgRoot) async { @@ -366,7 +366,7 @@ class GeneratedFile extends GeneratedContent { /// Callback function which computes the file. final FileContentsComputer computeContents; - GeneratedFile(this.outputPath, this.computeContents); + new(this.outputPath, this.computeContents); bool get isDartFile => outputPath.endsWith('.dart'); diff --git a/pkg/analyzer_utilities/lib/verify_tests.dart b/pkg/analyzer_utilities/lib/verify_tests.dart index e02aca80c8d..79ba5695795 100644 --- a/pkg/analyzer_utilities/lib/verify_tests.dart +++ b/pkg/analyzer_utilities/lib/verify_tests.dart @@ -22,7 +22,7 @@ class VerifyTests { /// Paths to exclude from analysis completely. final List? excludedPaths; - VerifyTests(this.testDirPath, {this.excludedPaths}); + new(this.testDirPath, {this.excludedPaths}); /// Build tests. void build({bool Function(AnalysisContext)? analysisContextPredicate}) { diff --git a/pkg/analyzer_utilities/pubspec.yaml b/pkg/analyzer_utilities/pubspec.yaml index 9b38e11eef8..3cde5cef71e 100644 --- a/pkg/analyzer_utilities/pubspec.yaml +++ b/pkg/analyzer_utilities/pubspec.yaml @@ -3,7 +3,7 @@ name: analyzer_utilities publish_to: none environment: - sdk: '^3.12.0-0' + sdk: '^3.13.0-0' resolution: workspace diff --git a/pkg/analyzer_utilities/tool/messages/use_literate_api_in_analyzer.dart b/pkg/analyzer_utilities/tool/messages/use_literate_api_in_analyzer.dart index 3446de729af..c8cabd4b51d 100644 --- a/pkg/analyzer_utilities/tool/messages/use_literate_api_in_analyzer.dart +++ b/pkg/analyzer_utilities/tool/messages/use_literate_api_in_analyzer.dart @@ -124,7 +124,7 @@ class _Visitor extends RecursiveAstVisitor { final List changes = []; - _Visitor({ + new({ required this.rejectStats, required this.fileContents, required this.path, diff --git a/pkg/analyzer_utilities/tool/messages/use_new_with_arguments.dart b/pkg/analyzer_utilities/tool/messages/use_new_with_arguments.dart index a2d4052e0a0..7465202b4b4 100644 --- a/pkg/analyzer_utilities/tool/messages/use_new_with_arguments.dart +++ b/pkg/analyzer_utilities/tool/messages/use_new_with_arguments.dart @@ -113,7 +113,7 @@ class _Visitor extends RecursiveAstVisitor { final List changes = []; - _Visitor({ + new({ required this.rejectStats, required this.fileContents, required this.path,