Skip to content

Commit

Permalink
Merge pull request #930 from Workiva/fix-key-diagnostic-ide-error
Browse files Browse the repository at this point in the history
FED-2855 Fix Analyzer Plugin error when typing keys
  • Loading branch information
rmconsole3-wf authored Jun 24, 2024
2 parents c08e8f6 + 4df64d0 commit b3ddb94
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,24 @@ 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 &&
[
typedProvider.iterableType(typedProvider.dynamicType),
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.
Expand All @@ -166,7 +169,7 @@ class BadKeyDiagnostic extends ComponentUsageDiagnosticContributor {
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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,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
Expand All @@ -94,6 +95,7 @@ class BadKeyDiagnosticTest_NoErrors extends BadKeyDiagnosticTest {
unorderedEquals(<dynamic>[
isA<AnalysisError>().havingCode('missing_identifier'),
isA<AnalysisError>().havingCode('missing_identifier'),
isA<AnalysisError>().havingCode('undefined_identifier'),
isA<AnalysisError>().havingCode('use_of_void_result'),
]),
reason: 'should only have the Dart analysis errors we expect');
Expand Down

0 comments on commit b3ddb94

Please sign in to comment.