From 6e4e9f3f4fc67f592f96d3c6f349864178c2b9ac Mon Sep 17 00:00:00 2001 From: Greg Littlefield Date: Tue, 28 Nov 2023 13:19:06 -0700 Subject: [PATCH 1/3] Add regression test for undefined identifiers --- .../test/integration/diagnostics/bad_key_test.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/analyzer_plugin/test/integration/diagnostics/bad_key_test.dart b/tools/analyzer_plugin/test/integration/diagnostics/bad_key_test.dart index d5c2974ea..df34eac14 100644 --- a/tools/analyzer_plugin/test/integration/diagnostics/bad_key_test.dart +++ b/tools/analyzer_plugin/test/integration/diagnostics/bad_key_test.dart @@ -81,6 +81,7 @@ class BadKeyDiagnosticTest_NoErrors extends BadKeyDiagnosticTest { (Dom.div()..key = 'greg')(), // Missing RHS (Dom.div()..key = )(), + (Dom.div()..key = undefinedVariable)(), // Missing interpolated expression (Dom.div()..key = '${}')(), // Weird type @@ -93,6 +94,7 @@ class BadKeyDiagnosticTest_NoErrors extends BadKeyDiagnosticTest { unorderedEquals([ isA().havingCode('missing_identifier'), isA().havingCode('missing_identifier'), + isA().havingCode('undefined_identifier'), isA().havingCode('use_of_void_result'), ]), reason: 'should only have the Dart analysis errors we expect'); From 42b1a96405846b0ab56c31f8e2043b656425257a Mon Sep 17 00:00:00 2001 From: Greg Littlefield Date: Tue, 28 Nov 2023 13:21:03 -0700 Subject: [PATCH 2/3] Handle InvalidType case, clean up unsafe cast --- tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart b/tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart index f32329a78..027f843af 100644 --- a/tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart +++ b/tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart @@ -139,11 +139,15 @@ class BadKeyDiagnostic extends ComponentUsageDiagnosticContributor { final topLevelKeyType = expression.staticType; // Type can't be resolved; bail out. if (topLevelKeyType == null) return; + // The type is bad because of errors, such as when the user's in the middle of typing an identifier, + // or referencing an identifier that doesn't exist yet. + if (topLevelKeyType is InvalidType) return; final typedProvider = result.typeProvider; // Special-case for Iterables and Maps: check if their (keys and) values are allowed types. final isIterableOrMap = + topLevelKeyType is! DynamicType && // Need to make sure it's not null since null is a subtype of all nullable-types. !topLevelKeyType.isDartCoreNull && [ @@ -151,9 +155,8 @@ class BadKeyDiagnostic extends ComponentUsageDiagnosticContributor { typedProvider.mapType(typedProvider.dynamicType, typedProvider.dynamicType), ].any((type) => result.typeSystem.isSubtypeOf(topLevelKeyType, type)); - final keyTypesToProcess = { - if (isIterableOrMap) ...(topLevelKeyType as InterfaceType).typeArguments else topLevelKeyType, - }; + final keyTypesToProcess = + isIterableOrMap && topLevelKeyType is InterfaceType ? topLevelKeyType.typeArguments : [topLevelKeyType]; for (final type in keyTypesToProcess) { // Provide context if this type was derived from a Map/Iterable type argument. @@ -162,7 +165,7 @@ class BadKeyDiagnostic extends ComponentUsageDiagnosticContributor { if (type.isDartCoreInt || type.isDartCoreDouble || type.isDartCoreString || type.isDartCoreSymbol) { // Ignore core types that have good `Object.toString` implementations values. - } else if (type.isDartCoreObject || type.isDynamic) { + } else if (type.isDartCoreObject || type is DynamicType) { collector.addError( dynamicOrObjectCode, result.locationFor(expression), From 4df64d061d5198657ef39d23d69f0b4155898960 Mon Sep 17 00:00:00 2001 From: Greg Littlefield Date: Fri, 21 Jun 2024 11:54:21 -0700 Subject: [PATCH 3/3] Update analyzer lower bound to version that added InvalidType --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index ee33fea2b..bb954c9ad 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,7 +7,7 @@ environment: dependencies: collection: ^1.15.0 - analyzer: ^5.1.0 + analyzer: ^5.13.0 build: '>=1.0.0 <3.0.0' dart_style: ^2.0.0 js: ^0.6.1+1