From 04bcc30c5894699c22e118ed4d366fe8cfdd7af2 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Fri, 6 May 2022 15:07:20 +0000 Subject: [PATCH] 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 Reviewed-by: Brian Wilkerson --- .../correction/dart/add_trailing_comma.dart | 4 ++ .../fix/add_trailing_comma_test.dart | 66 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_trailing_comma.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_trailing_comma.dart index a7948d5a3aa..2e221916b02 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/add_trailing_comma.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/add_trailing_comma.dart @@ -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); } } diff --git a/pkg/analysis_server/test/src/services/correction/fix/add_trailing_comma_test.dart b/pkg/analysis_server/test/src/services/correction/fix/add_trailing_comma_test.dart index 474e1d875bc..cd498c11e9a 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/add_trailing_comma_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/add_trailing_comma_test.dart @@ -102,6 +102,50 @@ void f(a, b) { '''); } + @failingTest + Future 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 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 test_named() async { await resolveTestCode(''' void f({a, b}) { @@ -140,6 +184,28 @@ void f(a, b) { f('a', 'b',); } +'''); + } + + @failingTest + Future test_set_literal() async { + await resolveTestCode(''' +void f() { + var l = { + 'a', + 'b' + }; + print(l); +} +'''); + await assertHasFix(''' +void f() { + var l = { + 'a', + 'b', + }; + print(l); +} '''); } }