From eefbe1fd148dc7f306aeb5944391fe024092d085 Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Thu, 21 Jul 2022 22:16:06 +0000 Subject: [PATCH] [analysis_server] add fix for `ASSERT_IN_REDIRECTING_CONSTRUCTOR` Fixes #49486 Change-Id: Ic96d2b347578f574eb67b12ef3dbbddcd9c04d2e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252080 Reviewed-by: Brian Wilkerson Commit-Queue: Brian Wilkerson --- .../correction/dart/remove_assertion.dart | 25 +++++++++ .../services/correction/error_fix_status.yaml | 4 +- .../lib/src/services/correction/fix.dart | 5 ++ .../src/services/correction/fix_internal.dart | 4 ++ .../correction/fix/remove_assertion_test.dart | 53 +++++++++++++++++++ .../src/services/correction/fix/test_all.dart | 2 + ...ssert_in_redirecting_constructor_test.dart | 32 +++++------ 7 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 pkg/analysis_server/lib/src/services/correction/dart/remove_assertion.dart create mode 100644 pkg/analysis_server/test/src/services/correction/fix/remove_assertion_test.dart diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_assertion.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_assertion.dart new file mode 100644 index 00000000000..be0ff14edb0 --- /dev/null +++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_assertion.dart @@ -0,0 +1,25 @@ +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// 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. + +import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart'; +import 'package:analysis_server/src/services/correction/fix.dart'; +import 'package:analyzer/dart/ast/ast.dart'; +import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; +import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; +import 'package:analyzer_plugin/utilities/range_factory.dart'; + +class RemoveAssertion extends CorrectionProducer { + @override + FixKind get fixKind => DartFixKind.REMOVE_ASSERTION; + + @override + Future compute(ChangeBuilder builder) async { + var parent = node.parent; + if (parent is! ConstructorDeclaration) return; + + await builder.addDartFileEdit(file, (builder) { + builder.addDeletion(range.nodeInList(parent.initializers, node)); + }); + } +} diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml index dfde9988d76..00fc90a7d0d 100644 --- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml +++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml @@ -125,9 +125,7 @@ CompileTimeErrorCode.AMBIGUOUS_SET_OR_MAP_LITERAL_EITHER: CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE: status: hasFix CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR: - status: needsFix - notes: |- - Remove the assert. + status: hasFix CompileTimeErrorCode.ASSIGNMENT_TO_CONST: status: noFix notes: |- diff --git a/pkg/analysis_server/lib/src/services/correction/fix.dart b/pkg/analysis_server/lib/src/services/correction/fix.dart index 74cce405a55..df9a57fe705 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix.dart @@ -864,6 +864,11 @@ class DartFixKind { DartFixKindPriority.IN_FILE, 'Remove arguments in file', ); + static const REMOVE_ASSERTION = FixKind( + 'dart.fix.remove.assertion', + DartFixKindPriority.DEFAULT, + 'Remove the assertion', + ); static const REMOVE_ASSIGNMENT = FixKind( 'dart.fix.remove.assignment', DartFixKindPriority.DEFAULT, diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart index 8786ddee8e3..0b9cd4fee51 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart @@ -109,6 +109,7 @@ import 'package:analysis_server/src/services/correction/dart/qualify_reference.d import 'package:analysis_server/src/services/correction/dart/remove_abstract.dart'; import 'package:analysis_server/src/services/correction/dart/remove_annotation.dart'; import 'package:analysis_server/src/services/correction/dart/remove_argument.dart'; +import 'package:analysis_server/src/services/correction/dart/remove_assertion.dart'; import 'package:analysis_server/src/services/correction/dart/remove_assignment.dart'; import 'package:analysis_server/src/services/correction/dart/remove_await.dart'; import 'package:analysis_server/src/services/correction/dart/remove_comparison.dart'; @@ -872,6 +873,9 @@ class FixProcessor extends BaseProcessor { RemoveAbstract.new, RemoveInitializer.new, ], + CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR: [ + RemoveAssertion.new, + ], CompileTimeErrorCode.ASSIGNMENT_TO_FINAL: [ MakeFieldNotFinal.new, AddLate.new, diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_assertion_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_assertion_test.dart new file mode 100644 index 00000000000..e75dbbe5ba6 --- /dev/null +++ b/pkg/analysis_server/test/src/services/correction/fix/remove_assertion_test.dart @@ -0,0 +1,53 @@ +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// 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. + +import 'package:analysis_server/src/services/correction/fix.dart'; +import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import 'fix_processor.dart'; + +void main() { + defineReflectiveSuite(() { + defineReflectiveTests(RemoveAssertionTest); + }); +} + +@reflectiveTest +class RemoveAssertionTest extends FixProcessorTest { + @override + FixKind get kind => DartFixKind.REMOVE_ASSERTION; + + Future test_class() async { + await resolveTestCode(''' +class A { + A(int x) : assert(x > 0), this.name(); + A.name() {} +} +'''); + await assertHasFix(''' +class A { + A(int x) : this.name(); + A.name() {} +} +'''); + } + + Future test_enum() async { + await resolveTestCode(''' +enum E { + v(42); + const E(int x) : this.name(), assert(x > 0); + const E.name(); +} +'''); + await assertHasFix(''' +enum E { + v(42); + const E(int x) : this.name(); + const E.name(); +} +'''); + } +} diff --git a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart index 71a3037ebc2..2760dbeb501 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart @@ -135,6 +135,7 @@ import 'qualify_reference_test.dart' as qualify_reference; import 'remove_abstract_test.dart' as remove_abstract; import 'remove_annotation_test.dart' as remove_annotation; import 'remove_argument_test.dart' as remove_argument; +import 'remove_assertion_test.dart' as remove_assertion; import 'remove_assignment_test.dart' as remove_assignment; import 'remove_await_test.dart' as remove_await; import 'remove_comparison_test.dart' as remove_comparison; @@ -356,6 +357,7 @@ void main() { remove_abstract.main(); remove_annotation.main(); remove_argument.main(); + remove_assertion.main(); remove_assignment.main(); remove_await.main(); remove_comparison.main(); diff --git a/pkg/analyzer/test/src/diagnostics/assert_in_redirecting_constructor_test.dart b/pkg/analyzer/test/src/diagnostics/assert_in_redirecting_constructor_test.dart index 93ccda13e0d..527d2f523e7 100644 --- a/pkg/analyzer/test/src/diagnostics/assert_in_redirecting_constructor_test.dart +++ b/pkg/analyzer/test/src/diagnostics/assert_in_redirecting_constructor_test.dart @@ -17,42 +17,38 @@ main() { class AssertInRedirectingConstructorTest extends PubPackageResolutionTest { test_class_assertBeforeRedirection() async { await assertErrorsInCode(r''' -class A {} -class B { - B(int x) : assert(x > 0), this.name(); - B.name() {} +class A { + A(int x) : assert(x > 0), this.name(); + A.name() {} } -''', [error(CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR, 34, 13)]); +''', [error(CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR, 23, 13)]); } test_class_justAssert() async { await assertNoErrorsInCode(r''' -class A {} -class B { - B(int x) : assert(x > 0); - B.name() {} +class A { + A(int x) : assert(x > 0); + A.name() {} } '''); } test_class_justRedirection() async { await assertNoErrorsInCode(r''' -class A {} -class B { - B(int x) : this.name(); - B.name() {} +class A { + A(int x) : this.name(); + A.name() {} } '''); } test_class_redirectionBeforeAssert() async { await assertErrorsInCode(r''' -class A {} -class B { - B(int x) : this.name(), assert(x > 0); - B.name() {} +class A { + A(int x) : this.name(), assert(x > 0); + A.name() {} } -''', [error(CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR, 47, 13)]); +''', [error(CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR, 36, 13)]); } test_enum_assertBeforeRedirection() async {