Add quick fix for invalid_constant_pattern_binary and invalid_constant_pattern_negation diagnostic.

Bug: 49960
Change-Id: Ib6c6951da8febd5bbe53b704d34530da0e47a0e6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283461
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Keerti Parthasarathy <keertip@google.com>
This commit is contained in:
Keerti Parthasarathy 2023-02-16 20:22:54 +00:00 committed by Commit Queue
parent 28f69d5bd3
commit 71bbeddf00
4 changed files with 114 additions and 31 deletions

View file

@ -51,6 +51,24 @@ class AddConst extends CorrectionProducer {
}
return;
}
if (targetNode is BinaryExpression || targetNode is PrefixExpression) {
var node_final = targetNode?.parent;
if (node_final?.parent is ParenthesizedPattern) {
// add const
var offset = node_final!.parent!.offset;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleInsertion(offset, 'const ');
});
} else {
// add const and parenthesis
var offset = node_final!.offset;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleInsertion(offset + node_final.length, ')');
builder.addSimpleInsertion(offset, 'const (');
});
}
return;
}
bool isParentConstant(
DartFileEditBuilderImpl builder, Expression targetNode) {

View file

@ -2278,7 +2278,7 @@ ParserErrorCode.INVALID_COMMENT_REFERENCE:
ParserErrorCode.INVALID_CONSTANT_CONST_PREFIX:
status: needsEvaluation
ParserErrorCode.INVALID_CONSTANT_PATTERN_BINARY:
status: needsEvaluation
status: hasFix
ParserErrorCode.INVALID_CONSTANT_PATTERN_DUPLICATE_CONST:
status: needsEvaluation
ParserErrorCode.INVALID_CONSTANT_PATTERN_EMPTY_RECORD_LITERAL:
@ -2286,7 +2286,7 @@ ParserErrorCode.INVALID_CONSTANT_PATTERN_EMPTY_RECORD_LITERAL:
ParserErrorCode.INVALID_CONSTANT_PATTERN_GENERIC:
status: needsEvaluation
ParserErrorCode.INVALID_CONSTANT_PATTERN_NEGATION:
status: needsEvaluation
status: hasFix
ParserErrorCode.INVALID_CONSTANT_PATTERN_UNARY:
status: needsEvaluation
ParserErrorCode.INVALID_CONSTRUCTOR_NAME:

View file

@ -1464,6 +1464,12 @@ class FixProcessor extends BaseProcessor {
ParserErrorCode.GETTER_WITH_PARAMETERS: [
RemoveParametersInGetterDeclaration.new,
],
ParserErrorCode.INVALID_CONSTANT_PATTERN_BINARY: [
AddConst.new,
],
ParserErrorCode.INVALID_CONSTANT_PATTERN_NEGATION: [
AddConst.new,
],
ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE: [
AddTypeAnnotation.new,
],

View file

@ -194,6 +194,46 @@ class AddConst_PatternExpressionMustBeValidConst extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.ADD_CONST;
Future<void> test_caseBinaryExpression() async {
await resolveTestCode('''
void f() {
var m = 5;
switch(m) {
case (5 * 5): break;
}
}
''');
await assertHasFix('''
void f() {
var m = 5;
switch(m) {
case const (5 * 5): break;
}
}
''');
}
Future<void> test_caseBinaryExpressionNoParen() async {
await resolveTestCode('''
void f() {
var m = 5;
switch(m) {
case 5 * 5: break;
}
}
''');
await assertHasFix('''
void f() {
var m = 5;
switch(m) {
case const (5 * 5): break;
}
}
''');
}
@FailingTest(issue: "https://github.com/dart-lang/sdk/issues/51139")
Future<void> test_caseConstConstructorCall() async {
await resolveTestCode('''
@ -224,27 +264,69 @@ void f() {
}
@FailingTest(
issue: "https://github.com/dart-lang/sdk/issues/50996",
issue: "https://github.com/dart-lang/sdk/issues/50947",
reason: "Waiting on issue to be resolved")
Future<void> test_caseConstExpression() async {
Future<void> test_caseListExpression() async {
await resolveTestCode('''
class A {}
void f() {
var m = 5;
switch(m) {
case (5 * 5): break;
case List<A>: break;
}
''');
await assertHasFix('''
class A {}
void f() {
var m = 5;
switch(m) {
case const (5 * 5): break;
case const List<A>: break;
}
''');
}
Future<void> test_caseConstWithField() async {
Future<void> test_casePrefixExpression() async {
await resolveTestCode('''
void f(Object? x) {
const m = 5;
switch(x) {
case (-m): break;
}
}
''');
await assertHasFix('''
void f(Object? x) {
const m = 5;
switch(x) {
case const (-m): break;
}
}
''');
}
Future<void> test_casePrefixExpressionNoParen() async {
await resolveTestCode('''
void f(Object? x) {
const m = 5;
switch(x) {
case -m: break;
}
}
''');
await assertHasFix('''
void f(Object? x) {
const m = 5;
switch(x) {
case const (-m): break;
}
}
''');
}
Future<void> test_caseWithField() async {
await resolveTestCode('''
int x = 1;
void f() {
@ -259,7 +341,7 @@ void f() {
}
@FailingTest(reason: "TODO(keertip): Add support for local variables")
Future<void> test_caseConstWithLocalVariable() async {
Future<void> test_caseWithLocalVariable() async {
await resolveTestCode('''
void f() {
var m = 5;
@ -281,29 +363,6 @@ void f() {
''');
}
@FailingTest(
issue: "https://github.com/dart-lang/sdk/issues/50947",
reason: "Waiting on issue to be resolved")
Future<void> test_caseListConstExpression() async {
await resolveTestCode('''
class A {}
void f() {
var m = 5;
switch(m) {
case const List<A>: break;
}
''');
await assertHasFix('''
class A {}
void f() {
var m = 5;
switch(m) {
case List<A>: break;
}
''');
}
Future<void> test_mapKeyConst() async {
await resolveTestCode('''
void f(Object x) {