Skip to content

Commit

Permalink
feat: Add prefer_dedicated_media_query_methods rule (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
naipaka authored Jul 29, 2024
1 parent 1534bff commit 945d5e8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
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 @@ -5,6 +5,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';
import 'src/lints/prefer_space_between_elements.dart';

PluginBase createPlugin() => _AltivePlugin();
Expand All @@ -17,6 +18,7 @@ class _AltivePlugin extends PluginBase {
const AvoidHardcodedJapanese(),
const AvoidShrinkWrapInListView(),
const AvoidSingleChild(),
const PreferDedicatedMediaQueryMethods(),
const PreferSpaceBetweenElements(),
];
}
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'),
],
);
}
}

0 comments on commit 945d5e8

Please sign in to comment.