File tree Expand file tree Collapse file tree 3 files changed +41
-13
lines changed
lib/src/services/correction
test/src/services/correction/fix Expand file tree Collapse file tree 3 files changed +41
-13
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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] .
Original file line number Diff line number Diff 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 ('''
378408extension E on String {
You can’t perform that action at this time.
0 commit comments