Skip to content

Commit

Permalink
Additional tests and fixes for creating getters
Browse files Browse the repository at this point in the history
Change-Id: Ia162cc49987d48c71052cba85c2d5da159c2c8f1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115722
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
bwilkerson authored and commit-bot@chromium.org committed Sep 5, 2019
1 parent 6e3498c commit cf5e9ca
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,8 @@ class FixProcessor extends BaseProcessor {
}
}
} else {
targetElement = getEnclosingClassElement(node);
targetElement =
getEnclosingClassElement(node) ?? getEnclosingExtensionElement(node);
if (targetElement == null) {
return;
}
Expand Down
21 changes: 9 additions & 12 deletions pkg/analysis_server/lib/src/services/correction/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,10 @@ String getElementQualifiedName(Element element) {
}
}

/**
* If the given [AstNode] is in a [ClassOrMixinDeclaration], returns the
* [ClassElement]. Otherwise returns `null`.
*/
ClassElement getEnclosingClassElement(AstNode node) {
ClassOrMixinDeclaration enclosingClassNode =
node.thisOrAncestorOfType<ClassOrMixinDeclaration>();
if (enclosingClassNode != null) {
return enclosingClassNode.declaredElement;
}
return null;
}
/// If the given [node] is in a class, enum or mixin declaration, return the
/// declared [ClassElement]. Otherwise return `null`.
ClassElement getEnclosingClassElement(AstNode node) =>
node.thisOrAncestorOfType<ClassOrMixinDeclaration>()?.declaredElement;

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

/// If the given [node] is in an extension, return the declared
/// [ExtensionElement]. Otherwise return `null`.
ExtensionElement getEnclosingExtensionElement(AstNode node) =>
node.thisOrAncestorOfType<ExtensionDeclaration>()?.declaredElement;

/**
* Returns [getExpressionPrecedence] for the parent of [node], or
* ASSIGNMENT_PRECEDENCE if the parent node is a [ParenthesizedExpression].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,36 @@ class CreateGetterWithExtensionMethodsTest extends FixProcessorTest {
super.setUp();
}

test_internal_instance() async {
await resolveTestUnit('''
extension E on String {
int m() => g;
}
''');
await assertHasFix('''
extension E on String {
get g => null;
int m() => g;
}
''');
}

test_internal_static() async {
await resolveTestUnit('''
extension E on String {
static int m() => g;
}
''');
await assertHasFix('''
extension E on String {
static get g => null;
static int m() => g;
}
''');
}

test_override() async {
await resolveTestUnit('''
extension E on String {
Expand Down

0 comments on commit cf5e9ca

Please sign in to comment.