Skip to content

Commit

Permalink
Fix crash when encountering ProviderBase fields (#3136)
Browse files Browse the repository at this point in the history
fixes #3131
  • Loading branch information
rrousselGit authored Nov 20, 2023
1 parent 1d10c36 commit ee99ee0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
5 changes: 5 additions & 0 deletions packages/riverpod_analyzer_utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -175,7 +175,7 @@ enum LegacyProviderType {
return LegacyProviderType.changeNotifierProvider;
}

throw StateError('Unknown provider type $providerType');
return null;
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -248,7 +248,7 @@ class LegacyProviderDeclarationElement implements ProviderDeclarationElement {

final LegacyFamilyInvocationElement? familyElement;

final LegacyProviderType providerType;
final LegacyProviderType? providerType;
}

class LegacyFamilyInvocationElement {
Expand Down
51 changes: 50 additions & 1 deletion packages/riverpod_analyzer_utils_tests/test/consumer_test.dart
Original file line number Diff line number Diff line change
@@ -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<T> extends ConsumerWidget {
const ProviderWidget({super.key, required this.provider});
final ProviderBase<AsyncValue<T>> 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<ConsumerWidgetDeclaration>());
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<WidgetRefWatchInvocation>()
.having((e) => e.node.toSource(), 'node', 'ref.watch(provider)')
.having(
(e) => e.provider,
'provider',
isA<ProviderListenableExpression>()
.having((e) => e.node.toSource(), 'node', 'provider')
.having(
(e) => e.providerElement,
'providerElement',
isA<LegacyProviderDeclarationElement>()
.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';
Expand Down
4 changes: 4 additions & 0 deletions packages/riverpod_generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/riverpod_lint/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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

Expand Down

0 comments on commit ee99ee0

Please sign in to comment.