passsy / avoid-singletonsr3
Avoid Singletons
PublicInject dependencies instead of reaching for a singleton, so tests can replace them with a fake or a mock.
detector.tsisolate · reads files, writes none · no network · 100 ms · 32 MBEdit
import { mentioned, onPrompt, onWrite } from "@wellactually/sdk/v3";
import { DART, isDartProject } from "dart-kit";
onPrompt({
*detect(event) {
if (isDartProject(event)) {
yield* mentioned(event, ["singleton", "singletons"]);
}
},
});
// The static instance field and the private constructor that feeds it.
onWrite({
globs: DART,
*detect(event) {
for (const written of event.written) {
if (/static\s+final\s+\w+\s+instance\b|static\s+\w+\??\s+_instance\b|\w+\._internal\(/.test(written.text)) {
yield { line: written.line };
}
}
},
});
fixtures/dart_app/pubspec.yamlEdit
namedemo
package.jsonEdit
{
"name": "avoid-singletons",
"private": true,
"dependencies": {
"dart-kit": "*"
},
"tags": [
"dart"
]
}