This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use dedicated MediaQuery methods instead of depending on al…
…l MediaQuery data
- Loading branch information
1 parent
13b44c6
commit d8928b9
Showing
4 changed files
with
76 additions
and
3 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
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
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
66 changes: 66 additions & 0 deletions
66
src/kaiteki_lints/lib/rules/prefer_media_query_aspects.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,66 @@ | ||
import 'package:analyzer/error/error.dart'; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
const _typeChecker = | ||
TypeChecker.fromName('MediaQueryData', packageName: 'flutter'); | ||
|
||
class PreferMediaQueryAspectsRule extends DartLintRule { | ||
const PreferMediaQueryAspectsRule() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'prefer_media_query_aspects', | ||
problemMessage: | ||
'Prefer calling MediaQuery method that depend on only one aspect', | ||
); | ||
|
||
static const mediaQueryAspects = { | ||
"size": "sizeOf", | ||
"orientation": "orientationOf", | ||
"devicePixelRatio": "devicePixelRatioOf", | ||
"textScaler": "textScalerOf", | ||
"textScaleFactor": "textScaleFactorOf", | ||
"platformBrightness": "platformBrightnessOf", | ||
"boldText": "boldTextOf", | ||
"highContrast": "highContrastOf", | ||
"disableAnimations": "disableAnimationsOf", | ||
"invertColors": "invertColorsOf", | ||
"alwaysUse24HourFormat": "alwaysUse24HourFormatOf", | ||
"accessibleNavigation": "accessibleNavigationOf", | ||
"navigationMode": "navigationModeOf", | ||
"displayFeatures": "displayFeaturesOf", | ||
"gestureSettings": "gestureSettingsOf", | ||
"viewPadding": "viewPaddingOf", | ||
"systemGestureInsets": "systemGestureInsetsOf", | ||
"viewInsets": "viewInsetsOf", | ||
"padding": "paddingOf", | ||
}; | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ErrorReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
// Detect access to e.g. MediaQuery.of(context).size and prompt to use MediaQuery.sizeOf(context) instead. | ||
context.registry.addPropertyAccess((node) { | ||
var propertyName = node.propertyName.name; | ||
final methodName = mediaQueryAspects[propertyName]; | ||
if (methodName == null) return; | ||
|
||
final targetType = node.target?.staticType; | ||
if (targetType == null) return; | ||
if (!_typeChecker.isAssignableFromType(targetType)) return; | ||
|
||
reporter.reportErrorForNode( | ||
LintCode( | ||
name: 'prefer_media_query_aspects', | ||
problemMessage: | ||
'Prefer calling MediaQuery.$methodName(context) instead of MediaQuery.of(context).$propertyName', | ||
errorSeverity: ErrorSeverity.WARNING, | ||
), | ||
node, | ||
); | ||
}); | ||
} | ||
} |