Skip to content

Commit 17cb638

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes static tearoff create method and type parameter use
Fixes: #60294 Change-Id: Ida570a50ed8e906e108201095b710200d6978263 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417901 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 89e3a63 commit 17cb638

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ class CreateMethodOrFunction extends ResolvedCorrectionProducer {
5555
isStatic = true;
5656
targetElement = element;
5757
argument = parent;
58+
} else if (target
59+
case SimpleIdentifier identifier ||
60+
PrefixedIdentifier(:var identifier)) {
61+
if (identifier.element case InterfaceElement element) {
62+
isStatic = true;
63+
targetElement = element;
64+
argument = target.parent as Expression;
65+
} else {
66+
return;
67+
}
5868
} else {
5969
return;
6070
}
@@ -160,6 +170,8 @@ class CreateMethodOrFunction extends ResolvedCorrectionProducer {
160170
builder.addLinkedEdit('NAME', (builder) {
161171
builder.write(name);
162172
});
173+
// append type parameters
174+
builder.writeTypeParameters(functionType.typeParameters);
163175
// append parameters
164176
builder.writeFormalParameters(functionType.formalParameters);
165177
if (functionType.returnType.isDartAsyncFuture) {

pkg/analysis_server/test/src/services/correction/fix/create_method_test.dart

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,147 @@ extension E on String {
16211621
void f() {
16221622
E.m();
16231623
}
1624+
''');
1625+
}
1626+
1627+
Future<void> test_static_tearoff() async {
1628+
await resolveTestCode('''
1629+
void f() async {
1630+
int _ = await g(C.foo);
1631+
}
1632+
1633+
Future<T> g<T>(Future<T> Function() foo) => foo();
1634+
1635+
class C {
1636+
}
1637+
''');
1638+
await assertHasFix('''
1639+
void f() async {
1640+
int _ = await g(C.foo);
1641+
}
1642+
1643+
Future<T> g<T>(Future<T> Function() foo) => foo();
1644+
1645+
class C {
1646+
static Future<int> foo() async {
1647+
}
1648+
}
1649+
''');
1650+
}
1651+
1652+
Future<void> test_static_tearoff_prefixed_typeParameter() async {
1653+
await resolveTestCode('''
1654+
import '' as self;
1655+
1656+
void f() async {
1657+
await g(self.C.foo);
1658+
}
1659+
1660+
Future<T> g<T>(Future<S> Function<S>() foo) => foo();
1661+
1662+
class C {
1663+
}
1664+
''');
1665+
await assertHasFix('''
1666+
import '' as self;
1667+
1668+
void f() async {
1669+
await g(self.C.foo);
1670+
}
1671+
1672+
Future<T> g<T>(Future<S> Function<S>() foo) => foo();
1673+
1674+
class C {
1675+
static Future<S> foo<S>() async {
1676+
}
1677+
}
1678+
''');
1679+
}
1680+
1681+
Future<void> test_static_tearoff_prefixed_typeParameter_bound1() async {
1682+
await resolveTestCode('''
1683+
import '' as self;
1684+
1685+
void f() async {
1686+
await g(self.C.foo);
1687+
}
1688+
1689+
Future<T> g<T>(Future<S> Function<S extends num>() foo) => foo();
1690+
1691+
class C {
1692+
}
1693+
''');
1694+
await assertHasFix('''
1695+
import '' as self;
1696+
1697+
void f() async {
1698+
await g(self.C.foo);
1699+
}
1700+
1701+
Future<T> g<T>(Future<S> Function<S extends num>() foo) => foo();
1702+
1703+
class C {
1704+
static Future<S> foo<S extends num>() async {
1705+
}
1706+
}
1707+
''');
1708+
}
1709+
1710+
Future<void> test_static_tearoff_prefixed_typeParameter_bound2() async {
1711+
await resolveTestCode('''
1712+
import '' as self;
1713+
1714+
void f() async {
1715+
await g(self.C.foo);
1716+
}
1717+
1718+
Future<T> g<T extends num>(Future<S> Function<S extends T>() foo) => foo();
1719+
1720+
class C {
1721+
}
1722+
''');
1723+
await assertHasFix('''
1724+
import '' as self;
1725+
1726+
void f() async {
1727+
await g(self.C.foo);
1728+
}
1729+
1730+
Future<T> g<T extends num>(Future<S> Function<S extends T>() foo) => foo();
1731+
1732+
class C {
1733+
static Future<S> foo<S extends num>() async {
1734+
}
1735+
}
1736+
''');
1737+
}
1738+
1739+
Future<void> test_static_tearoff_prefixed_typeParameter_class() async {
1740+
await resolveTestCode('''
1741+
import '' as self;
1742+
1743+
void f() async {
1744+
await g(self.C.foo);
1745+
}
1746+
1747+
Future<T> g<T>(Future<S> Function<S>() foo) => foo();
1748+
1749+
class C<S> {
1750+
}
1751+
''');
1752+
await assertHasFix('''
1753+
import '' as self;
1754+
1755+
void f() async {
1756+
await g(self.C.foo);
1757+
}
1758+
1759+
Future<T> g<T>(Future<S> Function<S>() foo) => foo();
1760+
1761+
class C<S> {
1762+
static Future<S> foo<S>() async {
1763+
}
1764+
}
16241765
''');
16251766
}
16261767
}

0 commit comments

Comments
 (0)