From 876752e40f3d37dded07fd4cf51ec06eb3b332cb Mon Sep 17 00:00:00 2001 From: pq Date: Tue, 7 Jan 2025 13:18:28 -0800 Subject: [PATCH] [CQ] add feature `isEnabled` utility Change-Id: I267aee8da50ec073a28bdb22ebe5379d31b1dc91 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403265 Reviewed-by: Brian Wilkerson Auto-Submit: Phil Quitslund Commit-Queue: Brian Wilkerson --- .../services/correction/dart/add_key_to_constructors.dart | 6 ++---- .../src/services/correction/dart/add_super_parameter.dart | 2 +- .../src/services/correction/dart/convert_class_to_enum.dart | 2 +- .../correction/dart/convert_to_if_case_statement.dart | 2 +- .../correction/dart/convert_to_super_parameters.dart | 2 +- .../correction/dart/convert_to_switch_statement.dart | 2 +- .../services/correction/dart/create_constructor_super.dart | 4 ++-- .../dart/move_annotation_to_library_directive.dart | 2 +- .../dart/move_doc_comment_to_library_directive.dart | 2 +- .../lib/edit/dart/correction_producer.dart | 4 ++++ 10 files changed, 15 insertions(+), 13 deletions(-) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_key_to_constructors.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_key_to_constructors.dart index 89c4f45824e..ca99309b1d2 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/add_key_to_constructors.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/add_key_to_constructors.dart @@ -95,7 +95,7 @@ class AddKeyToConstructors extends ResolvedCorrectionProducer { } builder.write(className); builder.write('({'); - if (libraryElement2.featureSet.isEnabled(Feature.super_parameters)) { + if (isEnabled(Feature.super_parameters)) { builder.write('super.key});'); } else { builder.writeType(keyType); @@ -115,9 +115,7 @@ class AddKeyToConstructors extends ResolvedCorrectionProducer { if (keyType == null) { return; } - var superParameters = libraryElement2.featureSet.isEnabled( - Feature.super_parameters, - ); + var superParameters = isEnabled(Feature.super_parameters); void writeKey(DartEditBuilder builder) { if (superParameters) { diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_super_parameter.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_super_parameter.dart index d419722535f..d0b070c2951 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/add_super_parameter.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/add_super_parameter.dart @@ -33,7 +33,7 @@ class AddSuperParameter extends ResolvedCorrectionProducer { @override Future compute(ChangeBuilder builder) async { - if (!libraryElement2.featureSet.isEnabled(Feature.super_parameters)) { + if (!isEnabled(Feature.super_parameters)) { return; } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_enum.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_enum.dart index 21ffa9d75c1..07acab52a5f 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_enum.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_enum.dart @@ -39,7 +39,7 @@ class ConvertClassToEnum extends ResolvedCorrectionProducer { @override Future compute(ChangeBuilder builder) async { - if (!libraryElement2.featureSet.isEnabled(Feature.enhanced_enums)) { + if (!isEnabled(Feature.enhanced_enums)) { // If the library doesn't support enhanced_enums then the class can't be // converted. return; diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_if_case_statement.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_if_case_statement.dart index 8033b565826..7084af2c8c6 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_if_case_statement.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_if_case_statement.dart @@ -27,7 +27,7 @@ class ConvertToIfCaseStatement extends ResolvedCorrectionProducer { @override Future compute(ChangeBuilder builder) async { - if (!libraryElement2.featureSet.isEnabled(Feature.patterns)) { + if (!isEnabled(Feature.patterns)) { return; } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_super_parameters.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_super_parameters.dart index 758a84a5121..435598c9974 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_super_parameters.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_super_parameters.dart @@ -36,7 +36,7 @@ class ConvertToSuperParameters extends ResolvedCorrectionProducer { @override Future compute(ChangeBuilder builder) async { - if (!libraryElement2.featureSet.isEnabled(Feature.super_parameters)) { + if (!isEnabled(Feature.super_parameters)) { // If the library doesn't support super_parameters then the change isn't // appropriate. return; diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_switch_statement.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_switch_statement.dart index 4e0b452f3fb..dd6c505f0aa 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_switch_statement.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_switch_statement.dart @@ -29,7 +29,7 @@ class ConvertIfStatementToSwitchStatement extends ResolvedCorrectionProducer { @override Future compute(ChangeBuilder builder) async { - if (!libraryElement2.featureSet.isEnabled(Feature.patterns)) { + if (!isEnabled(Feature.patterns)) { return; } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_super.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_super.dart index bce03bc5bb8..b4d24586a81 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_super.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/create_constructor_super.dart @@ -67,7 +67,7 @@ class _CreateConstructor extends ResolvedCorrectionProducer { var buffer = StringBuffer(); buffer.write('super'); var constructorName = _constructor.name3; - if (libraryElement2.featureSet.isEnabled(Feature.super_parameters)) { + if (isEnabled(Feature.super_parameters)) { if (constructorName != null && constructorName != 'new') { buffer.write('.'); buffer.write(constructorName); @@ -90,7 +90,7 @@ class _CreateConstructor extends ResolvedCorrectionProducer { @override Future compute(ChangeBuilder builder) async { - if (libraryElement2.featureSet.isEnabled(Feature.super_parameters)) { + if (isEnabled(Feature.super_parameters)) { await _computeWithSuperParameters(builder); } else { await _computeWithoutSuperParameters(builder); diff --git a/pkg/analysis_server/lib/src/services/correction/dart/move_annotation_to_library_directive.dart b/pkg/analysis_server/lib/src/services/correction/dart/move_annotation_to_library_directive.dart index 005896f0ff9..a2791c84976 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/move_annotation_to_library_directive.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/move_annotation_to_library_directive.dart @@ -47,7 +47,7 @@ class MoveAnnotationToLibraryDirective extends ResolvedCorrectionProducer { return; } - if (!libraryElement2.featureSet.isEnabled(Feature.unnamedLibraries)) { + if (!isEnabled(Feature.unnamedLibraries)) { // If the library doesn't support unnamed libraries, then we cannot add // a new library directive; we don't know what to name it. return; diff --git a/pkg/analysis_server/lib/src/services/correction/dart/move_doc_comment_to_library_directive.dart b/pkg/analysis_server/lib/src/services/correction/dart/move_doc_comment_to_library_directive.dart index 13b62a80ed6..819f6c840d2 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/move_doc_comment_to_library_directive.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/move_doc_comment_to_library_directive.dart @@ -37,7 +37,7 @@ class MoveDocCommentToLibraryDirective extends ResolvedCorrectionProducer { var firstDirective = compilationUnit.directives.firstOrNull; if (firstDirective is LibraryDirective) { await _moveToExistingLibraryDirective(builder, comment, firstDirective); - } else if (libraryElement2.featureSet.isEnabled(Feature.unnamedLibraries)) { + } else if (isEnabled(Feature.unnamedLibraries)) { await _moveToNewLibraryDirective(builder, comment, compilationUnit); } diff --git a/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart b/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart index 268865b08cd..db890bf5d14 100644 --- a/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart +++ b/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart @@ -10,6 +10,7 @@ import 'package:analysis_server_plugin/edit/fix/dart_fix_context.dart'; import 'package:analysis_server_plugin/src/utilities/selection.dart'; import 'package:analyzer/dart/analysis/analysis_options.dart'; import 'package:analyzer/dart/analysis/code_style_options.dart'; +import 'package:analyzer/dart/analysis/features.dart'; import 'package:analyzer/dart/analysis/results.dart'; import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/dart/element/nullability_suffix.dart'; @@ -613,6 +614,9 @@ abstract class ResolvedCorrectionProducer // We don't know. return null; } + + bool isEnabled(Feature feature) => + libraryElement2.featureSet.isEnabled(feature); } final class StubCorrectionProducerContext implements CorrectionProducerContext {