oxlint-plugin-no-branching
v0.1.2
Published
Oxlint JS plugin that bans switch statements and else branches to encourage early returns and strategy/map dispatch.
Maintainers
Readme
oxlint-plugin-no-branching
Ban switch and else in Oxlint. Encourage early returns and strategy/map dispatch.
Install
npm i -D oxlint-plugin-no-branchingOxlint Config (Explicit)
{
"jsPlugins": ["oxlint-plugin-no-branching"],
"rules": {
"no-branching/no-switch": "error",
"no-branching/no-else": "error"
}
}Rules
no-branching/no-switch
Errors on any SwitchStatement.
Message:
Switch statements are not allowed. Use a function with early returns or a strategy object (or a map).
no-branching/no-else
Errors on any IfStatement where alternate != null.
This bans both else {} and else if.
Message:
Else is not allowed. Use early returns or a strategy object (or a map).
Quick Examples
Fails:
switch (kind) {
case "a":
return 1;
default:
return 0;
}if (!user) {
return "anonymous";
} else {
return user.name;
}Passes:
if (!user) return "anonymous";
return user.name;const handlers = { a: () => 1, b: () => 2 };
return (handlers[kind] ?? (() => 0))();Local Demo
oxlint examplesYou should see errors in the examples/fail-*.js files.
