passsy / one-question-per-ifr3
One Question per if
PublicJoin conditions with && only when they form one question, and give each separate question its own nested if.
tests, run by the registry9 passed · slowest 4.4 ms
- The advice links to wellactually.dev
✓fires2.9 ms
an if joining two clauses fires
write lib/sync.dartfired: line 2
void sync() {
if (fileChanged && Platform.isMacOS) {
refreshFinderIcon();
}
}✓fires2.8 ms
it does not judge: a null check joined to its use fires too
write lib/sync.dartfired: line 2
void sync() {
if (user != null && user.isAdmin) {
open();
}
}✓fires3.1 ms
an else if fires
write lib/sync.dartfired: line 4
void sync() {
if (isIdle) {
open();
} else if (fileChanged && isVisible) {
open();
}
}✓fires3.1 ms
a condition that wraps fires on the line of its if, wherever the && sits
write lib/sync.dartfired: line 2
void sync() {
if (fileChanged &&
Platform.isMacOS) {
open();
}
}write lib/sync.dartfired: line 2
void sync() {
if (fileChanged
&& Platform.isMacOS) {
open();
}
}✓fires3.3 ms
adding a clause on a continuation line fires for the if above it
write lib/sync.dart · wrote lines 4, 5fired: line 2
void sync() {
if (fileChanged(
path,
) &&
Platform.isMacOS) {
open();
}
}✓quiet4.4 ms
editing a line outside a joined condition stays quiet, and so does a read
write lib/sync.dart · wrote line 3stayed quiet
void sync() {
if (fileChanged && isVisible) {
open();
}
}read lib/sync.dartno handler of the detector wants this event
void sync() {
if (fileChanged && isVisible) {
open();
}
}✓quiet3.3 ms
the nested form the rule asks for stays quiet
write lib/sync.dartstayed quiet
void sync() {
if (fileChanged) {
if (Platform.isMacOS) {
refreshFinderIcon();
}
}
}✓quiet2.9 ms
an && outside an if condition or in a comment stays quiet
write lib/sync.dartstayed quiet
void sync() {
final ready = fileChanged && isVisible;
// if (fileChanged && isVisible) was joined before
if (name == 'a') {
open();
}
}✓skips0.0 ms
other languages are left alone
write src/sync.tsno handler of the detector wants this event
if (changed && visible) {
open();
}detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, read, source, write } from "@wellactually/sdk/v3/test";
/** A Dart file whose body is `lines`, written by the agent in one write. */
function sync(lines: string[]): string {
return ["void sync() {", ...lines.map((line) => ` ${line}`), "}"].join("\n");
}
test("an if joining two clauses fires", () => {
expect(detect(write("lib/sync.dart", sync(["if (fileChanged && Platform.isMacOS) {", " refreshFinderIcon();", "}"])))).toEqual([{ line: 2 }]);
});
test("it does not judge: a null check joined to its use fires too", () => {
expect(detect(write("lib/sync.dart", sync(["if (user != null && user.isAdmin) {", " open();", "}"])))).toEqual([{ line: 2 }]);
});
test("an else if fires", () => {
expect(detect(write("lib/sync.dart", sync(["if (isIdle) {", " open();", "} else if (fileChanged && isVisible) {", " open();", "}"])))).toEqual([{ line: 4 }]);
});
test("a condition that wraps fires on the line of its if, wherever the && sits", () => {
expect(detect(write("lib/sync.dart", sync(["if (fileChanged &&", " Platform.isMacOS) {", " open();", "}"])))).toEqual([{ line: 2 }]);
expect(detect(write("lib/sync.dart", sync(["if (fileChanged", " && Platform.isMacOS) {", " open();", "}"])))).toEqual([{ line: 2 }]);
});
test("adding a clause on a continuation line fires for the if above it", () => {
const code = source`
void sync() {
if (fileChanged(
path,
) &&
Platform.isMacOS) {
open();
}
}
`;
expect(detect(edit("lib/sync.dart", code, { written: " ) &&\n Platform.isMacOS) {" }))).toEqual([{ line: 2 }]);
});
test("editing a line outside a joined condition stays quiet, and so does a read", () => {
const code = sync(["if (fileChanged && isVisible) {", " open();", "}"]);
expect(detect(edit("lib/sync.dart", code, { written: " open();" }))).toEqual([]);
expect(detect(read("lib/sync.dart", code))).toEqual([]);
});
test("the nested form the rule asks for stays quiet", () => {
expect(detect(write("lib/sync.dart", sync(["if (fileChanged) {", " if (Platform.isMacOS) {", " refreshFinderIcon();", " }", "}"])))).toEqual([]);
});
test("an && outside an if condition or in a comment stays quiet", () => {
const lines = ["final ready = fileChanged && isVisible;", "// if (fileChanged && isVisible) was joined before", "if (name == 'a') {", " open();", "}"];
expect(detect(write("lib/sync.dart", sync(lines)))).toEqual([]);
});
test("other languages are left alone", () => {
expect(detect(write("src/sync.ts", "if (changed && visible) {\n open();\n}"))).toEqual([]);
});