Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work on riverpod_graph #3178

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion examples/pub/lib/detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ class PackageMetrics extends _$PackageMetrics {
}
}

extension on WidgetRef {
T myRefresh<T>(Refreshable<T> provider) {
return refresh(provider);
}
}

/// The detail page of a package, typically reached by clicking on a package from [SearchPage].
class PackageDetailPage extends ConsumerWidget {
const PackageDetailPage({super.key, required this.packageName});
Expand All @@ -118,7 +124,7 @@ class PackageDetailPage extends ConsumerWidget {
return RefreshIndicator(
onRefresh: () {
return Future.wait([
ref.refresh(
ref.myRefresh(
packageMetricsProvider(packageName: packageName).future,
),
ref.refresh(
Expand Down
2 changes: 1 addition & 1 deletion examples/pub/lib/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Future<List<Package>> fetchPackages(
return Future.wait([
for (final package in searchedPackages)
ref.watch(
FetchPackageDetailsProvider(packageName: package.package).future,
fetchPackageDetailsProvider(packageName: package.package).future,
),
]);
}
Expand Down
8 changes: 7 additions & 1 deletion packages/riverpod_analyzer_utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
## Unreleased minor

- **Breaking** Removed all `WidgetRef*Invocation` in favour of a single `WidgetRefInvocation`.
- **Breaking** Removed all `Ref*Invocation` in favour of a single `RefInvocation`.
- Now, custom extension methods on `Ref`/`WidgetRef` are also parsed as their respective `RefInvocation`.

## 0.5.0 - 2023-11-20

- **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
3 changes: 3 additions & 0 deletions packages/riverpod_analyzer_utils/lib/src/riverpod_ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ abstract class RiverpodAst {
RiverpodAst? _parent;
RiverpodAst? get parent => _parent;

/// The [CompilationUnit] that contains this node, if any.
CompilationUnit? get unit;

void accept(RiverpodAstVisitor visitor);

@mustCallSuper
Expand Down
80 changes: 59 additions & 21 deletions packages/riverpod_analyzer_utils/lib/src/riverpod_ast/consumer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,70 @@ part of '../riverpod_ast.dart';
abstract class ConsumerDeclaration extends RiverpodAst {
static ConsumerDeclaration? _parse(
ClassDeclaration node,
_ParseRefInvocationMixin parent,
) {
_ParseRefInvocationMixin parent, {
required CompilationUnit unit,
}) {
final extendsClause = node.extendsClause;
if (extendsClause == null) return null;
final extendsType = extendsClause.superclass.type;
if (extendsType == null) return null;

if (consumerWidgetType.isExactlyType(extendsType)) {
return ConsumerWidgetDeclaration._parse(node, parent);
return ConsumerWidgetDeclaration._parse(node, parent, unit: unit);
} else if (hookConsumerWidgetType.isExactlyType(extendsType)) {
return HookConsumerWidgetDeclaration._parse(node, parent);
return HookConsumerWidgetDeclaration._parse(node, parent, unit: unit);
} else if (consumerStatefulWidgetType.isExactlyType(extendsType)) {
return ConsumerStatefulWidgetDeclaration.parse(node);
return ConsumerStatefulWidgetDeclaration.parse(node, unit: unit);
} else if (statefulHookConsumerStateType.isExactlyType(extendsType)) {
return StatefulHookConsumerWidgetDeclaration.parse(node);
return StatefulHookConsumerWidgetDeclaration.parse(node, unit: unit);
} else if (consumerStateType.isExactlyType(extendsType)) {
return ConsumerStateDeclaration._parse(node, parent);
return ConsumerStateDeclaration._parse(node, parent, unit: unit);
}

return null;
}

ClassDeclaration get node;
@override
CompilationUnit get unit;
}

class ConsumerWidgetDeclaration extends ConsumerDeclaration {
ConsumerWidgetDeclaration._({
required this.buildMethod,
required this.node,
required this.unit,
});

static ConsumerWidgetDeclaration? _parse(
ClassDeclaration node,
_ParseRefInvocationMixin parent,
) {
_ParseRefInvocationMixin parent, {
required CompilationUnit unit,
}) {
final buildMethod = node.members
.whereType<MethodDeclaration>()
.firstWhereOrNull((e) => e.name.lexeme == 'build');

final consumerWidgetDeclaration = ConsumerWidgetDeclaration._(
buildMethod: buildMethod,
node: node,
unit: unit,
);
final visitor = _ParseConsumerRefInvocationVisitor(
consumerWidgetDeclaration,
consumerWidgetDeclaration.widgetRefInvocations,
consumerWidgetDeclaration.providerScopeInstanceCreateExpressions,
parent,
unit: unit,
);

buildMethod?.accept(visitor);

return consumerWidgetDeclaration;
}

@override
final CompilationUnit unit;
final MethodDeclaration? buildMethod;
final List<WidgetRefInvocation> widgetRefInvocations = [];
final List<ProviderScopeInstanceCreationExpression>
Expand Down Expand Up @@ -88,9 +97,12 @@ class _ParseConsumerRefInvocationVisitor extends RecursiveAstVisitor<void>
this.parent,
this.widgetRefInvocations,
this.providerScopeInstanceCreateExpressions,
this.parentVisitor,
);
this.parentVisitor, {
required this.unit,
});

@override
final CompilationUnit unit;
final RiverpodAst parent;
final List<WidgetRefInvocation> widgetRefInvocations;
final List<ProviderScopeInstanceCreationExpression>
Expand Down Expand Up @@ -129,32 +141,38 @@ class HookConsumerWidgetDeclaration extends ConsumerDeclaration {
HookConsumerWidgetDeclaration({
required this.buildMethod,
required this.node,
required this.unit,
});

static HookConsumerWidgetDeclaration? _parse(
ClassDeclaration node,
_ParseRefInvocationMixin parent,
) {
_ParseRefInvocationMixin parent, {
required CompilationUnit unit,
}) {
final buildMethod = node.members
.whereType<MethodDeclaration>()
.firstWhereOrNull((e) => e.name.lexeme == 'build');

final consumerWidgetDeclaration = HookConsumerWidgetDeclaration(
buildMethod: buildMethod,
node: node,
unit: unit,
);
final visitor = _ParseConsumerRefInvocationVisitor(
consumerWidgetDeclaration,
consumerWidgetDeclaration.widgetRefInvocations,
consumerWidgetDeclaration.providerScopeInstanceCreateExpressions,
parent,
unit: unit,
);

buildMethod?.accept(visitor);

return consumerWidgetDeclaration;
}

@override
final CompilationUnit unit;
final MethodDeclaration? buildMethod;
final List<WidgetRefInvocation> widgetRefInvocations = [];
final List<ProviderScopeInstanceCreationExpression>
Expand All @@ -180,11 +198,18 @@ class HookConsumerWidgetDeclaration extends ConsumerDeclaration {
}

class ConsumerStatefulWidgetDeclaration extends ConsumerDeclaration {
ConsumerStatefulWidgetDeclaration._({required this.node});
ConsumerStatefulWidgetDeclaration._({
required this.node,
required this.unit,
});

ConsumerStatefulWidgetDeclaration.parse(ClassDeclaration node)
: this._(node: node);
ConsumerStatefulWidgetDeclaration.parse(
ClassDeclaration node, {
required CompilationUnit unit,
}) : this._(node: node, unit: unit);

@override
final CompilationUnit unit;
@override
final ClassDeclaration node;

Expand All @@ -198,11 +223,18 @@ class ConsumerStatefulWidgetDeclaration extends ConsumerDeclaration {
}

class StatefulHookConsumerWidgetDeclaration extends ConsumerDeclaration {
StatefulHookConsumerWidgetDeclaration._({required this.node});
StatefulHookConsumerWidgetDeclaration._({
required this.node,
required this.unit,
});

StatefulHookConsumerWidgetDeclaration.parse(ClassDeclaration node)
: this._(node: node);
StatefulHookConsumerWidgetDeclaration.parse(
ClassDeclaration node, {
required CompilationUnit unit,
}) : this._(node: node, unit: unit);

@override
final CompilationUnit unit;
@override
final ClassDeclaration node;

Expand All @@ -218,27 +250,33 @@ class StatefulHookConsumerWidgetDeclaration extends ConsumerDeclaration {
class ConsumerStateDeclaration extends ConsumerDeclaration {
ConsumerStateDeclaration._({
required this.node,
required this.unit,
});

static ConsumerStateDeclaration? _parse(
ClassDeclaration node,
_ParseRefInvocationMixin parent,
) {
_ParseRefInvocationMixin parent, {
required CompilationUnit unit,
}) {
final consumerWidgetDeclaration = ConsumerStateDeclaration._(
node: node,
unit: unit,
);
final visitor = _ParseConsumerRefInvocationVisitor(
consumerWidgetDeclaration,
consumerWidgetDeclaration.widgetRefInvocations,
consumerWidgetDeclaration.providerScopeInstanceCreateExpressions,
parent,
unit: unit,
);

node.accept(visitor);

return consumerWidgetDeclaration;
}

@override
final CompilationUnit unit;
final List<WidgetRefInvocation> widgetRefInvocations = [];
final List<ProviderScopeInstanceCreationExpression>
providerScopeInstanceCreateExpressions = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ extension on LibraryElement {
}

abstract class GeneratorProviderDeclaration extends ProviderDeclaration {
@override
CompilationUnit get unit;

@override
GeneratorProviderDeclarationElement get providerElement;
RiverpodAnnotation get annotation;
Expand Down Expand Up @@ -187,15 +190,17 @@ class ClassBasedProviderDeclaration extends GeneratorProviderDeclaration {
required this.createdTypeNode,
required this.exposedTypeNode,
required this.valueTypeNode,
required this.unit,
});

static ClassBasedProviderDeclaration? _parse(
ClassDeclaration node,
_ParseRefInvocationMixin parent,
) {
_ParseRefInvocationMixin parent, {
required CompilationUnit unit,
}) {
final element = node.declaredElement;
if (element == null) return null;
final riverpodAnnotation = RiverpodAnnotation._parse(node);
final riverpodAnnotation = RiverpodAnnotation._parse(node, unit: unit);
if (riverpodAnnotation == null) return null;

final buildMethod = node.members
Expand Down Expand Up @@ -237,15 +242,22 @@ class ClassBasedProviderDeclaration extends GeneratorProviderDeclaration {
createdTypeNode: createdTypeNode,
exposedTypeNode: exposedTypeNode,
valueTypeNode: valueTypeNode,
unit: unit,
);
riverpodAnnotation._parent = classBasedProviderDeclaration;
node.accept(
_GeneratorRefInvocationVisitor(classBasedProviderDeclaration, parent),
_GeneratorRefInvocationVisitor(
classBasedProviderDeclaration,
parent,
unit: unit,
),
);

return classBasedProviderDeclaration;
}

@override
final CompilationUnit unit;
@override
final Token name;
@override
Expand Down Expand Up @@ -276,7 +288,14 @@ class ClassBasedProviderDeclaration extends GeneratorProviderDeclaration {

class _GeneratorRefInvocationVisitor extends RecursiveAstVisitor<void>
with _ParseRefInvocationMixin {
_GeneratorRefInvocationVisitor(this.declaration, this.parent);
_GeneratorRefInvocationVisitor(
this.declaration,
this.parent, {
required this.unit,
});

@override
final CompilationUnit unit;

final GeneratorProviderDeclaration declaration;
final _ParseRefInvocationMixin parent;
Expand Down Expand Up @@ -316,15 +335,20 @@ class FunctionalProviderDeclaration extends GeneratorProviderDeclaration {
required this.createdTypeNode,
required this.exposedTypeNode,
required this.valueTypeNode,
required this.unit,
});

static FunctionalProviderDeclaration? _parse(
FunctionDeclaration node,
_ParseRefInvocationMixin parent,
) {
_ParseRefInvocationMixin parent, {
required CompilationUnit unit,
}) {
final element = node.declaredElement;
if (element == null) return null;
final riverpodAnnotation = RiverpodAnnotation._parse(node);
final riverpodAnnotation = RiverpodAnnotation._parse(
node,
unit: unit,
);
if (riverpodAnnotation == null) return null;

final providerElement = FunctionalProviderDeclarationElement.parse(
Expand All @@ -349,17 +373,23 @@ class FunctionalProviderDeclaration extends GeneratorProviderDeclaration {
createdTypeNode: createdTypeNode,
exposedTypeNode: exposedTypeNode,
valueTypeNode: _getValueType(createdTypeNode, element.library),
unit: unit,
);
riverpodAnnotation._parent = functionalProviderDeclaration;
node.accept(
_GeneratorRefInvocationVisitor(functionalProviderDeclaration, parent),
_GeneratorRefInvocationVisitor(
functionalProviderDeclaration,
parent,
unit: unit,
),
);
return functionalProviderDeclaration;
}

@override
final CompilationUnit unit;
@override
final Token name;

@override
final FunctionDeclaration node;
@override
Expand Down
Loading