From 8cee82debb1b9b33e6d227f98979f1f4d9f89af4 Mon Sep 17 00:00:00 2001 From: pq Date: Wed, 8 Sep 2021 21:56:02 +0000 Subject: [PATCH] constructor tearoff fix support for `unnecessary_lambdas` Fixes: https://github.com/dart-lang/sdk/issues/47103 Change-Id: I521461a587b9272503772e9b13999d8e4e688352 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212803 Commit-Queue: Phil Quitslund Reviewed-by: Brian Wilkerson --- .../dart/replace_with_tear_off.dart | 10 ++ .../fix/replace_with_tear_off_test.dart | 115 ++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_tear_off.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_tear_off.dart index 8f0f19b600f..9c84f73641e 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_tear_off.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_tear_off.dart @@ -43,6 +43,16 @@ class ReplaceWithTearOff extends CorrectionProducer { builder.write(utils.getNodeText(expression.function)); }); }); + } else if (expression is InstanceCreationExpression) { + await builder.addDartFileEdit(file, (builder) { + builder.addReplacement(range.node(ancestor), (builder) { + var constructorName = expression.constructorName; + builder.write(utils.getNodeText(constructorName)); + if (constructorName.name == null) { + builder.write('.new'); + } + }); + }); } } diff --git a/pkg/analysis_server/test/src/services/correction/fix/replace_with_tear_off_test.dart b/pkg/analysis_server/test/src/services/correction/fix/replace_with_tear_off_test.dart index 52898ab207f..49f90416cc7 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/replace_with_tear_off_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/replace_with_tear_off_test.dart @@ -53,6 +53,121 @@ class ReplaceWithTearOffTest extends FixProcessorLintTest { @override String get lintCode => LintNames.unnecessary_lambdas; + Future test_constructorTearOff_named() async { + await resolveTestCode(''' +class C { + int c; + C.create([this.c = 3]); +} + +void f() { + var listOfInts = [1, 2, 3]; + for (var c in listOfInts.map((x) => C.create(x))) { + print(c.c); + } +} +'''); + await assertHasFix(''' +class C { + int c; + C.create([this.c = 3]); +} + +void f() { + var listOfInts = [1, 2, 3]; + for (var c in listOfInts.map(C.create)) { + print(c.c); + } +} +'''); + } + + // @soloTest + Future test_constructorTearOff_nameUnnamed() async { + await resolveTestCode(''' +class C { + int c; + C([this.c = 3]); +} + +void f() { + var listOfInts = [1, 2, 3]; + for (var c in listOfInts.map((x) => C.new(x))) { + print(c.c); + } +} +'''); + await assertHasFix(''' +class C { + int c; + C([this.c = 3]); +} + +void f() { + var listOfInts = [1, 2, 3]; + for (var c in listOfInts.map(C.new)) { + print(c.c); + } +} +'''); + } + + Future test_constructorTearOff_oneParameter() async { + await resolveTestCode(''' +class C { + int c; + C([this.c = 3]); +} + +void f() { + var listOfInts = [1, 2, 3]; + for (var c in listOfInts.map((x) => C(x))) { + print(c.c); + } +} +'''); + await assertHasFix(''' +class C { + int c; + C([this.c = 3]); +} + +void f() { + var listOfInts = [1, 2, 3]; + for (var c in listOfInts.map(C.new)) { + print(c.c); + } +} +'''); + } + + Future test_constructorTearOff_zeroParameters() async { + await resolveTestCode(''' +typedef Maker = Object Function(); + +class C { + int c; + C([this.c = 3]); +} + +var l = [ + () => C(), +]; +'''); + await assertHasFix(''' +typedef Maker = Object Function(); + +class C { + int c; + C([this.c = 3]); +} + +var l = [ + C.new, +]; +'''); + } + Future test_function_oneParameter() async { await resolveTestCode(''' Function f() => (name) {