Skip to content

Commit aea99b2

Browse files
committed
scope debug property assist to Diagnosticables
Fixes: #42783 Change-Id: Ied9b0fa04e0f99b4d5b4604c72696b5cfaec737d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155501 Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 4b96f20 commit aea99b2

File tree

6 files changed

+65
-10
lines changed

6 files changed

+65
-10
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class AddDiagnosticPropertyReference extends CorrectionProducer {
2727
if (node is! SimpleIdentifier) {
2828
return;
2929
}
30+
31+
var classDeclaration = node.thisOrAncestorOfType<ClassOrMixinDeclaration>();
32+
if (classDeclaration == null ||
33+
!flutter.isDiagnosticable(classDeclaration.declaredElement.thisType)) {
34+
return;
35+
}
36+
3037
SimpleIdentifier name = node;
3138
final parent = node.parent;
3239

@@ -98,10 +105,6 @@ class AddDiagnosticPropertyReference extends CorrectionProducer {
98105
builder.writeln("$constructorName('${name.name}', ${name.name}));");
99106
}
100107

101-
final classDeclaration = parent.thisOrAncestorOfType<ClassDeclaration>();
102-
if (classDeclaration == null) {
103-
return;
104-
}
105108
final debugFillProperties =
106109
classDeclaration.getMethod('debugFillProperties');
107110
if (debugFillProperties == null) {

pkg/analysis_server/lib/src/utilities/flutter.dart

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class Flutter {
3636
final Uri _uriContainer = Uri.parse(
3737
'package:flutter/src/widgets/container.dart',
3838
);
39+
final Uri _uriDiagnostics = Uri.parse(
40+
'package:flutter/src/foundation/diagnostics.dart',
41+
);
3942
final Uri _uriEdgeInsets = Uri.parse(
4043
'package:flutter/src/painting/edge_insets.dart',
4144
);
@@ -337,6 +340,35 @@ class Flutter {
337340
return isColorElement(type.element);
338341
}
339342

343+
/// Return `true` if the given [type] is the flutter mixin `Diagnosticable`
344+
/// or its subtype.
345+
bool isDiagnosticable(DartType type) {
346+
if (type is! InterfaceType) {
347+
return false;
348+
}
349+
350+
bool isDiagnosticableElement(ClassElement element) {
351+
if (element == null) {
352+
return false;
353+
}
354+
355+
bool isExactDiagnosticable(ClassElement element) =>
356+
element?.name == 'Diagnosticable' &&
357+
element.source.uri == _uriDiagnostics;
358+
if (isExactDiagnosticable(element)) {
359+
return true;
360+
}
361+
for (var type in element.allSupertypes) {
362+
if (isExactDiagnosticable(type.element)) {
363+
return true;
364+
}
365+
}
366+
return false;
367+
}
368+
369+
return isDiagnosticableElement(type.element);
370+
}
371+
340372
/// Return `true` if the [element] is the Flutter class `Alignment`.
341373
bool isExactAlignment(ClassElement element) {
342374
return _isExactWidget(element, 'Alignment', _uriAlignment);
@@ -432,8 +464,8 @@ class Flutter {
432464
isWidgetType(type.typeArguments[0]);
433465
}
434466

435-
/// Return `true` if the given [type] is the dart.ui class `Color`, or its
436-
/// subtype.
467+
/// Return `true` if the given [type] is the vector_math_64 class `Matrix4`,
468+
/// or its subtype.
437469
bool isMatrix4(DartType type) {
438470
if (type is! InterfaceType) {
439471
return false;

pkg/analysis_server/test/mock_packages/flutter/lib/src/foundation/diagnostics.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
abstract class Diagnosticable with DiagnosticableMixin {}
6-
5+
///// todo (pq): remove when linter 0.1.118 is integrated.
76
mixin DiagnosticableMixin {}
87

8+
mixin Diagnosticable {}
9+
10+
abstract class DiagnosticableTree with Diagnosticable {}
11+
912
class DiagnosticPropertiesBuilder {
1013
void add(DiagnosticsNode property) {}
1114
}

pkg/analysis_server/test/mock_packages/flutter/lib/src/widgets/framework.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ abstract class StatelessWidget extends Widget {
4747
Widget build(BuildContext context) => null;
4848
}
4949

50-
class Widget {
50+
class Widget extends DiagnosticableTree {
5151
final Key key;
5252

5353
const Widget({this.key});

pkg/analysis_server/test/src/services/correction/assist/add_diagnostic_property_reference_test.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ class AddDiagnosticPropertyReferenceTest extends AssistProcessorTest {
2121

2222
/// Full coverage in fix/add_diagnostic_property_reference_test.dart
2323
Future<void> test_boolField_debugFillProperties() async {
24-
verifyNoTestUnitErrors = false;
24+
addFlutterPackage();
2525
await resolveTestUnit('''
26+
import 'package:flutter/foundation.dart';
27+
import 'package:flutter/widgets.dart';
28+
2629
class W extends Widget {
2730
bool /*caret*/property;
2831
@override
@@ -32,6 +35,9 @@ class W extends Widget {
3235
}
3336
''');
3437
await assertHasAssist('''
38+
import 'package:flutter/foundation.dart';
39+
import 'package:flutter/widgets.dart';
40+
3541
class W extends Widget {
3642
bool property;
3743
@override
@@ -49,6 +55,16 @@ class W extends Widget {
4955
mixin MyMixin {
5056
String get foo/*caret*/() {}
5157
}
58+
''');
59+
await assertNoAssist();
60+
}
61+
62+
Future<void> test_notAvailable_outsideDiagnosticable() async {
63+
addFlutterPackage();
64+
await resolveTestUnit('''
65+
class C {
66+
String get f/*caret*/ => null;
67+
}
5268
''');
5369
await assertNoAssist();
5470
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class AddDiagnosticPropertyReferenceTest extends FixProcessorLintTest {
3131
}
3232

3333
Future<void> test_boolField() async {
34+
// todo(pq): when linter 0.1.118 is integrated, update DiagnosticableMixin to Diagnosticable
3435
await resolveTestUnit('''
3536
import 'package:flutter/foundation.dart';
3637
import 'package:flutter/widgets.dart';

0 commit comments

Comments
 (0)