[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>
This commit is contained in:
Ahmed Ashour
2022-07-21 22:16:06 +00:00
committed by Commit Bot
parent a9ba31a5b2
commit eefbe1fd14
7 changed files with 104 additions and 21 deletions
@@ -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));
});
}
}
@@ -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: |-
@@ -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,
@@ -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,
@@ -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();
}
''');
}
}
@@ -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();
@@ -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 {