add trailing comma fix for literals

This PR extends the`add_trailing_comma` fix to handle for the [upcoming support of list/set/map literals in `require_trailing_commas`](https://github.com/dart-lang/linter/pull/3340).

It's likely the tests in this PR will fail until sdk update the linter dependency to include https://github.com/dart-lang/linter/pull/3340

Closes https://github.com/dart-lang/sdk/pull/48968

GitOrigin-RevId: 24b374ec39b313640f7c06fbf8f725c09d3ed7ac
Change-Id: I066a8524d18a4213eb5300b9310bdb4a562f0e27
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/243920
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Alexandre Ardhuin 2022-05-06 15:07:20 +00:00 committed by Commit Bot
parent bb3135586c
commit 04bcc30c58
2 changed files with 70 additions and 0 deletions

View file

@ -30,6 +30,10 @@ class AddTrailingComma extends CorrectionProducer {
await _insertComma(builder, node.parameters.last);
} else if (node is Assertion) {
await _insertComma(builder, node.message ?? node.condition);
} else if (node is ListLiteral) {
await _insertComma(builder, node.elements.last);
} else if (node is SetOrMapLiteral) {
await _insertComma(builder, node.elements.last);
}
}

View file

@ -102,6 +102,50 @@ void f(a, b) {
''');
}
@failingTest
Future<void> test_list_literal() async {
await resolveTestCode('''
void f() {
var l = [
'a',
'b'
];
print(l);
}
''');
await assertHasFix('''
void f() {
var l = [
'a',
'b',
];
print(l);
}
''');
}
@failingTest
Future<void> test_map_literal() async {
await resolveTestCode('''
void f() {
var l = {
'a': 1,
'b': 2
};
print(l);
}
''');
await assertHasFix('''
void f() {
var l = {
'a': 1,
'b': 2,
};
print(l);
}
''');
}
Future<void> test_named() async {
await resolveTestCode('''
void f({a, b}) {
@ -140,6 +184,28 @@ void f(a, b) {
f('a',
'b',);
}
''');
}
@failingTest
Future<void> test_set_literal() async {
await resolveTestCode('''
void f() {
var l = {
'a',
'b'
};
print(l);
}
''');
await assertHasFix('''
void f() {
var l = {
'a',
'b',
};
print(l);
}
''');
}
}