[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 <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
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 0000000..be0ff14
--- /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<void> 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 dfde998..00fc90a 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.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 74cce40..df9a57f 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 @@
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 8786dde..0b9cd4f 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/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 @@
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 0000000..e75dbbe
--- /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<void> 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<void> 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 71a3037..2760dbe 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 '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 @@
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 93ccda1..527d2f5 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 @@
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 {