Skip to content

Commit

Permalink
Add flutter_lints (#414)
Browse files Browse the repository at this point in the history
Adds flutter_lints, removes duplicate rules, and addresses linting
issues. See #232 for motivation.

This includes a
[workaround](mobxjs/mobx.dart#809) for mobx
stores.

Also, while the uuid package was already imported in
fullscreenable_image.dart, it was not in pubspec.yaml.

Lastly, there are some issues with the use_build_context_synchronously
rule. For the files most heavily impacted, I have added a temporary
ignore rule as the linting fixes are not currently in the Flutter SDK.
See: https://github.com/dart-lang/linter/issues/4007
  • Loading branch information
pattersonjack authored Jul 17, 2023
1 parent bfdd457 commit 58263be
Show file tree
Hide file tree
Showing 81 changed files with 281 additions and 210 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ From issues to wikis: everything is on [GitHub](https://github.com/liftoff-app/l

## Linting / Formatting

Everything is formatted with `dart format` (no flags) and linted with `dart analyze` ([see rules](analysis_options.yaml)). Both are enforced by the CI.
Everything is formatted with `dart format` (no flags) and linted with `flutter analyze` ([see rules](analysis_options.yaml)). Both are enforced by the CI.

## Translations

Expand Down
71 changes: 2 additions & 69 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,127 +1,60 @@
include: package:flutter_lints/flutter.yaml

linter:
rules:
- annotate_overrides
- avoid_bool_literals_in_conditional_expressions
- avoid_catching_errors
- avoid_equals_and_hash_code_on_mutable_classes
- avoid_escaping_inner_quotes
- avoid_final_parameters
- avoid_function_literals_in_foreach_calls
- avoid_init_to_null
- avoid_null_checks_in_equality_operators
- avoid_positional_boolean_parameters
- avoid_print
- avoid_private_typedef_functions
- avoid_redundant_argument_values
- avoid_relative_lib_imports
- avoid_return_types_on_setters
- avoid_returning_null
- avoid_returning_null_for_void
- avoid_returning_this
- avoid_setters_without_getters
- avoid_single_cascade_in_expression_statements
- avoid_type_to_string
- avoid_unnecessary_containers
- avoid_unused_constructor_parameters
- avoid_void_async
- await_only_futures
- camel_case_extensions
- camel_case_types
- cascade_invocations
- cast_nullable_to_non_nullable
- constant_identifier_names
- conditional_uri_does_not_exist
- curly_braces_in_flow_control_structures
- directives_ordering
- empty_catches
- empty_constructor_bodies
- eol_at_end_of_file
- exhaustive_cases
- file_names
- hash_and_equals
- implementation_imports
- library_names
- library_prefixes
- literal_only_boolean_expressions
- non_constant_identifier_names
- noop_primitive_operations
- no_leading_underscores_for_library_prefixes
- no_leading_underscores_for_local_identifiers
- null_check_on_nullable_type_parameter
- omit_local_variable_types
- one_member_abstracts
- package_api_docs
- parameter_assignments
- prefer_adjacent_string_concatenation
- prefer_asserts_in_initializer_lists
- prefer_collection_literals
- prefer_conditional_assignment
- prefer_const_constructors
- prefer_const_constructors_in_immutables
- prefer_const_declarations
- prefer_const_literals_to_create_immutables
- prefer_constructors_over_static_methods
- prefer_contains
- prefer_final_fields
- prefer_final_in_for_each
- prefer_final_locals
- prefer_for_elements_to_map_fromIterable
- prefer_function_declarations_over_variables
- prefer_generic_function_type_aliases
- prefer_if_elements_to_conditional_expressions
- prefer_if_null_operators
- prefer_initializing_formals
- prefer_int_literals
- prefer_interpolation_to_compose_strings
- prefer_is_empty
- prefer_is_not_empty
- prefer_is_not_operator
- prefer_iterable_whereType
- prefer_mixin
- prefer_null_aware_operators
- prefer_relative_imports
- prefer_single_quotes
- prefer_spread_collections
- prefer_typing_uninitialized_variables
- recursive_getters
- secure_pubspec_urls
- sized_box_for_whitespace
- sized_box_shrink_expand
- slash_for_doc_comments
- sort_child_properties_last
- sort_unnamed_constructors_first
- tighten_type_of_initializing_formals
- type_annotate_public_apis
- type_init_formals
- unawaited_futures
- unnecessary_brace_in_string_interps
- unnecessary_const
- unnecessary_constructor_name
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_late
- unnecessary_new
- unnecessary_null_aware_assignments
- unnecessary_null_checks
- unnecessary_null_in_if_null_operators
- unnecessary_parenthesis
- unnecessary_raw_strings
- unnecessary_string_escapes
- unnecessary_string_interpolations
- unnecessary_this
- unrelated_type_equality_checks
- use_colored_box
- use_enums
- use_full_hex_values_for_flutter_colors
- use_is_even_rather_than_modulo
- use_named_constants
- use_raw_strings
- use_rethrow_when_possible
- use_setters_to_change_properties
- use_super_parameters
- use_test_throws_matchers
- use_to_and_as_if_applicable
- void_checks

analyzer:
exclude:
Expand Down
2 changes: 1 addition & 1 deletion lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final l10nDelegates = [
];

class MyApp extends StatelessWidget {
const MyApp();
const MyApp({super.key});

@override
Widget build(BuildContext context) {
Expand Down
2 changes: 1 addition & 1 deletion lib/app_link_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import './util/goto.dart';
/// - liftoff://programming.dev/c/programmer_humor
/// - liftoff://lemmy.world/u/zachatrocity
class AppLinkHandler extends HookWidget {
AppLinkHandler(this.child);
AppLinkHandler(this.child, {super.key});

final Widget child;
final _appLinks = AppLinks();
Expand Down
2 changes: 1 addition & 1 deletion lib/comment_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class CommentTree {
}

/// recursive sorter
void _sort(int compare(CommentTree a, CommentTree b)) {
void _sort(int Function(CommentTree a, CommentTree b) compare) {
children.sort(compare);
for (final el in children) {
el._sort(compare);
Expand Down
2 changes: 1 addition & 1 deletion lib/hooks/stores.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:provider/provider.dart';
import '../stores/accounts_store.dart';

AccountsStore useAccountsStore() => useContext().watch<AccountsStore>();
T useAccountsStoreSelect<T>(T selector(AccountsStore store)) =>
T useAccountsStoreSelect<T>(T Function(AccountsStore store) selector) =>
useContext().select<AccountsStore, T>(selector);

V useStore<S extends Store, V>(V Function(S value) selector) {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/communities_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import 'instance/instance.dart';

/// List of subscribed communities per instance
class CommunitiesTab extends HookWidget {
const CommunitiesTab();
const CommunitiesTab({super.key});

@override
Widget build(BuildContext context) {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/community/community.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import 'community_store.dart';

/// Displays posts, comments, and general info about the given community
class CommunityPage extends HookWidget {
const CommunityPage();
const CommunityPage({super.key});

@override
Widget build(BuildContext context) {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/community/community_follow_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'community_store.dart';
class CommunityFollowButton extends HookWidget {
final CommunityView communityView;

const CommunityFollowButton(this.communityView);
const CommunityFollowButton(this.communityView, {super.key});

@override
Widget build(BuildContext context) {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/community/community_overview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'community_follow_button.dart';
class CommunityOverview extends StatelessWidget {
final FullCommunityView fullCommunityView;

const CommunityOverview(this.fullCommunityView);
const CommunityOverview(this.fullCommunityView, {super.key});

@override
Widget build(BuildContext context) {
Expand Down
11 changes: 10 additions & 1 deletion lib/pages/community/community_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import '../../util/async_store.dart';

part 'community_store.g.dart';

class CommunityStore = _CommunityStore with _$CommunityStore;
class CommunityStore extends _CommunityStore with _$CommunityStore {
CommunityStore.fromId({required super.id, required super.instanceHost})
: super.fromId();

static CommunityStore fromName(
{required String communityName, required String instanceHost}) {
throw Exception('CommunityStore could not be created');
}
}

abstract class _CommunityStore with Store {
final String instanceHost;
Expand All @@ -18,6 +26,7 @@ abstract class _CommunityStore with Store {
required String this.communityName,
required this.instanceHost,
}) : id = null;

// ignore: unused_element
_CommunityStore.fromId({required this.id, required this.instanceHost})
: communityName = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/create_post/create_post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import 'create_post_url_field.dart';
/// Modal for creating a post to some community in some instance
/// Pops the navigator stack with a [PostView]
class CreatePostPage extends HookWidget {
const CreatePostPage();
const CreatePostPage({super.key});

@override
Widget build(BuildContext context) {
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/create_post/create_post_fab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'create_post.dart';
class CreatePostFab extends HookWidget {
final CommunityView? community;

const CreatePostFab({this.community});
const CreatePostFab({super.key, this.community});

@override
Widget build(BuildContext context) {
Expand All @@ -25,7 +25,7 @@ class CreatePostFab extends HookWidget {
: CreatePostPage.toCommunityRoute(community!),
);

if (postView != null) {
if (postView != null && context.mounted) {
await Navigator.of(context)
.push(FullPostPage.fromPostViewRoute(postView));
}
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/create_post/create_post_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import '../../util/pictrs.dart';

part 'create_post_store.g.dart';

class CreatePostStore = _CreatePostStore with _$CreatePostStore;
class CreatePostStore extends _CreatePostStore with _$CreatePostStore {
CreatePostStore(
{required super.instanceHost, super.postToEdit, super.selectedCommunity});
}

abstract class _CreatePostStore with Store {
final Post? postToEdit;
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/create_post/create_post_url_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'create_post_store.dart';
class CreatePostUrlField extends HookWidget {
final FocusNode titleFocusNode;

const CreatePostUrlField(this.titleFocusNode);
const CreatePostUrlField(this.titleFocusNode, {super.key});

@override
Widget build(BuildContext context) {
Expand All @@ -29,7 +29,7 @@ class CreatePostUrlField extends HookWidget {
);

// pic is null when the picker was cancelled
if (pic != null) {
if (pic != null && context.mounted) {
await context.read<CreatePostStore>().uploadImage(pic.path, userData);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/display_document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DisplayDocumentPage extends StatelessWidget {
link: href!,
context: context,
);
if (didLaunch) {
if (didLaunch && context.mounted) {
Navigator.of(context).pop();
}
},
Expand Down
1 change: 1 addition & 0 deletions lib/pages/federation_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FederationResolver extends HookWidget {
builder;

const FederationResolver({
super.key,
required this.userData,
required this.query,
required this.loadingMessage,
Expand Down
13 changes: 12 additions & 1 deletion lib/pages/full_post/full_post_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ import '../../widgets/post/post_store.dart';

part 'full_post_store.g.dart';

class FullPostStore = _FullPostStore with _$FullPostStore;
class FullPostStore extends _FullPostStore with _$FullPostStore {
FullPostStore(
{required super.postId, required super.instanceHost, super.commentId});

static FullPostStore fromPostStore(PostStore postStore) {
throw Exception('FullPostStore could not be created');
}

static FullPostStore fromPostView(PostView postView, {int? commentId}) {
throw Exception('FullPostStore could not be created');
}
}

abstract class _FullPostStore with Store {
final int postId;
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'profile_tab.dart';
import 'search_tab.dart';

class HomePage extends HookWidget {
const HomePage();
const HomePage({super.key});

static const List<Widget> pages = [
HomeTab(),
Expand Down
Loading

0 comments on commit 58263be

Please sign in to comment.