Skip to content

Commit

Permalink
Fix provider_dependencies not considering dependencies inside metho…
Browse files Browse the repository at this point in the history
…ds other than `build` of a notifier. (#2435)

fixes #2417
  • Loading branch information
rrousselGit committed Apr 7, 2023
1 parent 70470fa commit a823388
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/riverpod_analyzer_utils/CHANGELOG.md
@@ -1,6 +1,7 @@
## Unreleased fix

- Handle cascade operators in ref expressions
- Fix ref.watch/read/... not being decoded inside Notifier methods of generated providers.

## 0.2.0 - 2023-03-13

Expand Down
Expand Up @@ -161,7 +161,7 @@ class StatefulProviderDeclaration extends GeneratorProviderDeclaration {
valueType: _getValueType(createdType),
);
riverpodAnnotation._parent = statefulProviderDeclaration;
buildMethod.accept(
node.accept(
_GeneratorRefInvocationVisitor(statefulProviderDeclaration, parent),
);

Expand Down
Expand Up @@ -36,6 +36,44 @@ final dependency = Provider((ref) {
expect(result.refWatchInvocations.single.provider.providerElement, null);
});

testSource('Decodes ref expressions in Notifier methods',
runGenerator: true, source: r'''
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'foo.g.dart';
@riverpod
class MyNotifier extends _$MyNotifier {
@override
int build() => 0;
void method() {
ref.watch(generatedScopedProvider);
}
}
''', (resolver) async {
// Regression test for https://github.com/rrousselGit/riverpod/issues/2417
final result = await resolver.resolveRiverpodAnalyssiResult();

final notifier = result.statefulProviderDeclarations.single;

expect(result.refInvocations, hasLength(1));
expect(
result.refWatchInvocations.single.provider.node.toSource(),
'generatedScopedProvider',
);
expect(
notifier.refInvocations.single,
isA<RefWatchInvocation>().having(
(e) => e.provider.node.toSource(),
'provider',
'generatedScopedProvider',
),
);
});

testSource('Decodes ..watch', runGenerator: true, source: r'''
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
Expand Down
2 changes: 2 additions & 0 deletions packages/riverpod_lint/CHANGELOG.md
Expand Up @@ -3,6 +3,8 @@
- Disable unsupported_provider_value when a notifier returns "this"
- Fix scoped_providers_should_specify_dependencies incorrectly triggering on functions other than "main"
- Handle cascade operators in ref expressions
- Fix `provider_dependencies` not considering dependencies inside methods
other than `build` of a notifier.

## 1.1.7 - 2023-04-06

Expand Down
Expand Up @@ -175,3 +175,13 @@ int regression2348(Regression2348Ref ref) {
ref..watch(generatedScopedProvider);
return 0;
}

@Riverpod(dependencies: [generatedScoped])
class Regression2417 extends _$Regression2417 {
@override
int build() => 0;

void method() {
ref.watch(generatedScopedProvider);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a823388

Please sign in to comment.