Skip to content

Setting Up Custom Lint Rules

What you'll learn

  • Generating a custom lint rules package for your project
  • Understanding how the Dart analyzer plugin system works
  • Enforcing architecture conventions automatically
  • Adding project-specific lint rules

Prerequisites

Step 1: Generate the Lint Package

bash
archipelago generate app_lints

You will be prompted for:

  • appNameMyApp (must match your monorepo app name)
  • isForMonorepotrue

Or use a config file:

json
{
  "appName": "MyApp",
  "isForMonorepo": true
}
bash
archipelago generate app_lints --config lints_config.json

Step 2: Understand the Generated Structure

The brick generates a Dart package with custom analysis rules:

packages/
└── my_app_lints/
    ├── lib/
    │   ├── my_app_lints.dart              # Package exports
    │   └── src/
    │       ├── analysis_options.yaml      # Base analysis options
    │       ├── rules/
    │       │   ├── avoid_print_rule.dart   # Example custom rule
    │       │   └── feature_import_rule.dart # Architecture enforcement
    │       └── all_rules.dart             # Rule registry
    ├── pubspec.yaml
    └── README.md

Step 3: Apply Lints to Your Packages

Include the lint package in each package's analysis_options.yaml:

yaml
# In any package's analysis_options.yaml
include: package:my_app_lints/analysis_options.yaml

Add it as a dev dependency:

yaml
# pubspec.yaml
dev_dependencies:
  my_app_lints:
    path: ../../packages/my_app_lints

Step 4: Add Custom Rules

Create rules that enforce your project's architecture conventions:

dart
// lib/src/rules/feature_import_rule.dart
// Prevents features from importing other feature implementations directly
// Features should only depend on _api packages, never _impl
class FeatureImportRule extends LintRule {
  @override
  void check(Source source) {
    // Flag imports like: import 'package:auth_sdk_impl/...'
    // from within another feature's code
  }
}

Key Customization Points

CustomizationWhere to Change
Add new lint ruleslib/src/rules/ — create new rule classes
Adjust severity levelsanalysis_options.yaml — set info, warning, error
Exclude files from rulesanalysis_options.yaml — add to exclude list
Enforce naming conventionsAdd a naming rule in rules/

Next Steps

Built by Banua Coder