To build a new linting package, you must define a plugin entrypoint and implement your lint rules using custom_lint_builder.
- Add
custom_lint_builder and analyzer to your pubspec.yaml dependencies. - Create a file at
lib/<your_package_name>.dart. - Implement
createPlugin() which returns an instance of PluginBase. - In your
PluginBase implementation, override getLintRules to return a list of LintRule objects. - Define your rules by extending
DartLintRule and implementing the run method to inspect the AST via the CustomLintContext registry.
# pubspec.yaml
name: my_custom_lint_package
environment:
sdk: ">=3.0.0 <4.0.0"
dependencies:
analyzer:
analyzer_plugin:
custom_lint_builder:
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
PluginBase createPlugin() => _ExampleLinter();
class _ExampleLinter extends PluginBase {
@override
List<LintRule> getLintRules(CustomLintConfigs configs) => [
MyCustomLintCode(),
];
}
class MyCustomLintCode extends DartLintRule {
MyCustomLintCode() : super(code: _code);
static const _code = LintCode(
name: 'my_custom_lint_code',
problemMessage: 'This is the description of our custom lint',
);
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
context.registry.addVariableDeclaration((node) {
reporter.atNode(node, code);
});
}
}