passsy / stateful-widget-organizationr4
Nothing Between StatefulWidget and Its State
PublicKeep a StatefulWidget and its State class next to each other, with no function, variable or other class declared between them.
tests, run by the registry8 passed · slowest 6.0 ms
✓fires3.7 ms
fires on a declaration between the widget and its State
write lib/counter.dartfired: line 8
class Counter extends StatefulWidget {
const Counter({super.key});
State<Counter> createState() => _CounterState();
}
const _step = 1;
class _CounterState extends State<Counter> {
int count = 0;
}✓fires2.9 ms
fires on a function between the pair
write lib/counter.dartfired: line 6
class Counter extends StatefulWidget {
State<Counter> createState() => _CounterState();
}
int increment(int value) => value + 1;
class _CounterState extends State<Counter> {}✓quiet3.0 ms
stays quiet when the pair is adjacent
write lib/counter.dartstayed quiet
class Counter extends StatefulWidget {
const Counter({super.key});
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int count = 0;
}
const _step = 1;✓quiet2.4 ms
stays quiet when the edit does not touch the pair
write lib/counter.dart · wrote line 11stayed quiet
class Counter extends StatefulWidget {
const Counter({super.key});
State<Counter> createState() => _CounterState();
}
const _step = 1;
class _CounterState extends State<Counter> {
int count = 0;
}✓fires6.0 ms
fires when the edit touches the pair
write lib/counter.dart · wrote line 10fired: line 8
class Counter extends StatefulWidget {
const Counter({super.key});
State<Counter> createState() => _CounterState();
}
const _step = 1;
class _CounterState extends State<Counter> {
int count = 0;
}✓skips0.0 ms
stays quiet when the agent only reads the file
read lib/counter.dartno handler of the detector wants this event
class Counter extends StatefulWidget {
const Counter({super.key});
State<Counter> createState() => _CounterState();
}
const _step = 1;
class _CounterState extends State<Counter> {
int count = 0;
}✓skips0.0 ms
stays quiet in a test file
write test/counter_test.dartno handler of the detector wants this event
class Counter extends StatefulWidget {
const Counter({super.key});
State<Counter> createState() => _CounterState();
}
const _step = 1;
class _CounterState extends State<Counter> {
int count = 0;
}✓skips0.0 ms
has no text trigger
promptno handler of the detector wants this event
Put a helper between the StatefulWidget and its Statedetector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, prompt, read, source, write } from "@wellactually/sdk/v3/test";
const split = source`
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
const _step = 1;
class _CounterState extends State<Counter> {
int count = 0;
}
`;
test("fires on a declaration between the widget and its State", () => {
expect(detect(write("lib/counter.dart", split))).toEqual([{ line: 8 }]);
});
test("fires on a function between the pair", () => {
const code = source`
class Counter extends StatefulWidget {
@override
State<Counter> createState() => _CounterState();
}
int increment(int value) => value + 1;
class _CounterState extends State<Counter> {}
`;
expect(detect(write("lib/counter.dart", code))).toEqual([{ line: 6 }]);
});
test("stays quiet when the pair is adjacent", () => {
const code = source`
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int count = 0;
}
const _step = 1;
`;
expect(detect(write("lib/counter.dart", code))).toEqual([]);
});
test("stays quiet when the edit does not touch the pair", () => {
expect(detect(edit("lib/counter.dart", split, { written: " int count = 0;" }))).toEqual([]);
});
test("fires when the edit touches the pair", () => {
expect(detect(edit("lib/counter.dart", split, { written: "class _CounterState extends State<Counter> {" }))).toEqual([{ line: 8 }]);
});
test("stays quiet when the agent only reads the file", () => {
expect(detect(read("lib/counter.dart", split))).toEqual([]);
});
test("stays quiet in a test file", () => {
expect(detect(write("test/counter_test.dart", split))).toEqual([]);
});
test("has no text trigger", () => {
expect(detect(prompt("Put a helper between the StatefulWidget and its State"))).toEqual([]);
});