runWithReporter method
Override this method to implement your lint rule.
Use context to register callbacks for AST node types:
context.addMethodInvocation((node) {
if (condition) {
reporter.atNode(node);
}
});
Implementation
@override
void runWithReporter(
SaropaDiagnosticReporter reporter,
SaropaContext context,
) {
void checkMembers(List<ClassMember> members) {
final List<ConstructorDeclaration> ctors = members
.whereType<ConstructorDeclaration>()
.toList();
if (ctors.length < 2) return;
int unnamed = 0;
final Map<String, int> perName = <String, int>{};
for (final ConstructorDeclaration c in ctors) {
final Token? nameTok = c.name;
if (nameTok == null) {
unnamed++;
} else {
final String n = nameTok.lexeme;
perName[n] = (perName[n] ?? 0) + 1;
}
}
if (unnamed > 1) {
for (final ConstructorDeclaration c in ctors) {
if (c.name == null) reporter.atNode(c, code);
}
}
for (final MapEntry<String, int> e in perName.entries) {
if (e.value <= 1) continue;
for (final ConstructorDeclaration c in ctors) {
if (c.name?.lexeme == e.key) reporter.atNode(c, code);
}
}
}
context.addClassDeclaration(
(ClassDeclaration node) => checkMembers(node.bodyMembers),
);
context.addEnumDeclaration(
(EnumDeclaration node) => checkMembers(node.bodyMembers),
);
}