Add a fix for invalid_inside_unary_pattern

Closes 51915

Change-Id: I378f66d70c083dd5852a9c8a04e3ddf59aff66d7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292981
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Brian Wilkerson 2023-04-03 20:56:10 +00:00 committed by Commit Queue
parent 70bbee75de
commit aa1f415e4f
6 changed files with 117 additions and 1 deletions

View file

@ -0,0 +1,27 @@
// Copyright (c) 2023, 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_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
class SurroundWithParentheses extends CorrectionProducer {
@override
bool get canBeAppliedInBulk => false;
@override
bool get canBeAppliedToFile => false;
@override
FixKind get fixKind => DartFixKind.SURROUND_WITH_PARENTHESES;
@override
Future<void> compute(ChangeBuilder builder) async {
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleInsertion(node.offset, '(');
builder.addSimpleInsertion(node.end, ')');
});
}
}

View file

@ -2500,7 +2500,7 @@ ParserErrorCode.INVALID_HEX_ESCAPE:
ParserErrorCode.INVALID_INITIALIZER:
status: needsEvaluation
ParserErrorCode.INVALID_INSIDE_UNARY_PATTERN:
status: needsEvaluation
status: hasFix
ParserErrorCode.INVALID_LITERAL_IN_CONFIGURATION:
status: needsEvaluation
ParserErrorCode.INVALID_OPERATOR:

View file

@ -1760,6 +1760,11 @@ class DartFixKind {
DartFixKindPriority.DEFAULT,
'Move all unnamed constructors before named constructors',
);
static const SURROUND_WITH_PARENTHESES = FixKind(
'dart.fix.surround.parentheses',
DartFixKindPriority.DEFAULT,
'Surround with parentheses',
);
static const UPDATE_SDK_CONSTRAINTS = FixKind(
'dart.fix.updateSdkConstraints',
DartFixKindPriority.DEFAULT,

View file

@ -207,6 +207,7 @@ import 'package:analysis_server/src/services/correction/dart/sort_child_property
import 'package:analysis_server/src/services/correction/dart/sort_combinators.dart';
import 'package:analysis_server/src/services/correction/dart/sort_constructor_first.dart';
import 'package:analysis_server/src/services/correction/dart/sort_unnamed_constructor_first.dart';
import 'package:analysis_server/src/services/correction/dart/surround_with_parentheses.dart';
import 'package:analysis_server/src/services/correction/dart/update_sdk_constraints.dart';
import 'package:analysis_server/src/services/correction/dart/use_curly_braces.dart';
import 'package:analysis_server/src/services/correction/dart/use_effective_integer_division.dart';
@ -1414,6 +1415,9 @@ class FixProcessor extends BaseProcessor {
ParserErrorCode.INVALID_CONSTANT_PATTERN_NEGATION: [
AddConst.new,
],
ParserErrorCode.INVALID_INSIDE_UNARY_PATTERN: [
SurroundWithParentheses.new,
],
ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE: [
AddTypeAnnotation.new,
],

View file

@ -0,0 +1,78 @@
// Copyright (c) 2023, 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(SurroundWithParenthesesTest);
});
}
@reflectiveTest
class SurroundWithParenthesesTest extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.SURROUND_WITH_PARENTHESES;
Future<void> test_cast_cast() async {
await resolveTestCode('''
void f(x) {
switch (x) {
case 0 as int as num:
break;
}
}
''');
await assertHasFix('''
void f(x) {
switch (x) {
case (0 as int) as num:
break;
}
}
''');
}
Future<void> test_cast_nullCheck() async {
await resolveTestCode('''
void f(x) {
switch (x) {
case 0 as int? ?:
break;
}
}
''');
await assertHasFix('''
void f(x) {
switch (x) {
case (0 as int?) ?:
break;
}
}
''');
}
Future<void> test_relationalNullCheck() async {
await resolveTestCode('''
void f(x) {
switch (x) {
case > 1?:
break;
}
}
''');
await assertHasFix('''
void f(x) {
switch (x) {
case (> 1)?:
break;
}
}
''');
}
}

View file

@ -251,6 +251,7 @@ import 'sort_combinators_test.dart' as sort_combinators_test;
import 'sort_constructor_first_test.dart' as sort_constructor_first_test;
import 'sort_unnamed_constructor_first_test.dart'
as sort_unnamed_constructor_first_test;
import 'surround_with_parentheses_test.dart' as surround_with_parentheses;
import 'type_literal_in_constant_pattern_test.dart'
as type_literal_in_constant_pattern;
import 'update_sdk_constraints_test.dart' as update_sdk_constraints;
@ -480,6 +481,7 @@ void main() {
sort_constructor_first_test.main();
sort_combinators_test.main();
sort_unnamed_constructor_first_test.main();
surround_with_parentheses.main();
type_literal_in_constant_pattern.main();
update_sdk_constraints.main();
use_curly_braces.main();