DirectInstantiationAnalyzer class

Analyzer that enforces dependency injection for better testability of auth/sync components.

This rule flags all direct instantiations of classes, except:

  • Classes whose names end with 'Factory' (e.g., FileProcessorFactory)
  • Classes that extend 'Module'
  • Any instantiation that occurs inside a class that extends 'Module'
  • Flutter widgets, BLoC states, exceptions, and other legitimate patterns

Example:

// ❌ Not allowed:
final service = AuthService();

// ✅ Allowed:
final service = Modular.get<AuthService>();
final factory = FileProcessorFactory();
final module = AppModule();
final widget = Container();
final state = AuthInitial();

// ✅ Allowed: Instantiation inside a Module
class AppModule extends Module {
  AppModule() {
    final service = AuthService(); // Allowed here
  }
}
Inheritance

Properties

correctionMessage String
The suggested correction message for fixing a violation of this rule.
no setteroverride
hashCode int
The hash code for this object.
no setterinherited
problemMessage String
The main problem message shown when this rule is violated.
no setteroverride
ruleName String
The unique name of the lint rule implemented by this analyzer.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
severity String
The severity of the lint rule (e.g., 'ERROR', 'WARNING').
no setterinherited

Methods

analyze(CompilationUnit unit) List<LintIssue>
Analyze the given CompilationUnit and return a list of LintIssues.
override
analyzeWithResolver(CompilationUnit unit, dynamic resolver) List<LintIssue>
Analyze the given CompilationUnit with resolver context and return a list of LintIssues.
override
createIssue(AstNode node, {String? customMessage}) LintIssue
Helper to create a LintIssue from an AstNode with consistent formatting.
inherited
isExcludedClass(String className, InstanceCreationExpression node) bool
Returns true if the class should be excluded from the rule (e.g., Factory or Module classes).
isInsideModule(AstNode node) bool
Returns true if the given node is inside a class that extends 'Module'.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited