diff --git a/packages/riverpod_analyzer_utils/CHANGELOG.md b/packages/riverpod_analyzer_utils/CHANGELOG.md index ac40a1b5e..4b4ecd14a 100644 --- a/packages/riverpod_analyzer_utils/CHANGELOG.md +++ b/packages/riverpod_analyzer_utils/CHANGELOG.md @@ -1,3 +1,8 @@ +## Unreleased minor + +- **Breaking** `LegacyProviderDeclarationElement.providerType` is now nullable. +- Fix crash when parsing classes with a `ProviderBase` field. + ## 0.4.3 - 2023-10-28 - `GeneratorProviderDeclaration.createdTypeDisplayString` now always diff --git a/packages/riverpod_analyzer_utils/lib/src/riverpod_element.dart b/packages/riverpod_analyzer_utils/lib/src/riverpod_element.dart index bc18d89b7..6c1bdefae 100644 --- a/packages/riverpod_analyzer_utils/lib/src/riverpod_element.dart +++ b/packages/riverpod_analyzer_utils/lib/src/riverpod_element.dart @@ -149,7 +149,7 @@ enum LegacyProviderType { /// Type for `Provider` provider; - static LegacyProviderType _parse(DartType providerType) { + static LegacyProviderType? _parse(DartType providerType) { if (anyFutureProviderType.isAssignableFromType(providerType)) { return LegacyProviderType.futureProvider; } @@ -175,7 +175,7 @@ enum LegacyProviderType { return LegacyProviderType.changeNotifierProvider; } - throw StateError('Unknown provider type $providerType'); + return null; } } @@ -203,7 +203,7 @@ class LegacyProviderDeclarationElement implements ProviderDeclarationElement { bool isAutoDispose; LegacyFamilyInvocationElement? familyElement; - LegacyProviderType providerType; + LegacyProviderType? providerType; if (providerBaseType.isAssignableFromType(element.type)) { isAutoDispose = !alwaysAliveProviderListenableType .isAssignableFromType(element.type); @@ -248,7 +248,7 @@ class LegacyProviderDeclarationElement implements ProviderDeclarationElement { final LegacyFamilyInvocationElement? familyElement; - final LegacyProviderType providerType; + final LegacyProviderType? providerType; } class LegacyFamilyInvocationElement { diff --git a/packages/riverpod_analyzer_utils_tests/test/consumer_test.dart b/packages/riverpod_analyzer_utils_tests/test/consumer_test.dart index f3a025a6b..b23769ef2 100644 --- a/packages/riverpod_analyzer_utils_tests/test/consumer_test.dart +++ b/packages/riverpod_analyzer_utils_tests/test/consumer_test.dart @@ -1,9 +1,58 @@ -import 'package:riverpod_analyzer_utils/src/riverpod_ast.dart'; +import 'package:riverpod_analyzer_utils/riverpod_analyzer_utils.dart'; import 'package:test/test.dart'; import 'analyzer_test_utils.dart'; void main() { + testSource('Handles consumers with a ProviderBase inside', source: ''' +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter/material.dart'; + +class ProviderWidget extends ConsumerWidget { + const ProviderWidget({super.key, required this.provider}); + + final ProviderBase> provider; + + @override + Widget build(BuildContext context, WidgetRef ref) { + ref.watch(provider); + return Container(); + } +} +''', (resolver) async { + final result = await resolver.resolveRiverpodAnalysisResult(); + + final consumerWidget = result.consumerWidgetDeclarations.single; + expect(consumerWidget, isA()); + expect(consumerWidget.node.name.toString(), 'ProviderWidget'); + expect( + consumerWidget.buildMethod!.toSource(), + '@override Widget build(BuildContext context, WidgetRef ref) {ref.watch(provider); return Container();}', + ); + + expect(consumerWidget.widgetRefInvocations, [ + isA() + .having((e) => e.node.toSource(), 'node', 'ref.watch(provider)') + .having( + (e) => e.provider, + 'provider', + isA() + .having((e) => e.node.toSource(), 'node', 'provider') + .having( + (e) => e.providerElement, + 'providerElement', + isA() + .having((e) => e.providerType, 'providerType', null), + ), + ), + ]); + + expect( + result.resolvedRiverpodLibraryResults.single.unknownWidgetRefInvocations, + isEmpty, + ); + }); + testSource('Decode ConsumerWidget declarations', source: ''' import 'package:riverpod/riverpod.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; diff --git a/packages/riverpod_generator/CHANGELOG.md b/packages/riverpod_generator/CHANGELOG.md index 540d0312b..4749db726 100644 --- a/packages/riverpod_generator/CHANGELOG.md +++ b/packages/riverpod_generator/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased fix + +- Fix crash when encountering classes with a `ProviderBase` field. + ## 2.3.6 - 2023-11-13 - Fix typos and internal changes diff --git a/packages/riverpod_lint/CHANGELOG.md b/packages/riverpod_lint/CHANGELOG.md index 55a5e1dda..c6908a86a 100644 --- a/packages/riverpod_lint/CHANGELOG.md +++ b/packages/riverpod_lint/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased fix + +- Fix crash when encountering classes with a `ProviderBase` field. + ## 2.3.4 - 2023-11-13 - Updated `scoped_providers_should_specify_dependencies` to ignore instances of using pumpWidget in tests (thanks to [lockieRichter](https://github.com/lockieRichter)) @@ -21,7 +25,6 @@ - Added `protected_notifier_state` lint, which warns against using the `Notifier.state` property of a notifier different than the current one. Aka a Notifier "A" should not directly access the `state` if a Notifier "B". - ## 2.2.1 - 2023-10-02