go_router_linter library
go_router_linter
The go_router_linter
package provides custom lint rules to improve code
quality and maintain consistency when using the go_router
package in
Dart/Flutter projects.
Key Features
-
Avoid Hardcoded Routes (
avoid_hardcoded_routes
): Detects hardcoded route strings incontext.go()
,context.push()
and related methods, as well as inGoRoute
definitions. Encourages the use of constants or enums. -
Ensure
GoRoute
Includes aname
Property (missing_go_route_name_property
): Ensures everyGoRoute
has aname
property, improving readability and maintainability. -
Use
context.go()
Instead ofGoRouter.of(context).go()
(use_context_directly_for_go_router
): Suggests the more concise and idiomaticcontext.go()
calls.
Structure
-
Extensions:
lint_code_extension.dart
: Provides acopyWith()
method for creating modified copies ofLintCode
instances.route_methods_extension.dart
: Adds a helper extension onString
to easily identify route-related methods (go
,push
,goNamed
,pushNamed
, etc.).
-
Rules:
avoid_hardcoded_routes.dart
: Checks for hardcoded route strings in route methods andGoRoute
definitions.missing_go_route_name_property.dart
: EnsuresGoRoute
definitions include aname
.use_context_directly_for_go_router.dart
: Advises usingcontext.go()
instead ofGoRouter.of(context).go()
.
-
Plugin Entrypoint:
go_router_linter.dart
: Defines the plugin and registers all available lint rules.
Getting Started
To use these lint rules, add go_router_linter
as a dev dependency and
configure it in your analysis_options.yaml
:
analyzer:
plugins:
- custom_lint
custom_lint:
rules:
- missing_go_route_name_property
- use_context_directly_for_go_router
- avoid_hardcoded_routes
Classes
- AvoidHardcodedRoutes
- A lint rule that checks for hardcoded route strings.
- MissingGoRouteNameProperty
-
A lint rule that checks if GoRoute definitions include a
name
property. - UseContextDirectlyForGoRouter
-
A custom lint rule that checks for the use of
GoRouter.of(context).go()
instead ofcontext.go()
.
Extensions
- LintCodeCopyWithExtension on LintCode
-
An extension that provides a method for creating modified copies of a
LintCode
. - RouteMethodExtension on String
- A way to add extensions to Strings to make it easy to identify the methods involved in routing (go, push, goNamed, pushNamed, etc.). This simplifies the methodName comparison logic in existing code and makes it easy to extend as new route methods are added.
Functions
-
createPlugin(
) → PluginBase - This is the entrypoint of our custom linter