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

feat: Add prefer_dedicated_media_query_methods rule #34

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/altive_lints/lib/altive_lints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'src/lints/avoid_hardcoded_color.dart';
import 'src/lints/avoid_hardcoded_japanese.dart';
import 'src/lints/avoid_shrink_wrap_in_list_view.dart';
import 'src/lints/avoid_single_child.dart';
import 'src/lints/prefer_dedicated_media_query_methods.dart';

PluginBase createPlugin() => _AltivePlugin();

Expand All @@ -14,5 +15,6 @@ class _AltivePlugin extends PluginBase {
const AvoidHardcodedJapanese(),
const AvoidShrinkWrapInListView(),
const AvoidSingleChild(),
const PreferDedicatedMediaQueryMethods(),
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';

/// A `prefer_dedicated_media_query_methods` rule that encourages
/// the use of dedicated `MediaQuery` methods instead of
/// the generic `MediaQuery.of` or `MediaQuery.maybeOf`.
///
/// Using specialized methods like `MediaQuery.sizeOf` or
/// `MediaQuery.viewInsetsOf` improves performance by reducing
/// unnecessary widget rebuilds.
///
/// These methods directly access specific properties,
/// avoiding the overhead associated with the broader
/// context changes that trigger when using `MediaQuery.of`.
///
/// ### Example
///
/// #### BAD:
///
/// ```dart
/// var size = MediaQuery.of(context).size; // LINT
/// var padding = MediaQuery.maybeOf(context)?.padding; // LINT
/// ```
///
/// #### GOOD:
///
/// ```dart
/// var size = MediaQuery.sizeOf(context);
/// var padding = MediaQuery.viewInsetsOf(context);
/// ```
class PreferDedicatedMediaQueryMethods extends DartLintRule {
const PreferDedicatedMediaQueryMethods() : super(code: _code);

static const _code = LintCode(
name: 'prefer_dedicated_media_query_methods',
problemMessage: 'Prefer using dedicated MediaQuery methods instead of '
'MediaQuery.of or MediaQuery.maybeOf.',
correctionMessage: 'Consider using methods like MediaQuery.sizeOf or '
'MediaQuery.viewInsetsOf.',
);

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
context.registry.addMethodInvocation((node) {
final method = node.methodName.name;
final target = node.target?.toString();
if (target == 'MediaQuery' && (method == 'of' || method == 'maybeOf')) {
reporter.reportErrorForNode(_code, node);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';

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

@override
Widget build(BuildContext context) {
// expect_lint: prefer_dedicated_media_query_methods
final size = MediaQuery.of(context).size;
// expect_lint: prefer_dedicated_media_query_methods
final viewInsets = MediaQuery.of(context).viewInsets;
return Column(
children: [
Text('Size: $size'),
Text('ViewInsets: $viewInsets'),
],
);
}
}