Skip to content

Commit cf5e9ca

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Additional tests and fixes for creating getters
Change-Id: Ia162cc49987d48c71052cba85c2d5da159c2c8f1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115722 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 6e3498c commit cf5e9ca

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,8 @@ class FixProcessor extends BaseProcessor {
19891989
}
19901990
}
19911991
} else {
1992-
targetElement = getEnclosingClassElement(node);
1992+
targetElement =
1993+
getEnclosingClassElement(node) ?? getEnclosingExtensionElement(node);
19931994
if (targetElement == null) {
19941995
return;
19951996
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,10 @@ String getElementQualifiedName(Element element) {
232232
}
233233
}
234234

235-
/**
236-
* If the given [AstNode] is in a [ClassOrMixinDeclaration], returns the
237-
* [ClassElement]. Otherwise returns `null`.
238-
*/
239-
ClassElement getEnclosingClassElement(AstNode node) {
240-
ClassOrMixinDeclaration enclosingClassNode =
241-
node.thisOrAncestorOfType<ClassOrMixinDeclaration>();
242-
if (enclosingClassNode != null) {
243-
return enclosingClassNode.declaredElement;
244-
}
245-
return null;
246-
}
235+
/// If the given [node] is in a class, enum or mixin declaration, return the
236+
/// declared [ClassElement]. Otherwise return `null`.
237+
ClassElement getEnclosingClassElement(AstNode node) =>
238+
node.thisOrAncestorOfType<ClassOrMixinDeclaration>()?.declaredElement;
247239

248240
/**
249241
* Returns a class or an unit member enclosing the given [node].
@@ -301,6 +293,11 @@ AstNode getEnclosingExecutableNode(AstNode node) {
301293
return null;
302294
}
303295

296+
/// If the given [node] is in an extension, return the declared
297+
/// [ExtensionElement]. Otherwise return `null`.
298+
ExtensionElement getEnclosingExtensionElement(AstNode node) =>
299+
node.thisOrAncestorOfType<ExtensionDeclaration>()?.declaredElement;
300+
304301
/**
305302
* Returns [getExpressionPrecedence] for the parent of [node], or
306303
* ASSIGNMENT_PRECEDENCE if the parent node is a [ParenthesizedExpression].

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,36 @@ class CreateGetterWithExtensionMethodsTest extends FixProcessorTest {
373373
super.setUp();
374374
}
375375

376+
test_internal_instance() async {
377+
await resolveTestUnit('''
378+
extension E on String {
379+
int m() => g;
380+
}
381+
''');
382+
await assertHasFix('''
383+
extension E on String {
384+
get g => null;
385+
386+
int m() => g;
387+
}
388+
''');
389+
}
390+
391+
test_internal_static() async {
392+
await resolveTestUnit('''
393+
extension E on String {
394+
static int m() => g;
395+
}
396+
''');
397+
await assertHasFix('''
398+
extension E on String {
399+
static get g => null;
400+
401+
static int m() => g;
402+
}
403+
''');
404+
}
405+
376406
test_override() async {
377407
await resolveTestUnit('''
378408
extension E on String {

0 commit comments

Comments
 (0)