-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
prefer_dedicated_media_query_methods
rule (#34)
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/altive_lints/lib/src/lints/prefer_dedicated_media_query_methods.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/altive_lints/lint_test/lints/prefer_dedicated_media_query_methods.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
], | ||
); | ||
} | ||
} |