Skip to content

Commit

Permalink
Merge pull request #54 from nubank/fix-analyzer-bump
Browse files Browse the repository at this point in the history
Fix problems when using recent versions of analyzer
  • Loading branch information
leoiacovini authored Oct 27, 2020
2 parents d8c9cde + 9397da9 commit 8000d17
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 41 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 0.7.2
- Fix problems caused by bump in `analyzer` dependency

## 0.7.1
- Update README
- Update pubspec.yaml to support analyzer up to version `0.41.0`
Expand Down
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#
# This file contains the analysis options used by Flutter tools, such as IntelliJ,
# Android Studio, and the `flutter analyze` command.
include: package:pedantic/analysis_options.yaml

analyzer:
strong-mode:
Expand Down
1 change: 1 addition & 0 deletions example/lib/samples/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class HomeScreen extends StatelessWidget {
}

if (text != null) {
// ignore: unawaited_futures
showDialog<void>(
context: context,
builder: (context) => AlertDialog(
Expand Down
1 change: 0 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ dev_dependencies:
sdk: flutter

flutter:

uses-material-design: true
2 changes: 1 addition & 1 deletion lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NuvigatorGenerator extends GeneratorForAnnotation<NuRouterAnnotation> {
FutureOr<String> generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) {
if (element is ClassElement) {
final ClassElement classElement = element;
final classElement = element;
final lib = BuilderLibrary(classElement).build();
return DartFormatter().format(lib.accept(DartEmitter()).toString());
}
Expand Down
4 changes: 2 additions & 2 deletions lib/builder/args_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class ArgsClass extends BaseBuilder {

String _safelyCastArg(ParameterElement arg, MethodElement method) {
final varName = arg.name.toString();
final typeName = arg.type.toString();
final typeName = arg.type.getDisplayString(withNullability: false);
final nuRouteFieldAnnotation =
nuRouteChecker.firstAnnotationOfExact(method);

Expand Down Expand Up @@ -166,7 +166,7 @@ class ArgsClass extends BaseBuilder {

for (final arg in method.parameters) {
final varName = arg.name.toString();
final typeName = arg.type.toString();
final typeName = arg.type.getDisplayString(withNullability: false);

argsParserBuffer.write('$varName: ${_safelyCastArg(arg, method)},\n');

Expand Down
4 changes: 2 additions & 2 deletions lib/builder/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ String getRouterName(Element element) {
}

String getRouteString(ClassElement routerElement, MethodElement element) {
String prefix = nuRouterChecker
var prefix = nuRouterChecker
.firstAnnotationOfExact(routerElement)
?.getField('routeNamePrefix')
?.toStringValue();
prefix = prefix != null && prefix.isNotEmpty ? lowerCamelCase(prefix) : '';

String routerName = getRouterName(routerElement);
var routerName = getRouterName(routerElement);
final needsFormat = prefix.isEmpty || prefix.endsWith('/');
routerName = needsFormat ? lowerCamelCase(routerName) : routerName;

Expand Down
12 changes: 7 additions & 5 deletions lib/builder/navigation_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ class NavigationExtension extends BaseBuilder {
for (final arg in method.parameters) {
final argName = arg.name.toString();
final isRequired = arg.metadata.isNotEmpty &&
arg.metadata.firstWhere((e) => e.isRequired, orElse: null) != null;
arg.metadata.firstWhere((e) => e.isRequired, orElse: () => null) !=
null;
parameters.add(
Parameter(
(p) => p
..name = argName
..annotations.addAll(isRequired ? [refer('required')] : [])
..named = true
..type = refer(arg.type.toString()),
..type = refer(arg.type.getDisplayString(withNullability: false)),
),
);
argumentsMapBuffer.write("'$argName': $argName,");
Expand Down Expand Up @@ -178,8 +179,9 @@ class NavigationExtension extends BaseBuilder {

if (nuRouteFieldAnnotation != null) {
final generics = getGenericTypes(method.returnType);
final screenReturn =
generics.length > 1 ? generics[1].name : generics.first.name;
final screenReturn = generics.length > 1
? generics[1].getDisplayString(withNullability: false)
: generics.first.getDisplayString(withNullability: false);
final pushMethods =
nuRouteFieldAnnotation.getField('pushMethods').toListValue();
if (pushMethods != null) {
Expand Down Expand Up @@ -222,7 +224,7 @@ class NavigationExtension extends BaseBuilder {
nuRouterChecker.firstAnnotationOfExact(field);
if (nuSubRouterAnnotation != null) {
methods.add(
_subRouterMethod(field.type.name),
_subRouterMethod(field.type.getDisplayString(withNullability: false)),
);
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/src/nurouter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ abstract class NuRouter {
final screen = _getScreen(settings);
if (screen != null) return screen;

for (NuRouter router in routers) {
for (final router in routers) {
final screen = router.getScreen(settings);
if (screen != null) return screen.wrapWith(screensWrapper);
}
Expand All @@ -107,7 +107,7 @@ abstract class NuRouter {
if (prefixRegex.hasMatch(deepLink)) {
final routeEntry = _getRouteEntryForDeepLink(deepLink);
if (routeEntry != null) return routeEntry;
for (final NuRouter router in routers) {
for (final router in routers) {
final newDeepLink = deepLink.replaceFirst(thisDeepLinkPrefix, '');
final subRouterEntry = router.getRouteEntryForDeepLink(newDeepLink);
if (subRouterEntry != null) {
Expand Down Expand Up @@ -137,8 +137,9 @@ abstract class NuRouter {
final routeEntry = getRouteEntryForDeepLink(deepLinkString(url));

if (routeEntry == null) {
if (onDeepLinkNotFound != null)
if (onDeepLinkNotFound != null) {
return await onDeepLinkNotFound(this, url, isFromNative, arguments);
}
return null;
}

Expand Down
49 changes: 26 additions & 23 deletions lib/src/nuvigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ NuvigatorState _tryToFindNuvigatorForRouter<T extends NuRouter>(
if (nuvigatorState == null) return null;
final nuvigatorRouterForType = nuvigatorState.router.getRouter<T>();
if (nuvigatorRouterForType != null) return nuvigatorState;
if (nuvigatorState != nuvigatorState.parent && nuvigatorState.parent != null)
if (nuvigatorState != nuvigatorState.parent &&
nuvigatorState.parent != null) {
return _tryToFindNuvigatorForRouter<T>(nuvigatorState.parent);
}
return null;
}

Expand Down Expand Up @@ -143,8 +145,7 @@ class Nuvigator<T extends NuRouter> extends Navigator {
}

static NuvigatorState ofRouter<T extends NuRouter>(BuildContext context) {
final NuvigatorState closestNuvigator =
context.findAncestorStateOfType<NuvigatorState>();
final closestNuvigator = context.findAncestorStateOfType<NuvigatorState>();
return _tryToFindNuvigatorForRouter<T>(closestNuvigator);
}

Expand All @@ -153,8 +154,9 @@ class Nuvigator<T extends NuRouter> extends Navigator {
bool rootNuvigator = false,
bool nullOk = false,
}) {
if (rootNuvigator)
if (rootNuvigator) {
return context.findRootAncestorStateOfType<NuvigatorState<T>>();
}
final nuvigatorState = ofRouter<T>(context);
if (nuvigatorState is NuvigatorState<T>) return nuvigatorState;
assert(() {
Expand Down Expand Up @@ -250,84 +252,85 @@ class NuvigatorState<T extends NuRouter> extends NavigatorState
@override
Future<bool> didPushRoute(String route) async {
assert(mounted);
// ignore: unawaited_futures
pushNamed(route);
return true;
}

@override
Future<T> pushNamed<T extends Object>(String routeName, {Object arguments}) {
Future<R> pushNamed<R extends Object>(String routeName, {Object arguments}) {
final possibleRoute =
router.getScreen(RouteSettings(name: routeName, arguments: arguments));
if (possibleRoute == null && parent != null) {
return parent.pushNamed<T>(routeName, arguments: arguments);
return parent.pushNamed<R>(routeName, arguments: arguments);
}
return super.pushNamed<T>(routeName, arguments: arguments);
return super.pushNamed<R>(routeName, arguments: arguments);
}

@override
Future<T> pushReplacementNamed<T extends Object, TO extends Object>(
Future<R> pushReplacementNamed<R extends Object, TO extends Object>(
String routeName,
{Object arguments,
TO result}) {
final possibleRoute =
router.getScreen(RouteSettings(name: routeName, arguments: arguments));
if (possibleRoute == null) {
return parent.pushReplacementNamed<T, TO>(routeName,
return parent.pushReplacementNamed<R, TO>(routeName,
arguments: arguments, result: result);
}
return super.pushReplacementNamed<T, TO>(routeName,
return super.pushReplacementNamed<R, TO>(routeName,
arguments: arguments, result: result);
}

@override
Future<T> pushNamedAndRemoveUntil<T extends Object>(
Future<R> pushNamedAndRemoveUntil<R extends Object>(
String newRouteName, RoutePredicate predicate,
{Object arguments}) {
final possibleRoute = router
.getScreen(RouteSettings(name: newRouteName, arguments: arguments));
if (possibleRoute == null) {
return parent.pushNamedAndRemoveUntil<T>(newRouteName, predicate,
return parent.pushNamedAndRemoveUntil<R>(newRouteName, predicate,
arguments: arguments);
}
return super.pushNamedAndRemoveUntil<T>(newRouteName, predicate,
return super.pushNamedAndRemoveUntil<R>(newRouteName, predicate,
arguments: arguments);
}

@override
Future<T> popAndPushNamed<T extends Object, TO extends Object>(
Future<R> popAndPushNamed<R extends Object, TO extends Object>(
String routeName,
{Object arguments,
TO result}) {
final possibleRoute =
router.getScreen(RouteSettings(name: routeName, arguments: arguments));
if (possibleRoute == null) {
return parent.popAndPushNamed<T, TO>(routeName,
return parent.popAndPushNamed<R, TO>(routeName,
arguments: arguments, result: result);
}
return super.popAndPushNamed<T, TO>(routeName,
return super.popAndPushNamed<R, TO>(routeName,
arguments: arguments, result: result);
}

@override
void pop<T extends Object>([T result]) {
void pop<R extends Object>([R result]) {
var isPopped = false;
if (canPop()) {
isPopped = super.canPop();
super.pop<T>(result);
super.pop<R>(result);
} else if (widget.shouldPopRoot && this == rootNuvigator) {
isPopped = true;
SystemNavigator.pop();
}
if (!isPopped && this != rootNuvigator && parent != null) {
parentPop<T>(result);
parentPop<R>(result);
}
}

void parentPop<T extends Object>([T result]) => parent.pop<T>(result);
void parentPop<R extends Object>([R result]) => parent.pop<R>(result);

void rootPop<T extends Object>([T result]) => rootNuvigator.pop<T>(result);
void rootPop<R extends Object>([R result]) => rootNuvigator.pop<R>(result);

void closeFlow<T extends Object>([T result]) {
void closeFlow<R extends Object>([R result]) {
if (isNested) {
parentPop(result);
}
Expand All @@ -347,7 +350,7 @@ class NuvigatorState<T extends NuRouter> extends NavigatorState

@override
Widget build(BuildContext context) {
Widget child = super.build(context);
var child = super.build(context);
if (widget.wrapper != null) {
child = widget.wrapper(context, child);
}
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: nuvigator
description: A powerful and strongly typed routing abstraction over Flutter navigator, providing some new features and an easy way to define routers with code generation.
version: 0.7.1
version: 0.7.2

homepage: https://github.com/nubank/nuvigator

Expand All @@ -21,6 +21,7 @@ dependencies:

dev_dependencies:
mockito: ^4.1.1
pedantic: ^1.9.0
build_runner: ^1.6.5
flutter_test:
sdk: flutter
3 changes: 2 additions & 1 deletion test/fixtures/test_router.g.dart

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

2 changes: 2 additions & 0 deletions test/navigation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void main() {
(WidgetTester tester) async {
final router = TestRouter();
await helpers.pumpApp(tester, router, TestRoutes.home);
// ignore: unawaited_futures
router.openDeepLink<void>(Uri.parse('exapp://testargs'));
await tester.pumpAndSettle();
expect(find.text('intArg: null'), findsOneWidget);
Expand All @@ -16,6 +17,7 @@ void main() {
(WidgetTester tester) async {
final router = TestRouter();
await helpers.pumpApp(tester, router, TestRoutes.home);
// ignore: unawaited_futures
router.openDeepLink<void>(Uri.parse(
'exapp://testargs?intArg=42&doubleArg=-4.2&boolArg=true&dateTimeArg=2020-07-07T12:34:00.000Z&dateArg=2020-08-07&stringArg=testing'));
await tester.pumpAndSettle();
Expand Down
4 changes: 2 additions & 2 deletions test/router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void main() {

test('router onScreenNotFound', () {
final testRouter = TestRouter();
bool notFound = false;
var notFound = false;
testRouter.onScreenNotFound = (settings) {
notFound = true;
return ScreenRoute(debugKey: 'screenNotFound', builder: (_) => null);
Expand Down Expand Up @@ -44,7 +44,7 @@ void main() {

test('router on deepLinkNotFound', () async {
final testRouter = TestRouter();
bool notFound = false;
var notFound = false;
final mockNuvigator = MockNuvigator(testRouter);
testRouter.nuvigator = mockNuvigator;
testRouter.onDeepLinkNotFound = (router, deepLink, [_, dynamic __]) async {
Expand Down
1 change: 1 addition & 0 deletions test/wrapper_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void main() {
final router = TestRouter();
await helpers.pumpApp(tester, router, TestRoutes.home);

// ignore: unawaited_futures
router.openDeepLink<void>(Uri.parse('exapp://testargs'));
await tester.pumpAndSettle();

Expand Down

0 comments on commit 8000d17

Please sign in to comment.