Skip to content

Require Module-Level Instantiation

ErrorRoblox
small-rules/require-module-level-instantiation

Require configured classes to be instantiated at module level only.

Rationale

Some classes should only ever exist once per file, and Log from @rbxts/rbxts-sleitnick-log is the one I care about: one logger per file, not a fresh instance every time a function runs. Let a class get instantiated inside a loop or a helper and you end up with a pile of them doing the same job.

You tell the rule which classes matter through the classes option (class name mapped to its import source). Anything you configure has to be created at module scope, or it reports. Keeps the expensive or stateful stuff exactly where it belongs.

Diagnostic Messages

mustBeModuleLevel
'{{className}}' from '{{importSource}}' must be instantiated at module level only.

Configuration

This rule accepts one options object after the severity.

classesOptional

Class names mapped to the import source that must be instantiated at module scope.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/require-module-level-instantiation": [
"error",
{
"classes": {
"Log": "@rbxts/rbxts-sleitnick-log"
}
}
]
}
}

Examples

nested logger instantiation
import Log from "@rbxts/rbxts-sleitnick-log";
function useStoryModesState() {
const log = new Log();
log.Info("test");
}
module-level logger instantiation
import Log from "@rbxts/rbxts-sleitnick-log";
const log = new Log();
function useStoryModesState() {
log.Info("test");
}